🔥 Just Launched! Werra Premium Template for HyväSee it in Action

How to Get Product Stock Information in Magento 2

By Sanjay JethvaUpdated on Apr 24, 2025 2 min read

Keeping up with the stock is the most time consuming and cumbersome task for store owners. Unless you have limited products, you have to spend a significant amount of time to track inventory. As stock management plays an effective role in efficient store functioning, store owners need to have a better track of stock and inventory.

Magento 2 store owners need to plan the store selling strategy based on the stock details. For that, they require to get stock information on the timely basis. To get product stock information in Magento 2 such as in stock products, out of stock products, minimum stock quantity, minimum sale quantity, minimum quantity, etc., implement any of the below methods.

  1. Use of the class
  2. Use of the object manager

Admin can use this information and modify the rules of the store that involve the quantity of products

Methods to Get Product Stock Information in Magento 2:

Now add the below code to the phtml file

Method 1: Use of the Class

<?php
namespace Vendor\Extension\Block;
 
use Magento\Framework\View\Element\Template;
use Magento\Backend\Block\Template\Context;
use Magento\CatalogInventory\Model\Stock\StockItemRepository;
 
class HelloWorld extends Template
{    
    protected $stockItemRepository;
        
    public function __construct(
        Context $context,        
        StockItemRepository $stockItemRepository
    )
    {
        $this->stockItemRepository = $stockItemRepository;
        parent::__construct($context);
    }
    
    public function getStockItem($productId)
    {
        return $this->stockItemRepository->get($productId);
    }
}

Now add the below code to the phtml file

$id = YOUR_PRODUCT_ID;
$productStock = $block->getStockItem($id);
echo $productStock->getQty().'<br />';
echo $productStock->getMinQty().'<br />';
echo $productStock->getMinSaleQty().'<br />';
echo $productStock->getMaxSaleQty().'<br />';
echo $productStock->getIsInStock().'<br />';

  Method 2: Use of Object Manager

Implement the below code to get product stock information in Magento 2 using the object manager.

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance(); 
$stockItem = $objectManager->get('\Magento\CatalogInventory\Model\Stock\StockItemRepository');
$productId = 1; // YOUR PRODUCT ID
$productStock = $stockItem->get($productId);
var_dump($productStock->getData());

Never allow the excess or shortage of the inventory to downfall your business, use the above methods to get stock information in Magento 2 and plan the selling strategy and manage inventory in a better way. Also you can programmatically check whether stock is managed for particular product or not so you will get to know about your product stock. Let me know how this blog has helped you and what changes you made to customize the code. Do comment if you have any questions regarding the topic.

Rate the blog with 5 stars and stay tuned for more such blogs.

Sanjay Jethva Full Image
Article bySanjay Jethva

Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe. His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.