2

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.

2

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);
    }
}
  • whats this $stockItem->getQty() getIsInStock() – Daniel_12 Jul 2 '18 at 11:54
  • updated check now. – Sohel Rana Jul 2 '18 at 11:56
  • instead of plugin can we use it in ViewMode – Daniel_12 Jul 2 '18 at 12:06
  • @SohelRana, Is there any way where we can get configurable product collection with child product qty? – Pushpendra Singh Jul 20 at 9:12

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.