How to get Quote Item Id after adding a product to

2019-07-18 06:38发布

I want quote item id every time a product added to cart. I have tried many event they return the quote item object but object doesn't contain quote item id as it exists only when cart save to the db. So is there any event which will return the quote item object with quote item id?

I have used following events

checkout_cart_product_add_after
sales_quote_add_item

but it will not return quote_item_id in

public function addItemToSalesModelInfo(Varien_Event_Observer $observer){
    $item = $observer->getEvent()->getQuoteItem();
}

4条回答
The star\"
2楼-- · 2019-07-18 06:44

Get quote and items count after adding product to cart.

//Save your cart
$cart->save();

//use checkout/session
$cart1=Mage::getSingleton('checkout/session');
$cart1->setCartWasUpdated(true);

// Quote ID
$result_array["quoteid"]=$quoteid=$cart1->getQuoteId();

// Items count
$result_array["items_count"]=Mage::helper('checkout/cart')->getCart()->getItemsCount();
查看更多
The star\"
3楼-- · 2019-07-18 06:46

The item id is only available checkout_cart_product_add_after if a fitting item already existed in cart. You will actually have to work in two steps. Listen to checkout_cart_product_add_after and add a flag to the item.

$item = $observer->getQuoteItem();
$item->setLastAdded(true);

And then listen to sales_quote_item_save_after and add the id to the session (or do whatever) if the flag is set.

$item = $observer->getItem();
if ($item->getLastAdded()) {
    Mage::getSingleton('checkout/session')->setLastAddedItemId($item->getId());
}
查看更多
Melony?
4楼-- · 2019-07-18 06:59

You are using a correct event i.e.

checkout_cart_product_add_after

to access quote item id, you need to use,

$item = $observer->getQuoteItem()

you can access all quote item info like

$item->getProductId() etc..

查看更多
Fickle 薄情
5楼-- · 2019-07-18 07:06

To get quote id and quote's item id, you can get using below code.

$currentcart = Mage::getModel('checkout/cart');
$quote = $currentcart->getQuote();
$quoteItemId = $quote->getEntityId();

To get quote's item id

$quoteItems = $quote->getAllVisibleItems();
foreach ($quoteItems as $item) {
$ItemId = $item->getId();
}
查看更多
登录 后发表回答