2
votes

I'm trying to create catalog price rule programmatically (percentage discount on category for specific customer group). But it is not being applied programmatically. Even if you go to Catalog Price Rules section, and press "Apply Rules" button - it is not being applied. It applies only if you go to edit this rule in backend, save it without any modification, and then press "Apply Rules". Here is the code:

$catalogRule = Mage::getModel('catalogrule/rule');
$catalogRule->setName($ruleName)
     ->setIsActive(true)
     ->setWebsiteIds(array(1))
     ->setCustomerGroupIds(array($group->getId()))
     ->setSimpleAction('by_percent')
     ->setDiscountAmount($discountAmount);

$categoryCondition = Mage::getModel('catalogrule/rule_condition_combine')
     ->setType('catalogrule/rule_condition_product')
     ->setAttribute('category_ids')
     ->setOperator('==')
     ->setValue($categoryId);

$catalogRule->getConditions()->addCondition($categoryCondition);
$catalogRule->save();
$catalogRule->applyAll();

Mage::app()->removeCache('catalog_rules_dirty');

Thank you for help!

1

1 Answers

1
votes

You have error in $categoryCondition. You should use catalogrule/rule_condition_product model, and not catalogrule/rule_condition_combine. Moreover category id value should be of string type. This is really weird thing, but with integer value it is not working (tested on 1.6.1.0 version).

So, here is the solution:

$categoryCondition = Mage::getModel('catalogrule/rule_condition_product')
     ->setType('catalogrule/rule_condition_product')
     ->setAttribute('category_ids')
     ->setOperator('==')
     ->setValue("23"); // notice: this is should be a string!