Reputation: 405
I need to get quote item id of the product when it is added to cart because I need to update a field of Db after this event,I have taken reference from How to get Quote Item Id after adding a product to cart?, but this is not working,I have used event: sales_quote_product_add_after and my oberver function is:
$quoteItem = $observer->getEvent()->getQuoteItem();
$id = $quoteItem->getId();
also I have tried with
$id = $quoteItem->getItemId();
It is throwing following Fatal error:
Fatal error</b>: Call to a member function getId() on a non-object in <b>C:\xampp\htdocs\project\app\code\local\Custom\Module\Model\Observer.php</b> on line <b>1053</b><br />
Please let me know what I am doing wrong,I have taken reference from many other links too but none of them is working.
Upvotes: 2
Views: 3588
Reputation: 31
Following code may help you,
$customer = Mage::getModel('customer/customer')->load($customerId);
$quote = Mage::getModel('sales/quote')->setSharedStoreIds($storeIds)
->loadByCustomer($customer);
$collection = $quote->getItemsCollection();
print_r($collection->getData());
Load customer with customer_id and get quote info, so you can get quote details.
Upvotes: 0
Reputation: 668
As per my understanding, You will not get QuoteItems keys for this event. This event provide items array which contain all the items which are getting added for a product. So you don't have to change your code a lot.
$item = $observer->getEvent()->getItems()[0];
$item_id = $item->getId();
It should give you desired result.
Upvotes: 1
Reputation: 2125
You should have an observer defined in your module config file which calls a method when someone adds an item to the basket. Something like the following;
<events>
<sales_quote_item_set_product>
<observers>
<quoteitem_set_eta_data>
<type>singleton</type>
<class>NameSpace_Eta_Model_Observer</class>
<method>setEtaOnQuoteItem</method>
</quoteitem_set_eta_data>
</observers>
</sales_quote_item_set_product>
</events>
Your observer method will then have access to the quote item using the following;
public function setEtaOnQuoteItem($oObserver) {
$oQuoteItem = $oObserver->getQuoteItem();
$quoteId = $oQuoteItem->getItemId();
}
Upvotes: 1