I am trying to show out of stock child products for configurable product in details and product view.I changed the settings from catalog inventory to display out of stock products even it is not showing. I tried below code but not working.


namespace Vendor\Firstmodule\Helper;

use Magento\Catalog\Model\Product;

class Data extends \Magento\ConfigurableProduct\Helper\Data

    protected $imageHelper;
    protected $_productloader;  
    protected $stockRegistry;  
    protected $_storeManager;

    public function __construct(
        \Magento\Catalog\Helper\Image $imageHelper,
        \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
        \Magento\Catalog\Model\ProductFactory $_productloader,
        \Magento\Store\Model\StoreManagerInterface $storeManager
        $this->imageHelper = $imageHelper;
        $this->stockRegistry = $stockRegistry;
        $this->_productloader = $_productloader;
        $this->_storeManager = $storeManager;

     public function getOptions($currentProduct, $allowedProducts)
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
       // $stockRegistry = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface');
        $options = [];
        foreach ($allowedProducts as $product) {
            $productId = $product->getId();
            $product = $objectManager->get('Magento\Catalog\Model\Product')->load($productId);
            $stockitem = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId());
            if($stockitem->getQty() == 0) continue;
            $images = $this->getGalleryImages($product);
            if ($images) {
                foreach ($images as $image) {
                    $options['images'][$productId][] =
                            'thumb' => $image->getData('small_image_url'),
                            'img' => $image->getData('medium_image_url'),
                            'full' => $image->getData('large_image_url'),
                            'caption' => $image->getLabel(),
                            'position' => $image->getPosition(),
                            'isMain' => $image->getFile() == $product->getImage(),
            foreach ($this->getAllowAttributes($currentProduct) as $attribute) {
                $productAttribute = $attribute->getProductAttribute();
                $productAttributeId = $productAttribute->getId();
                $attributeValue = $product->getData($productAttribute->getAttributeCode());
                $options[$productAttributeId][$attributeValue][] = $productId;
                $options['index'][$productId][$productAttributeId] = $attributeValue;
        return $options;



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

    <preference for="Magento\ConfigurableProduct\Helper\Data" type="Vendor\Firstmodule\Helper\Data" />


Try to change this line

$stockitem = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId());


$stockitem = $this->stockRegistry->get($product->getId(), $product->getStore()->getWebsiteId()); echo 'Stock : '.$stockitem->getIsInStock();

$_productStock->getIsInStock() return 0 or 1 value
based on this put lable 'in-stock' or 'out-stock'

use this repository


Instead of


  • hi @snkt,it is not working even it is giving some error – Hitesh Balpande Nov 30 '18 at 13:37
  • $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $StockState = $objectManager->get('\Magento\CatalogInventory\Model\Stock\StockItemRepository'); $_productStock = $StockState->get($_product->getId(), $_product->getStore()->getWebsiteId()); $_productStock->getIsInStock() this code working for me in list.phtml – snkt Nov 30 '18 at 13:42
  • i tried in list.phtml also but not working.but for me out of stock child products data not coming using \Magento\CatalogInventory\Model\Stock\StockItemRepository i tried with \Magento\CatalogInventory\Api\StockRegistryInterface this one is returning only instock ie 1. can you tell me please this code will read child products stock in list.phtml file. – Hitesh Balpande Nov 30 '18 at 14:18
  • Yes,this solution working for configurable and it's child product.i'm use magento 2.2.5 – snkt Dec 1 '18 at 4:50

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.