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

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

1 day ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

1 day ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

2 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

3 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

5 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

5 days ago