Creating a shopping cart price rule in Magento automatically

后端 未结 2 992
无人及你
无人及你 2020-12-08 05:52

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

相关标签:
2条回答
  • 2020-12-08 06:13

    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

    0 讨论(0)
  • 2020-12-08 06:20

    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();
    



    0 讨论(0)
提交回复
热议问题