ON THIS PAGE
What is Salable Quantity in Magento (Adobe Commerce)?
Magento salable quantity (formerly known as Adobe Commerce) is the sum of all products from all your sources. It decreases when an order is placed. However, when somebody cancels an order, products are added back to the salable quantity. In other words, salable quantity reflects the number of items you can sell.
Let’s look at how to get product salable quantity programmatically.
Get Salable Quantity
<?php
namespace Vendor\Extension\Model;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\InventorySalesApi\Api\GetProductSalableQtyInterface;
use Magento\InventorySalesApi\Api\StockResolverInterface;
use Magento\Store\Model\StoreManagerInterface;
class GetSalableQtyBySku
{
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @var StockResolverInterface
*/
private $stockResolver;
/**
* @var GetProductSalableQtyInterface
*/
private $getProductSalableQty;
/**
* @var array
*/
private $stockIdCache=[];
public function __construct(
StoreManagerInterface $storeManager,
StockResolverInterface $stockResolver,
GetProductSalableQtyInterface $getProductSalableQty
)
{
$this->storeManager = $storeManager;
$this->stockResolver = $stockResolver;
$this->getProductSalableQty = $getProductSalableQty;
}
public function execute($productSku){
$salableQty=0;
try {
$store = $this->storeManager->getStore();
$stockId = $this->getStockId($store);
$salableQty = $this->getProductSalableQty->execute($productSku, $stockId);
}catch (\Exception $exception){
$salableQty=0;
}
return $salableQty;
}
private function getStockId($store){
$websiteCode = $store->getWebsite()->getCode();
if(!isset($this->stockIdCache[$websiteCode])) {
$stock = $this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, $websiteCode);
$stockId = (int)$stock->getStockId();
$this->stockIdCache[$websiteCode] = $stockId;
}
return $this->stockIdCache[$websiteCode];
}
}
Use the above model class to get salable qty from your Helper class or ViewModel.
Here I have used that model in the Helper class to get product salable qty.
<?php
namespace Vendor\Extension\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
class Data extends AbstractHelper
{
/**
* @var \Vendor\Extension\Model\GetSalableQtyBySku
*/
private $getSalableQtyBySku;
public function __construct(
\Vendor\Extension\Model\GetSalableQtyBySku $getSalableQtyBySku
)
{
$this->getSalableQtyBySku = $getSalableQtyBySku;
}
public function getSalableQty($productSku){
$salableQty=$this->getSalableQtyBySku->execute($productSku);
return $salableQty;
}
}
By calling the helper class you will get salable qty
Output:
25
Please visit the developer documentation to get know more about Magento (Adobe commerce) MSI Here.