Question

After get qty its show zero always Magento2

enter image description here

Was it helpful?

Solution

Try below way,

echo $_product->getExtensionAttributes()->getStockItem()->getQty();

To get product qty in list page, for testing you can use objectmanager after finished testing create this method in helper/block and call that helper/block file from phtml(recommended).

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

OTHER TIPS

Try below code - in your phtml file

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface');
echo $StockState->getStockQty($product->getId(), $product->getStore()->getWebsiteId());
?>

** For coding standard keep objectManager in helper class.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top