How to get salable qty of each Product and display in Product view page.
Can someone help me?
Thanks in advance.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this community<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get('\Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku');
$qty = $StockState->execute($_product->getSku());
echo ($qty[0]['qty']);
?>
You can get only the saleable quantity by the following above code.
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);
?>
Hope this will help you.
Use the following code :-
namespace Vendor\Module\ModelName;
use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku;
class ClassName
{
private $getSalableQuantityDataBySku;
public function __construct(
GetSalableQuantityDataBySku $getSalableQuantityDataBySku
) {
$this->getSalableQuantityDataBySku = $getSalableQuantityDataBySku;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$sku = "testsku"; //pass your product sku
$salable = $this->getSalableQuantityDataBySku->execute($sku);
echo json_encode($salable);
}
}
Output :-
[{"stock_name":"Default Stock","qty":4,"manage_stock":true}]
Loop stock qty or sum the stock you need