How to get stock quantity for all products in my cart Magento 2 and check if they are in stock or out of stock ?

  • You want Quantity for those products which are in cart now? – Amit Bera Feb 4 '18 at 12:27
  • Yes I need the stock quantity for each product in cart, not cart quantity. – Jsparo30 Feb 4 '18 at 12:34
  • please try this to getQty of the Product... $item->getProduct()->getQty(); – Rakesh Varma Feb 4 '18 at 16:48
  1. Extend block with your custom block using di.xml
  2. Load Magento\CatalogInventory\Api\StockRegistryInterface object in your construct function using dependency injection method.
  3. Get current your store Id. It is possible that cart block already method for that.
  4. Try to use $stockRegistry->getStockItem($product->getId(), $storeId)->getQty();

- I believe you can get store id from item itself $item->getStoreId()
- Keep in mind to use product id not item id (item id is product id on the quote) It would be something like this : $item->getProduct()->getId();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.