1

我想在购物车中添加一些免费礼物,所以我创建了一个观察者,代码是:

<?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,有人告诉我如何解决这个问题吗?当我再添加一项或刷新购物车页面时,小计是正确的

4

2 回答 2

3

尝试

$quote->collectTotals()->save()

在你的函数结束时

于 2013-03-29T12:37:27.237 回答
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();
于 2013-03-30T10:36:59.753 回答