General

How to Get Product Stock Information in Magento 2

Howdy Friends?,

Welcome to the Magento 2 Tutorial Blog. We are going to explore How to Get Product Stock Information in Magento 2. Have a look at our latest article published to solve How to Configure the Wishlist in Magento 2. Let’s get started?

Introduction:

Basically, merchants are suffering when it comes to handling the stock of the store. It is very time-consuming and complicated one has to spend their valuable time to manage stock. But the store owner cannot ignore stock management because it is a very important task of any store. Magento 2 store owners should precisely plan the stock management of the store. For the best practice, the merchants are advised to plan a strategy where the stock information is updated on selling that is carried out on a timely basis. This will help the store owners to maintain stock easily. For that, the admin should have to Get Product Stock Information in Magento 2. Apply the given below method for getting product stock information in Magento 2. For the ultimate guide for Inventory Management visit How to Manage Magento 2 Inventory Management: The Ultimate Guide. 

Steps to Get Product Stock Information in Magento 2:

Using class:

Create one block file on our custom extension add Blockname.php in following path

app\code\Vendor\Extension\Block\Blockname.php

<?php
namespace Vendor\Extension\Block;

use Magento\Framework\View\Element\Template;
use Magento\Backend\Block\Template\Context;
use Magento\CatalogInventory\Model\Stock\StockItemRepository;

class Blockname extends Template
{    
    protected $stockItemRepository;
        
    public function __construct(
        Context $context,        
        StockItemRepository $stockItemRepository
    )
    {
        $this->stockItemRepository = $stockItemRepository;
        parent::__construct($context);
    }
    
    public function getStockItemInformation($productId)
    {
        return $this->stockItemRepository->get($productId);
    }
}

After this step, you need to add the given below code to the phtml file.

$id = YOUR_PRODUCT_ID; <!-- Here you need to pass product id -->
$productStock = $block->getStockItemInformation($id);
echo $productStock->getQty().'<br />';
echo $productStock->getMinQty().'<br />';
echo $productStock->getMinSaleQty().'<br />';
echo $productStock->getMaxSaleQty().'<br />';
echo $productStock->getIsInStock().'<br />';

Using Object Manager:

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

Note: Using ObjectManager for Magento development is not recommended by us and Magento. This is just for Knowledge purposes.

That’s It. Apply any of the above methods and easily handle the stock management of your Magento 2 store. Without manual effort Automate your Inventory Management with Magento 2 Inventory Sync Extension?

Final Words:

All the Magento 2 merchants can easily handle the stock management with the help of the above tutorial. One can apply and experience the increase in revenue of their store. In case any difficulties do write down in the comment section below I would love to answer your concerns. If you liked the article then share it with your Magento friends. 

Happy Coding?

Click to rate this post!
[Total: 33 Average: 3.1]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

Recent Posts

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

12 hours ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

1 day ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

3 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

5 days ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

6 days ago

Best Beginners Guide to Shopify Balance Account

If you are a Shopify admin, using a Shopify Balance Account for your business revenue…

6 days ago