0

I have a store of around 30000 products and I want to apply rules for single product by using SKU of that Product. If i go and update the same via admin panel is reflecting but i would like to achieve the same pro-grammatically. I have also gone through applying rules pro-grammatically for all the products

  • 1
    Need to create new shopping cart / catalog rules for a product pro-grammatically or apply existing rules pro-grammatically? – Abdul Apr 24 '17 at 6:21
  • @abdul i don't want to create any rule, i just want to apply an existing rule to that sku – Guruprasad Apr 24 '17 at 6:27
  • is it shopping cart or catalog rules? – Abdul Apr 24 '17 at 6:39
  • @Abdul it is Catalog Rules – Guruprasad Apr 24 '17 at 6:58
3

try this

$ruleId = <rule_Id>;
$productSku = <sku>;    
$shoppingCartPriceRule = Mage::getModel('catalogrule/rule')->load($ruleId);
$skuCondition = Mage::getModel('catalogrule/rule_condition_product')
                ->setType('catalogrule/rule_condition_product')
                ->setAttribute('sku')
                ->setOperator('==')
                ->setValue($sku);
try {    
   $catalogPriceRule->getConditions()->addCondition($skuCondition);
   $catalogPriceRule->save();                
   $catalogPriceRule->applyAll();     
} catch (Exception $e) {
  echo $e->getMessage();
  die();//remove die when in production and set the error message in session and return
} 

I have not tested this code but this should work. Let us know if further help is needed.

  • @simplfsaif i want the rules applied to catalog rules, not for sales rules – Guruprasad Apr 24 '17 at 12:21
  • sorry sorry ! I have update answer – magento noob Apr 24 '17 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.