I'd like to display the childProduct saleable stock quantity within a grouped or configurable product page on magento 2, I can put in various blocks that supposedly call the stockQty but it's always the stock of the config or grouped (i.e., 0).

edit: this solution: Display Stock Quantity on grouped product

doesn't work, just shows a no such entity exception for an entiry_id that does exist.


This will get you there of what you've wants, add the below code in the block file

<?php namespace Vendor\Module\Block;
use Magento\Catalog\Api\ProductRepositoryInterface;                 
use Magento\ConfigurableProduct\Api\LinkManagementInterface;        
use Magento\Framework\Api\SearchCriteriaBuilder;                    
use Magento\Framework\Registry;                                   
class Product extends \Magento\Framework\View\Element\Template{
public $registry;
public $stockProduct;
protected $productRepository;
protected $linkManagement;

public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\Registry $registry,
\Magento\CatalogInventory\Api\StockStateInterface $stockProduct,
LinkManagementInterface $linkManagement,
ProductRepositoryInterface $productRepository,
SearchCriteriaBuilder $searchCriteriaBuilder,
array $data = []
) {
    $this->registry      = $registry;
    $this->stockProduct  = $stockProduct;
    $this->linkManagement = $linkManagement;
    $this->productRepository = $productRepository;
    $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    parent::__construct($context, $data);

public function getCurrentProduct()
    return $this->registry->registry('current_product')->getId();
public function getStock()
    $searchCriteria = $this->searchCriteriaBuilder
        ->addFilter('type_id', 'configurable')
        ->addFilter('entity_id', $this->getCurrentProduct())

    $configurableProducts = $this->productRepository
    $simpleStock      = array();
    foreach ($configurableProducts->getItems() as $configurableProduct) {
        $childProducts = $this->linkManagement
        foreach ($childProducts as $items) {
            $simpleStock[] = $this->stockProduct->getStockQty($items->getId(), $items->getStore()->getWebsiteId());
    return $simpleStock;


Then add the

catalog_product_view.xml add the below to there in layout from you module

<?xml version="1.0"?>                                             
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
   <referenceContainer name="content">
        <block class="Vendor\Module\Block\Product"  name="customer_section" template="Vendor_Module::product.phtml">

Add the below to the product.phtml

$blockobj = $block->getStock();

That's all hope it helps you. Have a nice day :)

  • Hi, yes i've seen these objectmanager options everywhere, but they are all immediately followed by "don't use this it's not the right way to do it" so what would be the point of trying it out? – Ian Richens Feb 11 at 13:21
  • @IanRichens the purpose of using object manager to achieve the task. Anyway updated. – Prathap Gunasekaran Feb 11 at 13:39
  • so if i put this in a product.php in my new module, i would call this with <?php $blockObj= $block->getLayout()->createBlock('Vendor\module\Block\Product'); echo $blockObj->getStock(); ?> – Ian Richens Feb 11 at 14:34

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.