3

I want to display the current stock of a specific product in a theme. How can I do that?

getStock() does not seem to work.

3

2 Answers 2

6

It mainly depends on the file where you want to display it.

First you need to find out the block where you want to display that info.

Then you'll have to setup a preference for that block in a custom module:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Path\To\Core\Block\You\Want\To\Rewrite" type="Vendor\Module\Block\Custom" />
</config>

Then create your Block\Custom file:

<?php

namespace Vendor\Module\Block;

class Custom extends Path\To\Core\Block\You\Want\To\Rewrite
{
    protected $_stockRegistry;

    public function _construct(
         ...
         \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
         ...)
     {
         $this->_stockRegistry = $stockRegistry;
         ...
     }

     public function getStockItem($productId)
     {
         return $this->_stockRegistry->getStockItem($productId);
     }
}

Please note the ... in the constructor. You'll have to replace those with the original arguments from the block you're rewriting and then call the parent constructor with the original parameters.

Finally in your template you can do:

$stockItem = $block->getStockItem($productId);
$stockQty = $stockItem->getQty();
5
  • is there any possibility by receiving this values WITHOUT creating a new module?
    – Max
    Oct 10, 2016 at 11:51
  • @Max yes using the ObjectManager directly but that's not recommended Oct 10, 2016 at 11:54
  • And that is the only possibility? Always asked myself if there is a way more simple then doing so (creating a plugin)
    – Max
    Oct 10, 2016 at 12:00
  • 1
    @Max well there's two possibilities : object manager (not recommended) or module (recommended) Oct 11, 2016 at 8:34
  • @RaphaelatDigitalPianism .. I need to display Quantity in grouped product. Can you please look at magento.stackexchange.com/q/153914/29175
    – Jarnail S
    Jan 10, 2017 at 8:14
2

You can use below function to get stock Qty

protected $stockStateInterface;

    public function _construct(
         ...
         \\Magento\CatalogInventory\Api\StockStateInterface $stockStateInterface,
         ...)
     {
         $this->stockStateInterface = $stockStateInterface;
         ...
     }

     public function getStockItemQty($product)
     {
         return $this->stockStateInterface->getStockQty($product->getId(), $product->getStore()->getWebsiteId());
     }

     //return true if product in stock otherwise return false
      public function getStockItemStatus($product)
      {
        return $this->stockStateInterface->verifyStock($product->getId(), $product->getStore()->getWebsiteId());
      }
1
  • 1
    Hi, I can't edit your post. Where is the $this->_stockItemRepository come from? It should be $this->stockStateInterface.
    – Key Shang
    Jun 19, 2018 at 3:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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