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] => Magento\SalesRule\Model\Rule\Condition\Combine
[attribute] =>
[operator] =>
[value] => 1
[is_value_processed] =>
[aggregator] => all
[conditions] => Array
(
[0] => Array
(
[type] => Magento\SalesRule\Model\Rule\Condition\Product\Found
[attribute] =>
[operator] =>
[value] => 1
[is_value_processed] =>
[aggregator] => all
[conditions] => Array
(
[0] => Array
(
[type] => Magento\SalesRule\Model\Rule\Condition\Product
[attribute] => category_ids
[operator] => ==
[value] => 5
[is_value_processed] =>
)
[1] => Array
(
[type] => Magento\SalesRule\Model\Rule\Condition\Product
[attribute] => category_factor
[operator] => ==
[value] => 13
[is_value_processed] =>
)
)
)
[1] => Array
(
[type] => Magently\CustomerRule\Model\Rule\Condition\Customer
[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.
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.
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.
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 :
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
{
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.