问题
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();
}
回答1:
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..
回答2:
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());
}
回答3:
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();
回答4:
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();
}
来源:https://stackoverflow.com/questions/24113640/how-to-get-quote-item-id-after-adding-a-product-to-cart