作为一般原则,您应该能够执行Magento系统本身所做的任何事情,而无需编写单行sql.几乎所有的Magento数据结构都使用Magento Model类.
在某处运行以下代码来查看salesrule /规则模型的外观.这假设您在管理员中创建了一个ID为1的单个购物车价格规则
$coupon = Mage::getModel('salesrule/rule')->load(1); var_dump($coupon->getData());
使用转储数据作为指导,我们可以使用以下方式编程创建模型
$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();