1

I want to add some free gift to cart , so I create an observer, the code is:

<?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();
        }

    }


}

but when I add an item to cart , in the shopping cart, the subtotal is 0, is anyone tell me how to solve this issue? when I add one more item or refresh the shopping cart page, the subtotal is correct

Leon Lai
  • 33
  • 1
  • 2
  • 5

2 Answers2

3

Try

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

at the end of your function

TaganPablo
  • 359
  • 1
  • 4
1

Try this one

$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();
Amit Kumar
  • 1,774
  • 1
  • 16
  • 27
  • Aha, I had tried this one but didn't get the correct result, but I add some code at the beginning , it works
    $cart = Mage::getSingleton('checkout/cart');
       $cart->init(); 
       $cart->save();
    – Leon Lai Mar 31 '13 at 07:23