0

I need to display available quantity on product list and details page and other pages where product shows.

I extended product list class as below

<?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\Catalog\Block\Product\ListProduct" type="Tm\Stockinfo\Block\Product\Stockqty" />
</config>

And here i write code in extended block

<?php

 namespace Tm\Stockinfo\Block\Product;


class Stockqty extends \Magento\Catalog\Block\Product\ListProduct {

    protected  $_stockState;

        public function __construct(
            \Magento\Framework\View\Element\Template\Context $context,
            \Magento\CatalogInventory\Api\StockStateInterface $stockState,
            array $data = []

        )
        {
            parent::__construct($context, $data);
            $this->_stockState = $stockState;
        }




    public function getStockQty($product) {


       return $this->stockState->getStockQty($product->getId(), $product->getStore()->getWebsiteId());
    }

}

Then i am getting below error

Fatal error: Uncaught TypeError: Argument 2 passed to Tm\Stockinfo\Block\Product\Stockqty::__construct() must implement interface Magento\CatalogInventory\Api\StockStateInterface, array given, called in E:\xampp\htdocs\amit\mage222\generated\code\Tm\Stockinfo\Block\Product\Stockqty\Interceptor.php on line 14 and defined in E:\xampp\htdocs\amit\mage222\app\code\Tm\Stockinfo\Block\Product\Stockqty.php:10 Stack trace: #0 E:\xampp\htdocs\amit\mage222\generated\code\Tm\Stockinfo\Block\Product\Stockqty\Interceptor.php(14): Tm\Stockinfo\Block\Product\Stockqty->__construct(Object(Magento\Catalog\Block\Product\Context), Array) #1 E:\xampp\htdocs\amit\mage222\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php(111): Tm\Stockinfo\Block\Product\Stockqty\Interceptor->__construct(Object(Magento\Catalog\Block\Product\Context), Array) #2 E:\xampp\htdocs\amit\mage222\vendor\magento\framework\ObjectManager\Factory\Dynamic\Developer.php(66): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('Tm\Stockinfo\ in E:\xampp\htdocs\amit\mage222\app\code\Tm\Stockinfo\Block\Product\Stockqty.php on line 10

=========================== Edit question ===================================

And here layout xml "\app\code\Tm\Stockinfo\view\frontend\layout\catalog_product_view.xml"

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page layout="2columns-right" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
       <referenceContainer name="product.info.price">
            <container name="quantity_show" htmlTag="div" htmlClass="show_quantity" after="product.price.final">
               <block class="Tm\Stockinfo\Block\Product\Stockqty" name="stockinfo_showstock" template="Tm_Stockinfo::stockinfo_index_index.phtml"/>
            </container>
        </referenceContainer>
    </body>
</page>

I am trying to inject "\Magento\CatalogInventory\Api\StockStateInterface" class but due to listproduct construct method it gives error. I know i can use same construct Listproduct block but it is not good to go in this way.

2 Answers 2

2

You have to use the StockRegistryInterface, Also you have to see the extended class and pass all needed parameters to the parent constructor, in your class you extended this class

=>\Magento\Catalog\Block\Product\ListProduct

So you have to pass his parameters,

Try this code.

 <?php
namespace Tm\Stockinfo\Block\Product;

class Stockqty extends \Magento\Catalog\Block\Product\ListProduct
{

    protected $_stockRegistry;

    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
        \Magento\Catalog\Model\Layer\Resolver $layerResolver,
        \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
        \Magento\Framework\Url\Helper\Data $urlHelper,
        \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
        array $data = [])
    {
            parent::__construct(
                $context,
                $postDataHelper,
                $layerResolver,
                $categoryRepository,
                $urlHelper,
                $data
                );
        $this->_stockRegistry = $stockRegistry;
    }

    public function getStockQty(
        $product)
    {
        $stockitem = $this->_stockRegistry->getStockItem($product->getId(),
            $product->getStore()
                ->getWebsiteId());
        $qtyStock = $stockitem->getData("qty");
        return $qtyStock;
    }
}

Please note that this code working on Magento 2.2.3, So you need to check the parent class in your version

0
1

You can use StockItemRepository instead

<?php
namespace Tm\Stockinfo\Block\Product;
class Stockqty extends \Magento\Framework\View\Element\Template
{    
    protected $_stockItemRepository;

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

    public function getStockItem($productId)
    {
        return $this->_stockItemRepository->get($productId);
    }
}
?>

and call on template like this :

$_productStk = $block->getStockItem($_product->getId());

echo $_productStk->getQty();
echo $_productStk->getIsInStock();

Or you can use this directly in template ( not recommended but just in case )

<?php 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface');
    echo $StockState->getStockQty($product->getId(), $product->getStore()->getWebsiteId());
?>
3
  • Thanks Naveed, Please check my editable section. how can i load product or get product id. I am using "Tm_Stockinfo::stockinfo_index_index.phtml" Mar 30, 2018 at 19:25
  • could you share your template code ? Mar 31, 2018 at 4:11
  • Hi naveed, apologize, I add nothing in template. it is just a blank file. That would be next task for me Mar 31, 2018 at 6:35

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.