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:
class HelloWorld extends Template
public function __construct(
$this->stockItemRepository = $stockItemRepository;
public function getStockItem($productId)
$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.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$stockItem = $objectManager->get('\Magento\CatalogInventory\Model\Stock\StockItemRepository');
$productId = 1; // YOUR PRODUCT ID
$productStock = $stockItem->get($productId);
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. 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.
Get Weekly Updates
Never miss Magento tips, tricks, tutorials, and news.
Thank you for subscribing.
Something went wrong.