Magento2 – Get Salable Qty

In Magento2, in order to get salable qty, you can call an API service Magento\InventorySalesApi\Api\GetProductSalableQtyInterface.

In core modules, it is actually implemented by a class Magento\InventorySales\Model\GetProductSalableQty. You can see a salable qty is the result of current stock quantity counted on inventory sources subtract the sum of reservation quantity. Min Qty will be considered if you define it.

public function execute(string $sku, int $stockId): float
    {
        $this->validateProductType($sku);
        $stockItemData = $this->getStockItemData->execute($sku, $stockId);
        if (null === $stockItemData || (bool)$stockItemData[GetStockItemDataInterface::IS_SALABLE] === false) {
            return 0;
        }

        $stockItemConfig = $this->getStockItemConfiguration->execute($sku, $stockId);
        $minQty = $stockItemConfig->getMinQty();

        $productQtyInStock = $stockItemData[GetStockItemDataInterface::QUANTITY]
            + $this->getReservationsQuantity->execute($sku, $stockId)
            - $minQty;

        return $productQtyInStock;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *