Magento 2.2.3 – How to Get and Display Stock Quantity on list.phtml

magento2magento2.2.3quantitystock

I have been trying to display the stock quantity of each item on the "list.phtml" page but not getting anywhere.

Please see the attached photos as an example; this is what I have been trying to achieve. If could someone gives me the step by step guide would be helpful.

enter image description here

Best Answer

If you want to show qty of the product, You can used this code.

$product_Id = 'YOUR_PRODUCT_ID';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$stockInfo = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface')->getStockItem($product_Id);
$stockqty = (int)$stockInfo->getQty();
echo $stockqty;
Related Topic