2010-05-09 77 views
11

我想创建一个购物车价格规则,当他们在我的Magento网站上完成一个过程时,可以为用户提供10%的订单。在Magento中自动创建购物车价格规则

有一种方法here直接插入规则到数据库。这对我的口味有点侵入性。

我会如何去使用Magento方法?

回答

18

作为一般原则,您应该能够做任何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

+0

我只是碰到了完全相同的问题,因为肯发布,以下。这些动作不是由setActionsSerialized()设置的 – Laizer 2010-05-26 15:06:34

+0

是的,它看起来像动作存储在系统某个地方的单独的模型,然后添加到salesrule /规则。我的猜测是序列化字段存在更快的访问。因此,在完成上述操作后,您可以手动添加它们(通过某种方法,或者可以设置其规则ID)。 – 2010-05-26 16:05:01

+0

是 - 看起来像Mage_SalesRule_Model_Rule_Action_Product – Laizer 2010-05-27 14:23:25

6

看看我的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();