Magento combine catalogue price rules

555 views Asked by At

I am trying to create a magento catalogue price rule with condition combined using 'any' instead of 'all'

The following code creates a rule with two conditons - but they are combined using all. Does anyone know how to achieve this? I'm using magento 1.7.0.2 (Community Edition)

$skuCondition = Mage::getModel('catalogrule/rule_condition_product')
                    ->setType('catalogrule/rule_condition_product')
                ->setAggregator('any')
                ->setAttribute('category_ids')
                    ->setOperator('==')
                ->setValue('18');


$skuCondition2 = Mage::getModel('catalogrule/rule_condition_product')
                    ->setType('catalogrule/rule_condition_product')
                ->setAttribute('category_ids')
                    ->setOperator('==')
                ->setValue('40');

 $catalogPriceRule->getConditions()->addCondition($skuCondition);
 $catalogPriceRule->getConditions()->addCondition($skuCondition2);

$catalogPriceRule->save(); 
1

There are 1 answers

1
Lee Dyche On

By changing my approach and using one rule with a different condition I managed to achieve the same thing as below. Notice the use of '()' instead of '==' to signify 'is any of'. I found this solution by creating a rule and looking at the data in the database table 'catalogrule' if you pick apart the column 'condition_serialized' i found

8:"operator";s:2:"()";s

Where the "()" is the operator

So my final code was:

$catalogPriceRule->setName($name)
                 ->setDescription('')
                 ->setIsActive(1)
                 ->setWebsiteIds(array($websiteId))
                 ->setCustomerGroupIds(array($customerGroupId))
                 ->setFromDate('')
                 ->setToDate('')
                 ->setSortOrder('')
                 ->setSimpleAction($actionType)
                 ->setDiscountAmount($discount)
                 ->setStopRulesProcessing(0);

$skuCondition = Mage::getModel('catalogrule/rule_condition_product')
                ->setType('catalogrule/rule_condition_product')
                ->setAggregator('any')
                ->setAttribute('category_ids')
                ->setOperator('()')
                ->setValue('18,40');

$catalogPriceRule->getConditions()->addCondition($skuCondition);
$catalogPriceRule->save();  

Hope that helps someone!