编程的方式创建,应用和删除Magento中的购物车价格规则

redmaomail 2024-07-24 18:12 阅读数 67 #Magento

红帽云邮外贸主机

默认的Magento中有个很棒的功能叫做“促销”。有两种类型的促销:目录价格跪着和购物车价格规则。这篇文章中我将演示如何以编程的方式来创建,应用和删除购物车价格规则。

有很多理由让你使用它。例如:你想模拟某种奖励功能,对达到要求的客户进行打折。除非正在使用Magento EE就不用了(带有奖励积分的功能),那么你将编写代码或者购买第三方插件。我个人不太喜欢用第三方插件,除非它是必须的或者能达到功能需求的90-95%。否则你将花费时间在修改插件上。让我们看看我们如何自己实现这个功能。一种方式是用代码粉方式创建和应用购物车价格规则,然后在执行的时候删除购物车价格规则。

首先,我们创建购物车价格规则。我们将以手动创建一个规则,然后学习后台进程来以编程的方式来复制它。我们将进入Magento后台,在“Promotions > Shopping Cart Price Rules”下,点击“Add New Rule”右上角按钮。在这个例子中,我们将限制一个客户每次只能使用一个优惠券。我们填上必须字段并用POST变量提交数据,最后的url会像这样:http://shop.net/index.php/admin/promo_quote/save/

form_key => YYhUwcLXX6sezYPY
product_ids => 
name => CUSTOMER_1 - 30% Summer discount
description => 
is_active => 1
website_ids[] => 1
customer_group_ids[] => 3
coupon_type => 2
coupon_code => leIYc4g1dbCCOlT1
uses_per_coupon => 1
uses_per_customer => 1
from_date => 
to_date => 
sort_order => 
is_rss => 1
rule[conditions][1][type] => salesrule/rule_condition_combine
rule[conditions][1][aggregator] => all
rule[conditions][1][value] => 1
rule[conditions][1][new_child] => 
simple_action => by_percent
discount_amount => 30
discount_qty => 0
discount_step => 
apply_to_shipping => 0
simple_free_shipping => 0
stop_rules_processing => 0
rule[actions][1][type] => salesrule/rule_condition_product_combine
rule[actions][1][aggregator] => all
rule[actions][1][value] => 1
rule[actions][1][new_child] => 
store_labels[0] => 
store_labels[1] => 

现在,如果我们研究http://shop.net/index.php/admin/promo_quote/save/ 后面的Mage_Adminhtml_Promo_QuoteController -> saveAction(),我们可以看到规则的创建:

$data = array(
    'product_ids' => null,
    'name' => sprintf('AUTO_GENERATION CUSTOMER_%s - 30%% Summer discount', Mage::getSingleton('customer/session')->getCustomerId()),
    'description' => null,
    'is_active' => 1,
    'website_ids' => array(1),
    'customer_group_ids' => array(1),
    'coupon_type' => 2,
    'coupon_code' => Mage::helper('core')->getRandomString(16),
    'uses_per_coupon' => 1,
    'uses_per_customer' => 1,
    'from_date' => null,
    'to_date' => null,
    'sort_order' => null,
    'is_rss' => 1,
    'rule' => array(
        'conditions' => array(
            array(
                'type' => 'salesrule/rule_condition_combine',
                'aggregator' => 'all',
                'value' => 1,
                'new_child' => null
            )
        )
    ),
    'simple_action' => 'by_percent',
    'discount_amount' => 30,
    'discount_qty' => 0,
    'discount_step' => null,
    'apply_to_shipping' => 0,
    'simple_free_shipping' => 0,
    'stop_rules_processing' => 0,
    'rule' => array(
        'actions' => array(
            array(
                'type' => 'salesrule/rule_condition_product_combine',
                'aggregator' => 'all',
                'value' => 1,
                'new_child' => null
            )
        )
    ),
    'store_labels' => array('30% Summer discount')
);
 
$model = Mage::getModel('salesrule/rule');
$data = $this->_filterDates($data, array('from_date', 'to_date'));
 
$validateResult = $model->validateData(new Varien_Object($data));
 
if ($validateResult == true) {
 
    if (isset($data['simple_action']) && $data['simple_action'] == 'by_percent'
            && isset($data['discount_amount'])) {
        $data['discount_amount'] = min(100, $data['discount_amount']);
    }
 
    if (isset($data['rule']['conditions'])) {
        $data['conditions'] = $data['rule']['conditions'];
    }
 
    if (isset($data['rule']['actions'])) {
        $data['actions'] = $data['rule']['actions'];
    }
 
    unset($data['rule']);
 
    $model->loadPost($data);
 
    $model->save();
}

上面涵盖了“create”部分,现在让我们转到应用部分。想法是:在执行chechout之前以编程的方式来应用优惠券。如果我们在像http://shop.net/checkout/cart/这样的url里应用优惠券,表单可能提交数据到http://shop.net/checkout/cart/couponPost/的controller动作。

提交数据非常简单:

remove => 0
coupon_code => da4K53cGuhNoTc10

现在,让我们看看http://shop.net/checkout/cart/couponPost/后面的controller动作。我们要看得更远点,我们将先寻找之前我们创建的特殊规则名。当筛选出特殊的数据check等等时。你留下类似“apply Shopping Cart Price Rule”代码的东西。

/* START This part is my extra, just to load our coupon for this specific customer */
$model = Mage::getModel('salesrule/rule')
        ->getCollection()
        ->addFieldToFilter('name', array('eq'=>sprintf('AUTO_GENERATION CUSTOMER_%s - 30%% Summer discount', Mage::getSingleton

('customer/session')->getCustomerId())))
        ->getFirstItem();
 
$couponCode = $model->getCouponCode();
/* END This part is my extra, just to load our coupon for this specific customer */
 
Mage::getSingleton('checkout/cart')
    ->getQuote()
    ->getShippingAddress()
    ->setCollectShippingRates(true);
 
Mage::getSingleton('checkout/cart')
    ->getQuote()
    ->setCouponCode(strlen($couponCode) ? $couponCode : '')
    ->collectTotals()
    ->save();

现在我们需要做的就是这个代码转移到合适的位置,像事件观察。最后一旦客户结账完成,我们就用一个简单的代码删除这个规则。

$model = Mage::getModel('salesrule/rule')
        ->getCollection()
        ->addFieldToFilter('name', array('eq'=>sprintf('AUTO_GENERATION CUSTOMER_%s - 30%% Summer discount', Mage::getSingleton('customer/session')->getCustomerId())))
        ->getFirstItem();
 
$model->delete();

电商网站开发服务


红帽云邮外贸主机

分享到:
版权声明:本站内容源自互联网,如有内容侵犯了你的权益,请联系删除相关内容。
    红帽云邮外贸主机
热门
    红帽云邮外贸主机
    红帽云邮外贸主机