Naveenbos Naveenbos - 7 months ago 24
PHP Question

Event observer not getting product details

I have created a observer event like below inside my config.xml file

<events>
<sales_quote_add_item>
<observers>
<CustomModule_model_observer>
<type>singleton</type>
<class>CustomModule_Model_Observer</class>
<method>updatePrice</method>
</CustomModule_model_observer>
</observers>
</sales_quote_add_item>
</events>


In my Model i have created Observer.php

require_once 'Mage/Checkout/controllers/CartController.php';

class CustomModule_Observer
{
public function _construct()
{
}

public function getNewPrice()
{

$newprice = "5";

return $newprice;
}

public function updatePrice($observer)
{
$event = $observer->getEvent();
$product = $event->getProduct();// not getting any values how can i get this.
$quote_item = $event->getQuoteItem();
$new_price = $this->getNewPrice();
$quote_item->setOriginalCustomPrice($new_price);
$quote_item->save();
}
}


Reference Changing the price in quote while adding product to cart: magento

Answer

I have tried this, and its working, it gives us the product id.

$quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems();
foreach ($cartItems as $item){
   $productId = $item->getProductId();
   $product = Mage::getModel('catalog/product')->load($productId);
   // Do something
}