0

Using Magento 2.1.7 - Stock fustration

Hi, I need to be able to access on a category view each of the products stock qty.

I need this so I can then display a particular icon against each product dependant on the value. If 0 show an out of stock icon, else, show an in stock icon which wont be a problem but accessing the stock details from the product collection is baffling me.

Any pointers would be very gratefully received.

Cheers Jim

0

protected $stockItem;

public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\CatalogInventory\Api\StockStateInterface $stockItem
    ) {
        $this->stockItem = $stockItem;
        parent::__construct($context);
    }

public function getStockQty(ModelProduct $product)
{
    $qty = $this->stockItem->getStockQty($product->getId(), $product->getStore()->getWebsiteId());
    return $qty;
}

You will have to pass $product object to get QTY.

Hope it helps :)

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.