How to get product stock information in magento 2

Getting product stock information means you can fetch some of the detailed information such as minimum quantity (min_qty), minimum sale quantity (min_sale_qty), maximum sale quantity (max_sale_qty), check if product is in stock (is_in_stock), etc. In Magento 2, absolutely yes for you to controll all data in the inventory when you need. The topic How to get product stock information in magento 2 today is a particular documentation so that you will follow to get the product information in your stock in Magento 2.

Overview of getting product stock information in Magento 2

  • Step 1: Declare the command to get product stock information
  • Step 2: Load product id and sku in template file

Step 1: Declare the command to get product stock information

You will use a block class of the module Mageplaza_HelloWorld, then possibly inject the object of \Magento\CatalogInventory\Model\Stock\StockItemRepository class in the constructor of the module’s block class.

app/code/Mageplaza/HelloWorld/Block/HelloWorld.php

<?php
namespace Mageplaza\HelloWorld\Block;
class HelloWorld 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);
    }
}
?>

Step 2: Load product id and sku in template file

Next, please use the below script to load the product by id and sku in the template file.

$id = 123;
$_productStock = $block->getStockItem($id);
//print_r($_productStock->getData()); 
echo $_productStock->getQty(); echo '<br />';
echo $_productStock->getMinQty(); echo '<br />';
echo $_productStock->getMinSaleQty(); echo '<br />';
echo $_productStock->getMaxSaleQty(); echo '<br />';
echo $_productStock->getIsInStock(); echo '<br />';

If you want to see how to get the product ID and SKU in the specific way, How to get product by id and sku in Magento 2 is the perfect suggestion for you.

If you have any queries about the article or any questions in general, use the comment section below!





Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
get
product
stock
information

Comments for How to get product stock information in magento 2

Please leave comments if you have any questions, feedbacks.

Module Development

You also may like these Magento 2 Extensions

One Step Checkout

$199
10 reviews

Layered Navigation

$99
12 reviews

SEO

FREE
no review

People also searched for:

  • magento 2 get product stock information
  • get product stock information in magento 2
  • magento 2 product stock information
  • magento 2 product stock quantity
  • how to get magento 2 product quantity in the stock
  • how to get product stock information in magento 2