我想在购物车中添加一些免费礼物,所以我创建了一个观察者,代码是:
<?php
class Free_Checkout_Model_Observer
{
public function modifyPrice(Varien_Event_Observer $observer)
{
$event = $observer->getEvent();
$quote = Mage::getModel('checkout/session')->getQuote();
$quote_item = $event->getQuoteItem();
$productId = $event->getQuoteItem()->getProduct()->getId();
$product = Mage::getModel('catalog/product')->load($productId);
$productData = $product->getData();
if($productData['gift']){
$new_price = 0;
$quote_item->setOriginalCustomPrice($new_price);
$quote_item->setCustomPrice($new_price);
$quote_item->save();
}
}
}
但是当我将商品添加到购物车时,在购物车中,小计为0,是否有人告诉我如何解决此问题? 当我再添加一个项目或刷新购物车页面时,小计是正确的
答案 0 :(得分:3)
尝试
$quote->collectTotals()->save()
在你的功能结束时
答案 1 :(得分:1)
试试这个
$quote = Mage::getSingleton('checkout/session')->getQuote();
foreach($quote->getAllItems() as $quote_item) {
$product = Mage::getModel('catalog/product')->load($quote_item->getProductId());
$productData = $product->getData();
if($productData['gift']){
$new_price = 0;
$quote_item->setOriginalCustomPrice($new_price);
$quote_item->setCustomPrice($new_price);
$quote_item->getProduct()->setIsSuperMode(true);
}
}
$quote->save();