Magento 2 – How to Display Stock Quantity on Grouped Product

magento-2.1magento2quantity

I need to display Stock Quantity on grouped products.

Grouped product detail are displaying by grouped.phtml

(Core module: module-grouped-product/view/frontend/templates/product/view/type/grouped.phtml).

I have extended this module and i need to display Quantity there.

Update: This is foreach loop that display products and I need to display Stock qty in for-each loop.

I am not sure If i can display from function reference of stock.

Line 34 – 82:

<?php foreach ($_associatedProducts as $_item): ?>
    <tbody>
        <tr>
            <td data-th="<?php echo $block->escapeHtml(__('Product Name')); ?>" class="col item">
                <strong class="product-item-name"><?php echo $block->escapeHtml($_item->getName()) ?></strong>
                <?php if ($block->getCanShowProductPrice($_product)): ?>
                    <?php if ($block->getCanShowProductPrice($_item)): ?>
                        <?php /* @escapeNotVerified */ echo $block->getProductPrice($_item) ?>
                    <?php endif; ?>
                 <?php endif; ?>
            </td>
            <?php if ($_product->isSaleable()): ?>
            <td data-th="<?php echo $block->escapeHtml(__('Qty')); ?>" class="col qty">
            <?php if ($_item->isSaleable()) : ?>
                <div class="control qty">
                    <input type="number" name="super_group[<?php /* @escapeNotVerified */ echo $_item->getId() ?>]"
                           data-selector="super_group[<?php /* @escapeNotVerified */ echo $_item->getId() ?>]"
                           maxlength="12"
                           value="<?php /* @escapeNotVerified */ echo $_item->getQty() * 1 ?>"
                           title="<?php /* @escapeNotVerified */ echo __('Qty') ?>"
                           class="input-text qty"
                           data-validate="{'validate-grouped-qty':'#super-product-table'}"
                           data-errors-message-box="#validation-message-box"/>
                </div>
            <?php else: ?>
                <div class="stock unavailable" title="<?php /* @escapeNotVerified */ echo __('Availability') ?>">
                    <span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span>
                </div>
            <?php endif; ?>
            </td>
            <?php endif; ?>
        </tr>
        <?php if ($block->getCanShowProductPrice($_product)
            && $block->getCanShowProductPrice($_item)
            && trim($block->getProductPriceHtml(
                $_item,
                \Magento\Catalog\Pricing\Price\TierPrice::PRICE_CODE
            ))): ?>
            <tr class="row-tier-price">
                <td colspan="2">
                    <?php echo $block->getProductPriceHtml(
                        $_item,
                        \Magento\Catalog\Pricing\Price\TierPrice::PRICE_CODE
                    ) ?>
                </td>
            </tr>
        <?php endif; ?>
    </tbody>
    <?php endforeach; ?>

Best Answer

You have to just create block file keep below code in block file,

app/code/Vendor/Extra/Block/Grouped.php file,

<?php
namespace Vendor\Extra\Block;

use Magento\Catalog\Helper\Data;

class Grouped extends \Magento\Framework\View\Element\Template
{
    protected $productRepository;

    public function __construct(        
        \Magento\Framework\View\Element\Template\Context $context,          
        \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository,
        array $data = [] 
    ){      
        $this->_stockItemRepository = $stockItemRepository;
        parent::__construct($context,$data);    
    }

    public function getProductQty($id){
        if(is_numeric($id)){           
            $product = $this->_stockItemRepository->get($id);            
            return $product->getQty();
        }else{
            return;
        }
    }

}

app/design/frontend/{Vendor}/{themename}/Magento_GroupedProduct/templates/product/view/type/grouped.phtml

Inside foreach loop in above file,

<?php 
    $blocks = $this->getLayout()->createBlock('Vendor\Extra\Block\Grouped');
    foreach ($_associatedProducts as $_item):
        echo $qtys = $blocks->getProductQty($_item->getId());
    endforeach; 
?>
Related Topic