Watching Add to Cart Event - quote item id is empty
-
16-10-2019 - |
문제
I am trying to catch the event that fires once an item is added to the cart. I'm currently watching the following event: checkout_cart_product_add_after
According to magento source this event is fired after everything is done to the Quote. but when i access the cart id and the quote id the values are empty:
$quoteItem = $observer->getQuoteItem();
$quote_item_id = $quoteItem->getItemId();
$cart = Mage::getSingleton('checkout/session');
$quote_id= $cart->getQuoteId();
The above returns empty for both ids when there are no items in the cart, if the cart already has an item the cart id has value but the quote_item_id doesn't.
Notice this has been asked before, but the question was never resolved, and the discussion ended up straying from this issue. I need the quote_item_id.
해결책
Don't do this.
Your problem is, that the cart is not yet saved, have a look here:
public function addAction()
{
// ...
$cart->addProduct($product, $params); // <-- you are inside this method
if (!empty($related)) {
$cart->addProductsByIds(explode(',', $related));
}
$cart->save(); // here is the saving, and therefore after this line,
// quote and items have an id
// ...
Mage::dispatchEvent('checkout_cart_add_product_complete',
array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
);
What you want is to listen on checkout_cart_add_product_complete
If you want to know which items where added this round, just flag them in checkout_cart_product_add_after
like $quoteItem->setIsNew()
then you can check in checkout_cart_add_product_complete
for $quoteItem->getIsNew()
다른 팁
You can use the checkout_cart_product_add_after
event with this:
$observer->getEvent()->getQuoteItem()->getProduct()->getData()
The data returned looks similar to this:
Array
(
[store_id] => 1
[entity_id] => 1
[entity_type_id] => 4
[attribute_set_id] => 4
[type_id] => simple
[sku] => TESTSKU
[has_options] => 0
[required_options] => 0
[created_at] => 2015-02-10T12:11:50-05:00
[updated_at] => 2015-02-10 17:18:47
[name] => Demo Product
[url_key] => demo-product
[country_of_manufacture] =>
[msrp_enabled] => 2
[msrp_display_actual_price_type] => 4
[meta_title] =>
[meta_description] =>
[image] => no_selection
[small_image] => no_selection
[thumbnail] => no_selection
[custom_design] =>
[page_layout] =>
[options_container] => container1
[gift_message_available] =>
[url_path] => demo-product.html
[weight] => 1.0000
[price] => 12.9900
[special_price] =>
[msrp] =>
[status] => 1
[visibility] => 4
[tax_class_id] => 2
[is_recurring] => 0
[description] => It's a sample product, what do you want?
[short_description] => It's a demo product
[meta_keyword] =>
[custom_layout_update] =>
[news_from_date] =>
[news_to_date] =>
[special_from_date] =>
[special_to_date] =>
[custom_design_from] =>
[custom_design_to] =>
[group_price] => Array
(
)
[group_price_changed] => 0
[media_gallery] => Array
(
[images] => Array
(
)
[values] => Array
(
)
)
[tier_price] => Array
(
)
[tier_price_changed] => 0
[stock_item] => Mage_CatalogInventory_Model_Stock_Item Object
(
// Crazy recursion happens here
)
[is_in_stock] => 1
[is_salable] => 1
[website_ids] => Array
(
[0] => 1
)
[cart_qty] => 1
[qty] => 1
[stick_within_parent] =>
[customer_group_id] => 0
[final_price] =>
)
This was tested on Magento 1.9.1.0, but from what I can tell, this should work on 1.7
you can use following event
sales_quote_item_set_product
and get item id in observer like this.
$quote_item = $observer->getEvent()->getQuoteItem();
$item_id = $quote_item->getItemId();
I solved this issue by calling save on $cart and on quoteItem. Not selecting this as correct since i'm not sure this is the best method.
Fabian Blechschmidt solution is much better, use that one.