在您的自定义模块中,将以下内容添加到config.xml
:
<models>
<salesrule>
<rewrite>
<quote_discount>Namespace_Module_Rewrite_SalesRule_Model_Quote_Discount</quote_discount>
</rewrite>
</salesrule>
</models>
<frontend>
<routers>
<checkout>
<args>
<modules>
<Namespace_Module before="Mage_Checkout">Namespace_Module_Checkout</Namespace_Module>
</modules>
</args>
</checkout>
</routers>
</frontend>
首先是重写Mage_SalesRule_Model_Quote_Discount
为Namespace_Module_Rewrite_SalesRule_Model_Quote_Discount
第二个是过载的控制器 Mage_Checkout_CartController
接下来添加以下文件app/code/community/Namespace/Module/controllers/Checkout/CartController.php
并插入以下代码:
<?php
require_once 'Mage/Checkout/controllers/CartController.php';
class Namespace_Module_Checkout_CartController extends Mage_Checkout_CartController
{
/**
* Initialize coupon
*/
public function couponPostAction()
{
/**
* No reason continue with empty shopping cart
*/
if (!$this->_getCart()->getQuote()->getItemsCount()) {
$this->_goBack();
return;
}
$couponCode = (string) $this->getRequest()->getParam('coupon_code');
if ($this->getRequest()->getParam('remove') == 1) {
$couponCode = '';
}
$oldCouponCode = $this->_getQuote()->getCouponCode();
if (!strlen($couponCode) && !strlen($oldCouponCode)) {
$this->_goBack();
return;
}
try {
$codeLength = strlen($couponCode);
$isCodeLengthValid = $codeLength && $codeLength <= Mage_Checkout_Helper_Cart::COUPON_CODE_MAX_LENGTH;
// Combine multiple coupons
$couponFlag = true;
if ($isCodeLengthValid) {
$del = ',';
if ($oldCouponCode) {
if ($oldCouponCode == $couponCode) {
$couponCode = $oldCouponCode;
} else {
$couponCode = $oldCouponCode . $del . $couponCode;
}
}
} else {
$couponCode = '';
}
$this->_getQuote()->getShippingAddress()->setCollectShippingRates(true);
$this->_getQuote()->setCouponCode($couponCode)
->collectTotals()
->save();
if ($codeLength) {
if ($isCodeLengthValid && $couponFlag) {
$this->_getSession()->addSuccess(
$this->__('Coupon code "%s" was applied.', Mage::helper('core')->escapeHtml($couponCode))
);
} else {
$this->_getSession()->addError(
$this->__('Coupon code "%s" is not valid.', Mage::helper('core')->escapeHtml($couponCode))
);
}
} else {
$this->_getSession()->addSuccess($this->__('Coupon code was canceled.'));
}
} catch (Mage_Core_Exception $e) {
$this->_getSession()->addError($e->getMessage());
} catch (Exception $e) {
$this->_getSession()->addError($this->__('Cannot apply the coupon code.'));
Mage::logException($e);
}
$this->_goBack();
}
}
您会注意到我添加了一个部分来组合以“,”分隔的优惠券代码。显然,这可以更加完善,您可能想要添加其他检查等,但是这段代码应该可以立即使用。
最后,我们需要添加完成所有魔力的作品。添加文件app/code/community/Namespace/Module/Rewrite/SalesRule/Model/Quote/Discount.php
并添加内容:
<?php
class Namespace_Module_Rewrite_SalesRule_Model_Quote_Discount extends Mage_SalesRule_Model_Quote_Discount
{
/**
* Collect address discount amount
*
* @param Mage_Sales_Model_Quote_Address $address
* @return Mage_SalesRule_Model_Quote_Discount
*/
public function collect(Mage_Sales_Model_Quote_Address $address)
{
Mage_Sales_Model_Quote_Address_Total_Abstract::collect($address);
$quote = $address->getQuote();
$store = Mage::app()->getStore($quote->getStoreId());
$this->_calculator->reset($address);
$items = $this->_getAddressItems($address);
if (!count($items)) {
return $this;
}
$couponCode = $quote->getCouponCode();
$couponArray = explode(',',$couponCode);
foreach ($couponArray as $couponCode) {
$this->_calculator->init($store->getWebsiteId(), $quote->getCustomerGroupId(), $couponCode);
$this->_calculator->initTotals($items, $address);
$eventArgs = array(
'website_id' => $store->getWebsiteId(),
'customer_group_id' => $quote->getCustomerGroupId(),
'coupon_code' => $couponCode,
);
$address->setDiscountDescription(array());
$items = $this->_calculator->sortItemsByPriority($items);
foreach ($items as $item) {
if ($item->getNoDiscount()) {
$item->setDiscountAmount(0);
$item->setBaseDiscountAmount(0);
}
else {
/**
* Child item discount we calculate for parent
*/
if ($item->getParentItemId()) {
continue;
}
$eventArgs['item'] = $item;
Mage::dispatchEvent('sales_quote_address_discount_item', $eventArgs);
if ($item->getHasChildren() && $item->isChildrenCalculated()) {
foreach ($item->getChildren() as $child) {
$this->_calculator->process($child);
$eventArgs['item'] = $child;
Mage::dispatchEvent('sales_quote_address_discount_item', $eventArgs);
$this->_aggregateItemDiscount($child);
}
} else {
$this->_calculator->process($item);
$this->_aggregateItemDiscount($item);
}
}
}
/**
* process weee amount
*/
if (Mage::helper('weee')->isEnabled() && Mage::helper('weee')->isDiscounted($store)) {
$this->_calculator->processWeeeAmount($address, $items);
}
/**
* Process shipping amount discount
*/
$address->setShippingDiscountAmount(0);
$address->setBaseShippingDiscountAmount(0);
if ($address->getShippingAmount()) {
$this->_calculator->processShippingAmount($address);
$this->_addAmount(-$address->getShippingDiscountAmount());
$this->_addBaseAmount(-$address->getBaseShippingDiscountAmount());
}
$this->_calculator->prepareDescription($address);
}
return $this;
}
}
基本上,此操作是分解优惠券,循环遍历每个优惠券代码,计算并更新报价总额。
为了测试,我设置了2个购物车规则:
- 测试1-10%的产品价格折扣-停止进一步处理规则:否
- 测试2-10%的产品价格折扣-停止进一步处理规则:否
没有优惠券:
添加了优惠券测试1:
添加了优惠券测试2
我已经测试了固定金额的折扣,并且效果也不错。
就像我说的那样,您可能需要添加其他检查,可能要检查重复项,但这是您将要开始的地方。对于前端,您可以添加一些逻辑拆分代码,但是可以按自己的喜好保留原样。