在我的 magento 商店中,如何动态更改购物车中的折扣总额?
我可以使用以下代码访问并获得当前折扣:
<?php
$quote = Mage::getSingleton('checkout/cart')->getQuote();
$totals = $quote->getTotals();
$discount = $totals["discount"]->getValue();
?>
我的购物车中有一个按钮,按下该按钮时应在折扣值上增加 5 英镑,同时更新总成本等。
在magento中有一个事件,
sales_quote_collect_totals_after
每当计算您的总数时都会触发此操作,您可以在会话中设置一个标志,单击按钮以应用折扣,并在上述事件的观察者方法中,检查它是否已设置然后应用折扣。
在你的 config.xml
<global>
<events>
<sales_quote_collect_totals_after>
<observers>
<class>Custom_Module_Model_Observer</class>
<method>collectTotals</method>
</observers>
</sales_quote_collect_totals_after>
</events>
</global>
在里面制作一个 Observer.php
Custom
/Module
/Model
/Observer.php
在 Observer.php 中创建一个函数
public function collectTotals(Varien_Event_Observer $observer)
{
$quote=$observer->getEvent()->getQuote();
$quoteid=$quote->getId();
//check condition here if need to apply Discount
if($disocuntApply) $discountAmount =5;
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().', Amount Waived');
$address->setBaseDiscountAmount(-($address->getBaseDiscountAmount()-$discountAmount));
}else {
$address->setDiscountAmount(-($discountAmount));
$address->setDiscountDescription('Amount Waived');
$address->setBaseDiscountAmount(-($discountAmount));
}
$address->save();
}//end: if
} //end: foreach
//echo $quote->getGrandTotal();
foreach($quote->getAllItems() as $item){
//We apply discount amount based on the ratio between the GrandTotal and the RowTotal
$rat=$item->getPriceInclTax()/$total;
$ratdisc=$discountAmount*$rat;
$item->setDiscountAmount(($item->getDiscountAmount()+$ratdisc) * $item->getQty());
$item->setBaseDiscountAmount(($item->getBaseDiscountAmount()+$ratdisc) * $item->getQty())->save();
}
}
}
}
collectTotals
每当报价总额更新时,都会调用函数,因此无需显式调用它。
在这里检查它是如何工作的。
设置 magento 会话变量,请在此处查看。
希望能帮助到你!