Magento – How to get stock quantity of configurable product in Magento 2

configurable-productmagento2stock

I am able to get all the available sizes of configurable product calling

getJsonConfig()

of below class

Magento\ConfigurableProduct\Block\Product\View\Type\Configurable;

But I need stock of each options.

Best Answer

Try following way:

app/code/SR/Stackexchange/etc/frontend/di.xml


<?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\ConfigurableProduct\Block\Product\View\Type\Configurable">
        <plugin name="sr_configurable_product_configurable" type="SR\Stackexchange\Plugin\Block\ConfigurableProduct\Product\View\Type\Configurable" sortOrder="1"/>
    </type>
</config>

app/code/SR/Stackexchange/Plugin/Block/ConfigurableProduct/Product/View/Type/Configurable.php


<?php
namespace SR\Stackexchange\Plugin\Block\ConfigurableProduct\Product\View\Type;

use Magento\Framework\Json\EncoderInterface;
use Magento\Framework\Json\DecoderInterface;

class Configurable
{
    /**
     * @var EncoderInterface
     */
    protected $jsonEncoder;

    /**
     * @var DecoderInterface
     */
    protected $jsonDecoder;

    /**
     * @var \Magento\CatalogInventory\Api\StockRegistryInterface
     */
    protected $stockRegistry;

    /**
     * Configurable constructor.
     *
     * @param EncoderInterface $jsonEncoder
     * @param DecoderInterface $jsonDecoder
     * @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
     */
    public function __construct(
        EncoderInterface $jsonEncoder,
        DecoderInterface $jsonDecoder,
        \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
    ) {
        $this->jsonDecoder = $jsonDecoder;
        $this->jsonEncoder = $jsonEncoder;;
        $this->stockRegistry = $stockRegistry;
    }

    public function aroundGetJsonConfig(
        \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable $subject,
        \Closure $proceed
    ) {
        $config = $proceed();
        $config = $this->jsonDecoder->decode($config);
        $productsCollection = $subject->getAllowProducts();
        $stockInfo = array();
        foreach ($productsCollection as $product) {
            $productId = $product->getId();
            $stockItem = $this->stockRegistry->getStockItem($product->getId());
            if ($stockItem->getQty() <= 0 || !($stockItem->getIsInStock())) {
                $stockInfo[$productId] = array(
                    "stockLabel" => __('Out of stock'),
                    "stockQty" => intval($stockItem->getQty()),
                    "is_in_stock" => false
                );
            } else {
                $stockInfo[$productId] = array(
                    "stockLabel" => __('In Stock'),
                    "stockQty" => intval($stockItem->getQty()),
                    "is_in_stock" => true
                );
            }
        }

        $config['stockInfo'] = $stockInfo;
        return $this->jsonEncoder->encode($config);
    }
}
Related Topic