I wanna put customize the code with qty while adding to cart product. For that I have created Plugin Magento\Quote\Model\Quote\Item and used addQty() method,but in this method I call quoteItem object. Below code working fine for simple product,but for configurable product it is not working.

Below I have created two files to customize code. I have created one plugin. Here is di.xml file code

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Quote\Model\Quote\Item">
        <plugin name="test_customizationquote_item" type="Test\CustomizationQuote\Plugin\Model\Quote\Item" sortOrder="10"/>


At front store, when I select color of configurable product and click to add to cart button. It comes in addQty() method with requested qty. In this function I call quoteItem object,but this quoteItem object get parent item object or data rather than get child item. My goal is when before add to cart item, I need to check quantity of item,but quoteItem object fetch parent item object so I am unable to fetch child item stock qty. So How can I get child product object in addQty() function.

I have written addQty() function in below code. please check it

Then Here is my plugin code Item.php


namespace Test\CustomizationQuote\Plugin\Model\Quote;

use Magento\Framework\Registry;
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\Quote\Model\Quote\Item as QuoteItem;
use Magento\CatalogInventory\Model\Stock;

class Item
     * @var Registry
    private $registry;

     * @var StockRegistryInterface
    private $stockRegistry;

     * @param Registry $registry
     * @param StockRegistryInterface $stockRegistry
    public function __construct(
        Registry $registry,
        StockRegistryInterface $stockRegistry
    ) {
        $this->registry = $registry;
        $this->stockRegistry = $stockRegistry;

    public function beforeAddQty(QuoteItem $quoteItem, $qty)
        $product = $quoteItem->getProduct(); // here I am getting configurable product object instead of child product object
        $stockItem = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId());

        return [$qty];
  • Try printing out $quoteItem to see if there are any product options there. For configurable products , Magento adds option codes (i-e color and size) along with parentId, to cart. If you can get selected options, hopefully you'll be able to get corresponding child product based on those option values. Jun 4, 2021 at 9:38

1 Answer 1


I suggest you try this:

$stockItem = $this->stockRegistry->getStockItem($product->getSku(), $product->getStore()->getWebsiteId());

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.