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), out of stock, etc. In Magento 2, online stores need to get data of inventory change for regular tracking, which can help them in making future business decisions. Although Magento 2 well supports this need, store owners like you may be confused in which way you can do it quickly. The tutorial today is going to show you how to get product stock information in Magento 2.

2 steps to get product stock information in Magento 2

Step 1: Declare the command to get product stock information

First, 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 />';

That’s all you have to do to get the information of product stock. So easy, right?

The bottom line

Getting product stock information is essential for every Magento 2 store, as it can influence stock management and future business strategies. The coding process can be done quickly as above, so I believe you can easily pull it off. 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!

Related Topics

Image Description
With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.

People also searched for

  • magento 2 get stock item by product id
  • magento 2 get product qty
  • magento 2 get product stock quantity
  • get product stock status magento 2
  • magento 2 get product stock
  • magento 2 get product stock status
  • magento 2 get product quantity
  • magento 2 check product is in stock
  • magento 2 get product quantity programmatically
  • magento 2 get stock status
  • magento 2 check if product is in stock
  • magento 2 update saleable quantity programmatically
  • magento get product stock quantity
  • get product quantity in magento 2
  • magento 2 check if product is out of stock
  • magento 2 get product stock qty
  • get product qty in magento 2
  • magento 2 quantity_and_stock_status
  • magento 2 product stock status
  • 2.3.x, 2.4.x
x