1

I am working on a custom module where a single product of a certain sku (say fixed-product) will be added to cart multiple times, but with different custom options programmatically. I also need to store it's item_id in another custom table for later use.

I managed to display the different products as separate entities on the cart (instead of just adding the quantity) by rewriting the Codilar\ProfServices\Model\Rewrite\CartItem::representProduct() method to return false if the conditions are met.

But I cannot obtain the item_id of the item once it's added to cart. I tried the following events

checkout_cart_product_add_after and sales_quote_item_set_product

but in the observer when I do $this->getEvent()->getQuoteItem()->getItemId() it always returns NULL

How do I get the item_id of an item added to cart in the observer?

3

For checkout_cart_add_product_complete event

<?php
namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
use Magento\Checkout\Model\Session as CheckoutSession;

class CheckoutCartAddProductCompleteObserver implements ObserverInterface
{
    private $_checkoutSession;

    public function __construct(
        CheckoutSession $checkoutSession
    )
    {
        $this->_checkoutSession = $checkoutSession;
    }

    public function execute(EventObserver $observer)
    {
        /** @var \Magento\Catalog\Model\Product $product */
        $product = $observer->getEvent()->getDataByKey('product');

        /** @var \Magento\Quote\Model\Quote\Item $item */
        $item = $this->_checkoutSession->getQuote()->getItemByProduct($product);

        $itemId = $item->getId();
    }
}
2

Create events.xml at

app/code/Vendor/Module/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

    <event name="checkout_cart_add_product_complete">
        <observer name="get_quote" instance="Vendor\Module\Observer\Getquote" />
    </event>

</config>

Create Getquote.php at

app/code/Vendor/Module/Observer/Getquote.php

<?php

namespace Vendor\Module\Observer;

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

class Getquote implements ObserverInterface
{
    protected $_checkoutSession;

    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession
    )
    {   
        $this->_checkoutSession = $checkoutSession;
    }

    public function execute(Observer $observer)
    {
        $quoteItems = $this->_checkoutSession->getQuote()->getAllVisibleItems();

    }
}
0

You can access the Quote object itself if you do a $observer->getEvent()->getQuote() right?

If so, you may try the following:

$items = $observer->getEvent()->getQuote()->getAllVisibleItems(); // get only visible items
$maxId = 0;
foreach ($items as $item){
  if ($item->getId() > $maxId) {
     $maxId = $item->getId();
  }
}
$lastItemId = $maxId;

This should get you the last item ID that was added in, for instance, checkout_cart_product_add_after.

But it may be that the cart is not yet saved at checkout_cart_product_add_after (then your last added item will not yet have an item id) so maybe you should check if there is a checkout_cart_add_product_complete. That one should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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