'Magento2.4: How to get Salable quantity in list.phtml file?

How can I get salable quantity on list.phtml or category page file, I want to show labels on products with 0 salable quantity. Are there any other approaches without using object manager?



Solution 1:[1]

Please use this code in phtml file to get salable qty

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get('\Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku');
$qty = $StockState->execute($_product->getSku());
echo($qty[0]['qty']);

Solution 2:[2]

Try the below code to get salable QTY.

<?php 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $StockState = $objectManager->get('\Magento\InventorySalesApi\Api\GetProductSalableQtyInterface');
    $qty = $StockState->execute($_product->getSku(), 2);
?>

Either Object Manager is not a good approach, but you need to inject in your custom module such as:

namespace Cloudways\Module\ModelName;
use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku;

Refer Link: https://magento.stackexchange.com/questions/301956/how-to-get-salable-qty-in-magento-2-3-3/302187#302187

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ankur Verma
Solution 2 Jeremy Caney