{"id":7303,"date":"2019-09-26T19:01:05","date_gmt":"2019-09-26T13:31:05","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=7303"},"modified":"2019-10-04T11:20:40","modified_gmt":"2019-10-04T05:50:40","slug":"magento-2-product-stock-quantity-min-quantity-stock-information","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-product-stock-quantity-min-quantity-stock-information\/","title":{"rendered":"Magento 2 &#8211; How to Get Product Stock Quantity, Min Quantity and Other Stock Information"},"content":{"rendered":"<p>In this post I am going to explain how to load product stock quantity, min quantity and other stock information in magento 2.<\/p>\n<p><strong>1. Using Dependency Injection<\/strong><\/p>\n<p>Add below code snippet in Block class.<\/p>\n<pre class=\"lang:default decode:true \">protected $_stockItemRepository;\r\n        \r\npublic function __construct(\r\n\t\\Magento\\Backend\\Block\\Template\\Context $context,        \r\n\t\\Magento\\CatalogInventory\\Model\\Stock\\StockItemRepository $stockItemRepository,\r\n\tarray $data = []\r\n)\r\n{\r\n\t$this-&gt;_stockItemRepository = $stockItemRepository;\r\n\tparent::__construct($context, $data);\r\n}\r\n\r\npublic function getStockInformation($productId)\r\n{\r\n\t$productStockInfo = $this-&gt;_stockItemRepository-&gt;get($productId);\r\n\treturn $productStockInfo;\r\n}<\/pre>\n<p>Add below code snippet in template file.<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Your product ID\r\n$productId = 9;\r\n\r\n\/\/ Get Product Stock Information\r\n$productStockInfo = $block-&gt;getStockInformation($productId);\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($productStockInfo-&gt;getData());\r\necho \"&lt;\/pre&gt;\";\r\n\r\necho $productStockInfo-&gt;getQty() . '&lt;br\/&gt;';\r\necho $productStockInfo-&gt;getIsInStock() . '&lt;br\/&gt;';\r\necho $productStockInfo-&gt;getMinQty() . '&lt;br\/&gt;';\r\necho $productStockInfo-&gt;getMinSaleQty() . '&lt;br\/&gt;';\r\necho $productStockInfo-&gt;getMaxSaleQty() . '&lt;br\/&gt;';<\/pre>\n<p><strong>2. Using Object Manager<\/strong><\/p>\n<pre class=\"lang:default decode:true \">$objectManager = \\Magento\\Framework\\App\\ObjectManager::getInstance();\r\n\r\n$stockItemRepository = $objectManager-&gt;get('\\Magento\\CatalogInventory\\Model\\Stock\\StockItemRepository');\r\n\r\n\/\/ Your product ID\r\n$productId = 9;\r\n\r\n\/\/ Get Product Stock Information\r\n$productStockInfo = $stockItemRepository-&gt;get($productId);\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($productStockInfo-&gt;getData());\r\necho \"&lt;\/pre&gt;\";\r\n\r\necho $productStockInfo-&gt;getQty() . '&lt;br\/&gt;';\r\necho $productStockInfo-&gt;getIsInStock() . '&lt;br\/&gt;';\r\necho $productStockInfo-&gt;getMinQty() . '&lt;br\/&gt;';\r\necho $productStockInfo-&gt;getMinSaleQty() . '&lt;br\/&gt;';\r\necho $productStockInfo-&gt;getMaxSaleQty() . '&lt;br\/&gt;';<\/pre>\n<p>Thats it. Enjoy Magento 2!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to load product stock quantity, min quantity and other stock information in magento 2. 1. Using Dependency Injection Add below code snippet in Block class. protected $_stockItemRepository; public function __construct( \\Magento\\Backend\\Block\\Template\\Context $context, \\Magento\\CatalogInventory\\Model\\Stock\\StockItemRepository $stockItemRepository, array $data = [] ) { $this-&gt;_stockItemRepository = $stockItemRepository; parent::__construct($context, $data); }&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-product-stock-quantity-min-quantity-stock-information\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":7372,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[9,10,27],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7303"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=7303"}],"version-history":[{"count":2,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7303\/revisions"}],"predecessor-version":[{"id":7305,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7303\/revisions\/7305"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/7372"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=7303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=7303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=7303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}