我已经工作了 3 天,试图在一个订单中应用多个优惠券代码。
我知道有可用的模块。但我不想花钱购买模块。我想要一些自定义代码,以便我可以在单个订单中应用超过 1 个优惠券代码。
为了添加 2 个优惠券代码,我所做的是这样的:
1) 在 sales_flat_quote 表中添加列 coupon_code2
ALTER TABLE sales_flat_quote
添加(
coupon_code2
varchar(255))“;
2) 要添加您的新优惠券代码 2,只需将其保存在报价单中:
Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode2($coupon_code)->save();
3)最后计算优惠券代码我在观察者文件中添加了函数:/magento/app/code/core/Mage/Sales/Model/Observer.php
public function setDiscountCouponCode2(Varien_Event_Observer $observer)
{
$couponCode2 = $observer->getEvent()->getQuote()->getCouponCode2();
$oCoupon = Mage::getModel('salesrule/coupon')->load($couponCode2, 'code');
$oRule = Mage::getModel('salesrule/rule')->load($oCoupon->getRuleId());
$ruleData = $oRule->getData();
$flag =0;
if(isset($ruleData['product_ids'])){
$product_ids = $ruleData['product_ids'];
foreach(Mage::getSingleton('checkout/session')->getQuote()->getAllItems() as $_item){
if($product_ids == $_item->getProductId()){
$flag = 1;
}
}
}
if($flag){
$ruleSimpleAction = $ruleData['simple_action'];
$ruleDiscountAmount = $ruleData['discount_amount'];
$quote=$observer->getEvent()->getQuote();
$quoteid=$quote->getId();
$discountAmount=$ruleDiscountAmount;
if($quoteid) {
if($discountAmount>0) {
$total=$quote->getBaseSubtotal();
$quote->setSubtotal(0);
$quote->setBaseSubtotal(0);
$quote->setSubtotalWithDiscount(0);
$quote->setBaseSubtotalWithDiscount(0);
$quote->setGrandTotal(0);
$quote->setBaseGrandTotal(0);
$canAddItems = $quote->isVirtual()? ('billing') : ('shipping');
foreach ($quote->getAllAddresses() as $address) {
$address->setSubtotal(0);
$address->setBaseSubtotal(0);
$address->setGrandTotal(0);
$address->setBaseGrandTotal(0);
$address->collectTotals();
$quote->setSubtotal((float) $quote->getSubtotal() + $address->getSubtotal());
$quote->setBaseSubtotal((float) $quote->getBaseSubtotal() + $address->getBaseSubtotal());
$quote->setSubtotalWithDiscount(
(float) $quote->getSubtotalWithDiscount() + $address->getSubtotalWithDiscount()
);
$quote->setBaseSubtotalWithDiscount(
(float) $quote->getBaseSubtotalWithDiscount() + $address->getBaseSubtotalWithDiscount()
);
$quote->setGrandTotal((float) $quote->getGrandTotal() + $address->getGrandTotal());
$quote->setBaseGrandTotal((float) $quote->getBaseGrandTotal() + $address->getBaseGrandTotal());
$quote ->save();
$quote->setGrandTotal($quote->getBaseSubtotal()-$discountAmount)
->setBaseGrandTotal($quote->getBaseSubtotal()-$discountAmount)
->setSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmount)
->setBaseSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmount)
->save();
if($address->getAddressType()==$canAddItems) {
$address->setSubtotalWithDiscount((float) $address->getSubtotalWithDiscount()-$discountAmount);
$address->setGrandTotal((float) $address->getGrandTotal()-$discountAmount);
$address->setBaseSubtotalWithDiscount((float) $address->getBaseSubtotalWithDiscount()-$discountAmount);
$address->setBaseGrandTotal((float) $address->getBaseGrandTotal()-$discountAmount);
if($address->getDiscountDescription()){
$address->setDiscountAmount(-($address->getDiscountAmount()-$discountAmount));
$address->setDiscountDescription($address->getDiscountDescription().', Instant Exchange -'.$couponCode2);
$address->setBaseDiscountAmount(-($address->getBaseDiscountAmount()-$discountAmount));
}else {
$address->setDiscountAmount(-($discountAmount));
$address->setDiscountDescription('Instant Exchange -'.$couponCode2);
$address->setBaseDiscountAmount(-($discountAmount));
}
$address->save();
}
}
foreach($quote->getAllItems() as $item){
$rat=$item->getPriceInclTax()/$total;
$ratdisc=$discountAmount*$rat;
$item->setDiscountAmount(($item->getDiscountAmount()+$ratdisc) * $item->getQty());
$item->setBaseDiscountAmount(($item->getBaseDiscountAmount()+$ratdisc) * $item->getQty())->save();
}
}
}
}else{
Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode2('')->save();
}
}
4) 最后通过更新同一文件中的 setQuoteCanApplyMsrp 函数调用上述函数:
public function setQuoteCanApplyMsrp(Varien_Event_Observer $observer)
{
/** @var $quote Mage_Sales_Model_Quote */
$quote = $observer->getEvent()->getQuote();
$canApplyMsrp = false;
if (Mage::helper('catalog')->isMsrpEnabled()) {
foreach ($quote->getAllAddresses() as $adddress) {
if ($adddress->getCanApplyMsrp()) {
$canApplyMsrp = true;
break;
}
}
}
$this->setDiscountCouponCode2($observer);
$quote->setCanApplyMsrp($canApplyMsrp);
}