Magento 2 get product salable qty

If a Magento store has multi-source inventory (MSI) enabled and you want to get the total qty of all the stores which are available to purchase then it is called the salable qty. The salable qty is not the same as the actual qty when MSI is enabled so if you wanted to get the salable qty then this blog post will show you how to do this.

How to get a Salable qty of product using SKU in Magento 2

<?php
namespace Dhairvi\Test\Model;

class ClassName
{
    /**
     * @var \Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku
     */
    protected $getSalableQuantityDataBySku;
	
    /**
     * @param \Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku $getSalableQuantityDataBySku
     */
    public function __construct(
        \Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku $getSalableQuantityDataBySku
    ) {
        $this->getSalableQuantityDataBySku = $getSalableQuantityDataBySku;
    }
    
    public function getSalableQtyBySku($sku)
    {
        return $this->getSalableQuantityDataBySku->execute($sku);
    }
}

In the output, you will get all the available sources and it’s total qty available to purchase.