get quote item id after adding product to cart

get quote item id after adding product to cart

August 21, 2026 · Illustrated author avatarBy Stefan Bogdanescu
get quote item id after adding product to cart

Getting Quote Item ID After Adding Product to Cart

When you add a product to your cart in Magento, you might need to update a database field associated with that item. To do this, you’ll need the quote item ID. Let’s explore how to correctly retrieve this value using an event observer.

Understanding the Problem

You’ve attempted to use the sales_quote_product_add_after event and created an observer class. However, when trying to access $quoteItem->getId() or $quoteItem->getItemId(), you’re encountering a fatal error: “Call to a member function getId() on a non-object.”

The issue lies in how the event data is structured by Magento.

The Correct Approach

Firstly, ensure your observer class is correctly registered. In your etc/events.xml file, define your observer like this:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_quote_product_add_after">
        <observer name="custom_module_sales_quote_product_add_after" instance="Custom\Module\Model\Observer"/>
    </event>
</config>

Now, let’s update your Observer.php file:

namespace Custom\Module\Model;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;

class Observer implements ObserverInterface
{
    public function execute(EventObserver $observer)
    {
        $quoteItem = $observer->getData('quote_item');

        if ($quoteItem instanceof \Magento\Quote\Model\Quote\Item) {
            $id = $quoteItem->getId();
            // Your update logic here using the ID
        }
    }
}

Here’s what we’ve done:

  1. We’re getting the quote_item data directly from the observer.
  2. We’re checking if it is an instance of \Magento\Quote\Model\Quote\Item. This ensures you have a valid object before calling methods on it.

Debugging Tip

To better understand why your original approach wasn’t working, log the $observer->getEvent()->getQuoteItem() value:

namespace Custom\Module\Model;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;

class Observer implements ObserverInterface
{
    public function execute(EventObserver $observer)
    {
        $quoteItem = $observer->getEvent()->getQuoteItem();
        var_dump($quoteItem);  // Add this line

        if ($quoteItem instanceof \Magento\Quote\Model\Quote\Item) {
            $id = $quoteItem->getId();
            // Your update logic here using the ID
        }
    }
}

When you check your log, you’ll see that $quoteItem is not an object in your original scenario.

Conclusion

Retrieving the quote item ID correctly requires careful handling of event data. By ensuring we have a valid Quote\Item object before calling methods on it, we can avoid fatal errors and successfully update our database fields.

Remember to replace the placeholder comments with your actual update logic.

For more advanced use cases or if you’re facing further challenges with Magento events, feel free to explore other resources like the official Magento Developer Documentation or reach out to an expert at Laravel Company.

Checkout Catalog Database Extensions

Our network