我想創建一個購物車價格規則,當他們在我的Magento網站上完成一個過程時,可以爲用戶提供10%的訂單。在Magento中自動創建購物車價格規則
有一種方法here直接插入規則到數據庫。這對我的口味有點侵入性。
我會如何去使用Magento方法?
我想創建一個購物車價格規則,當他們在我的Magento網站上完成一個過程時,可以爲用戶提供10%的訂單。在Magento中自動創建購物車價格規則
有一種方法here直接插入規則到數據庫。這對我的口味有點侵入性。
我會如何去使用Magento方法?
作爲一般原則,您應該能夠做任何Magento系統本身所做的事情,而無需編寫一行SQL。幾乎所有的Magento數據結構都使用Magento Model類。
在某處運行以下代碼以查看銷售規則/規則模型的外觀。這是假設你已經創建了一個管理員的單一購物車價格規則與1
$coupon = Mage::getModel('salesrule/rule')->load(1);
var_dump($coupon->getData());
使用轉儲的數據爲指導的ID,我們可以使用下面的
$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();
編程方式創建一個模型
對於任何好奇的人,上面是生成的代碼,使用討論的技巧here
看看我的code.It將添加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/>
如果要添加條件的購物車價格規則然後按照這個例子。
$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();
我只是碰到了完全相同的問題,因爲肯發佈,以下。這些動作不是由setActionsSerialized()設置的 – Laizer 2010-05-26 15:06:34
是的,它看起來像動作存儲在系統某個地方的單獨的模型,然後添加到salesrule /規則。我的猜測是序列化字段存在更快的訪問。因此,在完成上述操作後,您可以手動添加它們(通過某種方法,或者可以設置其規則ID)。 – 2010-05-26 16:05:01
是 - 看起來像Mage_SalesRule_Model_Rule_Action_Product – Laizer 2010-05-27 14:23:25