Creating a shopping cart price rule in Magento automatically

14,900

Solution 1

As a general principle, you should be able to do anything that the Magento system itself does without writing a single line of SQL. Almost all the Magento data structures use Magento Model classes.

Run the following code somewhere to see what a salesrule/rule model looks like. This assumes you've created a single Shopping Cart Price Rule in the admin with an ID of 1

    $coupon = Mage::getModel('salesrule/rule')->load(1);
    var_dump($coupon->getData());

Using the dumped data as a guide, we can programatically create a model using the following

    $coupon = Mage::getModel('salesrule/rule');
    $coupon->setName('test coupon')
    ->setDescription('this is a description')
    ->setFromDate('2010-05-09')
    ->setCouponCode('CODENAME')
    ->setUsesPerCoupon(1)
    ->setUsesPerCustomer(1)
    ->setCustomerGroupIds(array(1)) //an array of customer grou pids
    ->setIsActive(1)
    //serialized conditions.  the following examples are empty
    ->setConditionsSerialized('a:6:{s:4:"type";s:32:"salesrule/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}')
    ->setActionsSerialized('a:6:{s:4:"type";s:40:"salesrule/rule_condition_product_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}')
    ->setStopRulesProcessing(0)
    ->setIsAdvanced(1)
    ->setProductIds('')
    ->setSortOrder(0)
    ->setSimpleAction('by_percent')
    ->setDiscountAmount(10)
    ->setDiscountQty(null)
    ->setDiscountStep('0')
    ->setSimpleFreeShipping('0')
    ->setApplyToShipping('0')
    ->setIsRss(0)
    ->setWebsiteIds(array(1));      
    $coupon->save();

For anyone that's curious, the above is generated code, using the technique discussed here

Solution 2

Have a look at my code.It will add Action condition.

$coupon_rule = Mage::getModel('salesrule/rule');
    $coupon_rule->setName($c_data[1])
    ->setDescription($c_data[2])
    ->setFromDate($fromDate)
 ->setToDate($toDate)
    ->setUsesPerCustomer(0)
    ->setCustomerGroupIds(array(0,1,2,3)) //an array of customer grou pids
    ->setIsActive(1)
 ->setCouponType(2)
 ->setCouponCode($c_data[0])
    ->setUsesPerCoupon(1)

    //serialized conditions.  the following examples are empty
    ->setConditionsSerialized('')

    ->setActionsSerialized('') 
    ->setStopRulesProcessing(0)
    ->setIsAdvanced(1)
 ->setProductIds('')
    ->setSortOrder(0)
    ->setSimpleAction('by_percent')
    ->setDiscountAmount($c_data[5])
    ->setDiscountQty(1)
    ->setDiscountStep('0')
    ->setSimpleFreeShipping('0')
    ->setApplyToShipping('1')
    ->setIsRss(1)
    ->setWebsiteIds(explode(',',$c_data[6]));

$sku =$c_data[7];            // Put your product SKU here 
$skuCond = Mage::getModel('salesrule/rule_condition_product')
           ->setType('salesrule/rule_condition_product')
           ->setAttribute('sku')
           ->setOperator('==')
           ->setValue($sku);
$coupon_rule->getActions()->addCondition($skuCond);  

    $coupon_rule->save();

echo "New Coupon was added and its ID is ".$coupon_rule->getId().'<br/>';<br/> 

If you want to add Condition for shopping cart price rule then follow this example.

$sku =$c_data[7];            // Put your product SKU here 
$found = Mage::getModel('salesrule/rule_condition_product_found')
         ->setType('salesrule/rule_condition_product_found') 
         ->setValue(1)           // 1 == FOUND
         ->setAggregator('all'); // match ALL conditions
$coupon_rule->getConditions()->addCondition($found);
$skuCond = Mage::getModel('salesrule/rule_condition_product')
           ->setType('salesrule/rule_condition_product')
           ->setAttribute('sku') 
           ->setOperator('==')
           ->setValue($sku);

$found->addCondition($skuCond);    
     $coupon_rule->save();



Share:
14,900
Laizer
Author by

Laizer

US native. Israel-based software development. Currently working with Sefaria, open-sourcing Jewish texts. Besides software dev, interests in: Design Complexity Cities Sustainable Tech Lev on LinkedIn

Updated on June 05, 2022

Comments

  • Laizer
    Laizer almost 2 years

    I'd like to create a shopping cart price rule that gives a user 10% off their order when and if they complete a process on my Magento site.

    There's a method here that inserts the rule directly to the database. That's a bit invasive for my tastes.

    How would I go about this using Magento methods?

  • Laizer
    Laizer almost 14 years
    I just ran into the exact same issue as Ken posted, below. The actions aren't set by setActionsSerialized()
  • Alan Storm
    Alan Storm almost 14 years
    Yeah, it looks like actions are stored as separate Models in the system somewhere and then added to salesrule/rule. My guess is the serialized field exists for quicker access. So, after doing the above you'd manually add them (via some method, or maybe setting their rule id).
  • Laizer
    Laizer almost 14 years
    Yup - looks like Mage_SalesRule_Model_Rule_Action_Product