skip to Main Content

I have below array and I want to simplify array to add my logic to check product qty

The array is as below:-

$conditions = Array
    (
        [type] => MagentoSalesRuleModelRuleConditionCombine
        [attribute] => 
        [operator] => 
        [value] => 1
        [is_value_processed] => 
        [aggregator] => all
        [conditions] => Array
            (
                [0] => Array
                    (
                        [type] => MagentoSalesRuleModelRuleConditionProductFound
                        [attribute] => 
                        [operator] => 
                        [value] => 1
                        [is_value_processed] => 
                        [aggregator] => all
                        [conditions] => Array
                            (
                                [0] => Array
                                    (
                                        [type] => MagentoSalesRuleModelRuleConditionProduct
                                        [attribute] => category_ids
                                        [operator] => ==
                                        [value] => 5
                                        [is_value_processed] => 
                                    )

                                [1] => Array
                                    (
                                        [type] => MagentoSalesRuleModelRuleConditionProduct
                                        [attribute] => category_factor
                                        [operator] => ==
                                        [value] => 13
                                        [is_value_processed] => 
                                    )                                                         

                            )

                    )

                [1] => Array
                    (
                        [type] => MagentlyCustomerRuleModelRuleConditionCustomer
                        [attribute] => product_qty
                        [operator] => >=
                        [value] => 99
                        [is_value_processed] => 
                    )

            )

    )

I want to check attribute operator and value like below :

if(product_qty >= 99){
//do your stuff
}

Please help me to simplify this array to add my condition to check the product qty for some purpose.

3

Answers


  1. If I correct understanding of your question.

    You can use array indexes like this:

    foreach($conditions['conditions'] as $condition){
    
        if($condition['attribute'] == 'product_qty' && $condition['value']  >= 99 ){
            //do your stuff
        }
    
    }
    

    this code checks when attribute is equal to product_qty and value of this item is more than equal 99. condition is true.

    Login or Signup to reply.
  2. I think you better create a function that has the array as input.
    it will do a for each like ttrasn mentioned:

    foreach($conditions['conditions'] as $condition){
    
        if($condition['attribute'] == 'product_qty' && $condition['value']  >= 99 ){
            //do your stuff
        }
    
    }
    

    But ad if $condition is an array, recursive call the same function with that array.

    Login or Signup to reply.
  3. You don’t have easy way to perform a comparison with the operator stored in a value, so you need to write the function yourself. A simple switch can do the job :

    function compareValue($variable, $operator, $value){
        switch($operator)
        {
            case '==' : return $variable == $value ; break ;
            case '>=' : return $variable >= $value ; break ;
            /* add all possibilities here */
            default : throw new RuntimeException("Undefined operator '$operator'");
        }
    }
    

    To evaluate you whole conditions, you can use recursion :

    • if it is a comparaison to a value, use the function written above
    • if it is an aggregate, evaluate all the childs and define the rule of the aggregate :

    Here is a skeleton :

    function checkConditions($product, $condition){
        if($condition['attribute'] !== false and $condition['operator'] !== false)
        {
            // simple condition 
            $variableToCheck = $product[ $condition['attribute'] ] ; // maybe use your API to query this value
            $operator = $condition['operator'] ;
            $valueToCompare = $condition['value'] ;
            return compareValue($variableToCheck, $operator, $valueToCompare);
        }
        else
        {
            // composite condition 
            if( $condition['aggregator'] == 'all' )
            {
                // check for each child condition
                foreach($condition['conditions'] as $childCondition)
                {
                    $childConditionReturn = checkConditions($product, $childCondition);
                    if($childConditionReturn === false)
                    {
                        // we found a child condition that is not satisfied, the whole 'all' aggregator is false
                        return false ;
                    }
                }
    
                // all childs were evaluated to true, whole 'all' aggregator is true
                return true ;
            }
            else // handle other aggregator types here
            {
    
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search