tags:

views:

684

answers:

2

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?

+3  A: 

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

Alan Storm
I just ran into the exact same issue as Ken posted, below. The actions aren't set by setActionsSerialized()
Laizer
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).
Alan Storm
Yup - looks like Mage_SalesRule_Model_Rule_Action_Product
Laizer
A: 

Hi Alan,

I found your method is very useful. But I met some problem when I follow you method to overwrite the existing rules.

it's able to change the discount amount by using coupon->setDiscountAmount(10);

but it's impossible to reset action by using coupon->setActionsSerialized();

my original data is ["actions_serialized"]=> string(377) "a:7:{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";s:10:"conditions";a:1:{i:0;a:5:{s:4:"type";s:32:"salesrule/rule_condition_product";s:9:"attribute";s:16:"quote_item_price";s:8:"operator";s:1:">";s:5:"value";s:4:"1000";s:18:"is_value_processed";b:0;}}}"

Then I did the following: $coupon->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";}'); $coupon->save();

But the action did not change to default (empty), it's still the same action as before. Do you know is there something wrong with my code? Thanks in advanced.

ken
See the comments above. No answers, but some places to start looking.
Alan Storm