Magento 2.4.6

I am listening to the checkout_cart_product_add_after event. In my observer I am trying to get an attribute from the added child product. I did not manage so far, all data that I get is from the configurable, but not from the added simple / child product.

In my observer I have:

$item = $observer->getEvent()->getData('quote_item');

Now, when I do something like $item->getId(), I am always getting the configurable id, not the id of the child which was added. Any ideas how to achieve this?


1 Answer 1


To get child product data for configurable products within the "checkout_cart_product_add_after" event, you can use the following approach.

Firstly create the events.xml file in your module

<?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_product_add_after">
        <observer name="vendor_module_custom_observer" instance="Vendor\Module\Observer\CustomObserver" />

After then create an Observer and then add the following code in it.

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Response\RedirectInterface;
use Magento\Checkout\Model\Cart;
use Magento\Framework\App\RequestInterface;
use Magento\Catalog\Model\Product;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
class CustomObserver implements ObserverInterface
    protected $cart;
    protected $redirect;
    protected $request;
    protected $product;
    protected $configurableproduct;
    public function __construct(RedirectInterface $redirect, Cart $cart, RequestInterface $request, Product $product, Configurable $configurableproduct)
        $this->redirect = $redirect;
        $this->cart = $cart;
        $this->request = $request;
        $this->product = $product;
        $this->configurableproduct = $configurableproduct;
    public function execute(\Magento\Framework\Event\Observer $observer)
        $postValues = $this->request->getPostValue();
        $productId = $postValues['product'];
        $addProduct = $this->product->load($productId);
        if ($addProduct->getTypeId() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) 
            $attributes = $postValues['super_attribute'];
            $simple_product = $this->configurableproduct->getProductByAttributes($attributes, $addProduct);

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.