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?
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 🙁
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.
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.