2

How to get salable qty of each Product and display in Product view page.

Can someone help me?

Thanks in advance.

7
<?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.

1

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.

1
  • I tried this one and got answer. Here you mentioned 2 manually.It is stock_id. How to get stock_id dynamically and update salable qty? – Ramalakshmi T Jan 23 '20 at 7:14
1

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}]

Reference Link

2
  • It's showing array of stock information.I need to show only salable qty. How to get only salable qty, not stock name,manage stock? @Rk Rathod – Ramalakshmi T Jan 23 '20 at 7:54
  • as per example salable qty is 4 – Rk Rathod Jan 23 '20 at 7:55

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.