自动在Magento中创建购物车价格规则

Lai*_*zer 11 php oop model-view-controller magento

我想创建一个购物车价格规则,当用户在我的Magento网站上完成流程时,可以为用户提供10%的订单折扣.

有一个方法,在这里直接插入规则到数据库中.这对我的口味有点侵略性.

我如何使用Magento方法解决这个问题?

Ala*_*orm 19

作为一般原则,您应该能够执行Magento系统本身所做的任何事情,而无需编写单行SQL.几乎所有Magento数据结构都使用Magento Model类.

在某处运行以下代码以查看salesrule/rule模型的外观.这假设您在管理员中创建了一个ID为1的购物车价格规则

    $coupon = Mage::getModel('salesrule/rule')->load(1);
    var_dump($coupon->getData());
Run Code Online (Sandbox Code Playgroud)

使用转储数据作为指导,我们可以使用以下方法以编程方式创建模型

    $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();
Run Code Online (Sandbox Code Playgroud)

对于任何好奇的人,使用此处讨论的技术生成代码


小智 6

看看我的代码.它会添加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/> 
Run Code Online (Sandbox Code Playgroud)

如果您要为购物车价格规则添加条件,请按照此示例操作.

$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();
Run Code Online (Sandbox Code Playgroud)