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?
Contents
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.
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); } }
$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 />';
$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?
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?
MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…
In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…
Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…
The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…
Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…
If you are a Shopify admin, using a Shopify Balance Account for your business revenue…
View Comments
stockItemRepository->get() method accept only stockItemId not the productId.
Thanks for the suggestion; we will check with the team and do needful for this one.
stockItemRepository->get() will return value only when the stock id is passed or you will get the following error if you pass product id
" The stock item with the "151" ID wasn't found. Verify the ID and try again. "
Yes, that information needs to pass there.
This is straight up wrong, you don't feed stockItemRepository->get() the product id, you give it stock item id >:(
indeed :( and how to get that stockItemId :(