Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

php - Creating a shopping cart price rule in Magento automatically

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...