11

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.

6
  • try with $quoteItem = $observer->getEvent()->getQuoteItem();
    – Marius
    Nov 11, 2013 at 15:46
  • Same thing, the id comes empty. Nov 11, 2013 at 15:58
  • Add this in your observer Mage::log($quoteItem) and see in var/log/system.log how the quote item looks like. Maybe you get an idea from there.
    – Marius
    Nov 11, 2013 at 15:59
  • that is a huge amount of information, i'll easily get lost in it Nov 11, 2013 at 16:08
  • 1
    =D still too big, i tried Mage::getLog($quoteItem->debug()) and i notice that there is no itemid in the resulting array. There seems to be an item_id in the stock_item element inside the quoteitem but i'm not even sure what that is. Edit: this isn't it this value is allways the same Nov 11, 2013 at 16:14

4 Answers 4

22

Don't do this.

Your problem is, that the cart is not yet saved, have a look here:

https://github.com/LokeyCoding/magento-mirror/blob/magento-1.7/app/code/core/Mage/Checkout/controllers/CartController.php#L201-L206

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()

4
  • I figured that would be it, problem is that event only sends the product out. Is it a good idea to get the cart from session and get the last of the allitems array? i worry that there might be some kind of race condition Nov 12, 2013 at 8:20
  • 1
    Edited the answer. You need to flag your quote items, then let magento save them, then you know your quote item and the product :) Nov 12, 2013 at 10:08
  • @FabianBlechschmidt is there an event to be triggered while updating the cart?
    – Anto S
    Jul 11, 2016 at 5:59
  • yes, many, you can just hook into the dispatchEvent and check it: magento.stackexchange.com/a/9155/217 And make sure to read the comment from ben! Jul 11, 2016 at 6:02
5

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

2

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();
1
  • This event is called for every quote item every time the cart is built, not only when an item is added to the cart.
    – Rooster242
    May 3, 2017 at 17:49
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.