3

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

Can someone help me?

Thanks in advance.

4 Answers 4

9
<?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
  • How can I check saleable qty of product in current quote?
    – huykon225
    Aug 3, 2021 at 2:56
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);
?>

Hope this will help you.

1
  • 2
    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? Jan 23, 2020 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 Jan 23, 2020 at 7:54
  • as per example salable qty is 4 Jan 23, 2020 at 7:55
0

Loop stock qty or sum the stock you need

1
  • output the array value in second position
    – Fabio
    Aug 9, 2022 at 11:05

Your Answer

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

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