[英]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. 我想创建一个购物车价格规则,当用户在我的Magento网站上完成流程时,可以为用户提供10%的订单折扣。
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? 我如何使用Magento方法解决这个问题?
As a general principle, you should be able to do anything that the Magento system itself does without writing a single line of SQL. 作为一般原则,您应该能够执行Magento系统本身所做的任何事情,而无需编写单行SQL。 Almost all the Magento data structures use Magento Model classes. 几乎所有Magento数据结构都使用Magento Model类。
Run the following code somewhere to see what a salesrule/rule model looks like. 在某处运行以下代码以查看salesrule / rule模型的外观。 This assumes you've created a single Shopping Cart Price Rule in the admin with an ID of 1 这假设您在管理员中创建了一个ID为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 对于任何好奇的人,使用此处讨论的技术生成代码
Have a look at my code.It will add Action condition. 看看我的代码。它会添加Action条件。
$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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.