How to Get Product Salable Quantity in Magento 2

Our today's topic in the Magento solution series is How To Get Product Salable Quantity In Magento 2. Each new version of Magento brings new things for the betterment. Magento 2.3.3 brought the Salable Quantity concept.

In earlier versions, products' quantity decreases when an order is placed. But due to the new concept, only salable quantity decreases, whereas quantity remains the same.

How To Get Product Salable Quantity In Magento 2

⇒ You do not need to do much to get product salable quantity in Magento. The only thing to do is add some code and get product salable quantity.

⇒ Use the below code for salable quantity:

namespace Milople\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 = "testsimpleproduct1";
        $salable = $this->getSalableQuantityDataBySku->execute($sku);
        echo json_encode($salable);
    }
}

⇒ Using Object manager also, you can get product salable quantity.

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

⇒ You are done!

You also can check more Solutions related to Magento. Else do let us know the issue you are facing, we'll help you.

Query solved?! If still facing the issue, write in the comment or contact us.
Get in touch with us to hire Magento Developer dedicatedly. Also, we provide hourly-based service for any of your Magento development needs.

Also, Read: