How to get stock quantity of each product in Magento 2
Solution:1
Create Helper file Stock.php in your module
<?php
namespace {VendorName}\{ModuleName}\Helper;
class Stock extends \Magento\Framework\App\Helper\AbstractHelper
{
/**
* @var Magento\CatalogInventory\Api\StockStateInterface
*/
protected $stockState;
/**
* Output constructor.
* @param \Magento\Framework\App\Helper\Context $context
* @param \Magento\CatalogInventory\Api\StockStateInterface $stockState
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\CatalogInventory\Api\StockStateInterface $stockState
) {
$this->stockState = $stockState;
parent::__construct($context);
}
/**
* Retrieve stock qty whether product
*
* @param int $productId
* @param int $websiteId
* @return float
*/
public function getStockQty($productId, $websiteId = null)
{
return $this->stockState->getStockQty($productId, $websiteId);
}
}
After add bellow code in your list.phtml file
$websiteId = 1; // Current websiteId
$productId = 2; // $_product->getId() Product Id
$_helperStock = $this->helper({VendorName}\{ModuleName}\Helper\Stock::class);
echo $_helperStock->getStockQty($productId, $websiteId);
Solution:2
Add bellow code in your list.phtml
file
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface');
echo $StockState->getStockQty($product->getId(), $product->getStore()->getWebsiteId());
?>
OR
<?php
$stockItem = $product->getExtensionAttributes()->getStockItem();
print_r($stockItem->getQty());
?>
Like some comments have mentioned, you want to use dependency injection. Do not use the object manager; in other words, do not do what any of the other responses state. The following technique can be applied anywhere. For Blocks, set the class to your class in layout XML, which extends the original, and inject the right information.
Inject the StockRegistryInterface
interface where you need access:
/**
* @var \Magento\CatalogInventory\Api\StockRegistryInterface
*/
private $stockRegistry;
/**
* Constructor for DI.
*
* @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
*/
public function __construct(
\Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
) {
$this->stockRegistry = $stockRegistry;
}
/**
* Get the product stock data and methods.
*
* @return \Magento\CatalogInventory\Api\StockRegistryInterface
*/
public function getStockRegistry()
{
return $this->stockRegistry;
}
To use it somewhere:
/** @var \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry */
$stockRegistry = [$this|$block]->getStockRegistry();
/** @var \Magento\Catalog\Model\Product $product */
$product = [Grab Product instance however you want. This is up to you.]
// Get stock data for given product.
$productStock = $stockRegistry->getStockItem($product->getId());
// Get quantity of product.
$productQty = $productStock->getQty();
For reference, Magento2 uses this exact interface all over the catalog when it comes to retrieving product stock information.
Note that anything within square brackets needs to be modified.
How to get stock quantity of each product in Magento 2
for controller or block inject \Magento\CatalogInventory\Api\StockStateInterface
public function __construct(
\Magento\CatalogInventory\Api\StockStateInterface $stockItem
)
{
$this->stockItem = $stockItem;
}
and then use getStockQty function to get qty
$this->stockItem->getStockQty($product->getId(), $product->getStore()->getWebsiteId());
if you want to get quantity in .phtml file then use
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface');
echo $StockState->getStockQty($product->getId(), $product->getStore()->getWebsiteId());
?>