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.