2

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; ?>
17
  • please show your code, which you have tried. Jan 10, 2017 at 9:05
  • @Rakesh Sir , Code is of default magento 2.1.2 : Root/vendor/magento/module-grouped-product/view/frontend/tem‌​plates/product/view/‌​type/grouped.phtml and Updated question with code.
    – Jarnail S
    Jan 11, 2017 at 12:02
  • @Jai : is it know about discount for grouped products,
    – Gem
    Jan 11, 2017 at 12:06
  • @JeevaRathinam Please elaborate
    – Jarnail S
    Jan 11, 2017 at 12:29
  • @Jai : magento.stackexchange.com/questions/152608/…
    – Gem
    Jan 11, 2017 at 12:30

1 Answer 1

1

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; 
?>
1
  • This is incorrect. public function get($stockItemId) - This returns the id in the stock table, but not the product_id
    – elfling
    Feb 8, 2019 at 14:42

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.