How to Get Product Stock Information in Magento 2
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.
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:
Method 1: Use of the Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?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); } } |
1 2 3 4 5 6 7 |
$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 />'; |
Implement the below code to get product stock information in Magento 2 using the object manager.
1 2 3 4 5 |
$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
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.
4 Comments
If we use product id it’s throwing the following error
” The stock item with the “151” ID wasn’t found. Verify the ID and try again. “
Hello Manikandan,
Please make sure the product ID that you are using is available.
Thank You
Here in this post you load stockitem by product id. when you go to \Magento\CatalogInventory\Model\Stock\StockItemRepository.php file check get() function . you can see that get() function load by stockItemId not by product id. If I am wrong you an correct me. Here is you post link https://meetanshi.com/blog/get-product-stock-information-in-magento-2/
Hello Sunil,
Yes, though the stockItemId is declared in a core file, we need to pass the Product ID there.
Thank You