How to Get Salable Quantity in Magento 2
If you are using MSI (Multi-Source Inventory) and you want to get the salable quantity using product SKU then you're in the right place. In this article, we'll explain you how to get salable quantity in Magento 2.
Let's create the helper class and inject the required classes in the constructor of your helper class.
saveCopyzoom_out_map<?php
namespace Vendor\ModuleName\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\InventorySalesApi\Api\GetProductSalableQtyInterface;
use Magento\InventorySalesApi\Api\StockResolverInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
class GetSalableQty extends AbstractHelper
{
private GetProductSalableQtyInterface $getProductSalableQty;
private StockResolverInterface $stockResolver;
private StoreManagerInterface $storeManager;
/**
* @param Context $context
* @param GetProductSalableQtyInterface $getProductSalableQty
* @param StockResolverInterface $stockResolver
* @param StoreManagerInterface $storeManager
*/
public function __construct(
Context $context,
GetProductSalableQtyInterface $getProductSalableQty,
StockResolverInterface $stockResolver,
StoreManagerInterface $storeManager,
) {
parent::__construct($context);
$this->getProductSalableQty = $getProductSalableQty;
$this->stockResolver = $stockResolver;
$this->storeManager = $storeManager;
}
public function getSalableQty($sku)
{
$websiteCode = $this->storeManager->getWebsite()->getCode();
$stock = $this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, $websiteCode);
$stockId = $stock->getStockId();
return $this->getProductSalableQty->execute($sku, $stockId);
}
}
In above code, you can see we've injected the GetProductSalableQtyInterface, StockResolverInterface and StoreManagerInterface classes in the constructor and created the getSalableQty Method with the sku parameter. You can then call the getSalableQty method with the SKU of the product to get the salable quantity.
Keep in mind that this is just an example, and you may need to adapt it based on your specific requirements and the structure of your code.
We hope this Magento article helped you to know how to get salable quantity in Magento 2.
Bookmark it for your future reference. Do comment below if you have any other questions or doubts.
P.S. Do share this article with your team.
AI-Powered Recommended Articles
How to Get Product Collection in Magento 2
Learn how to get product collections in Magento 2 and filter them based on various parameters.
How to Get Current Product in Magento 2
Learn how to retrieve the current product in Magento 2 to dynamically display product-specific information on your website.
How to Get Current Category in Magento 2
Learn how to retrieve the current category in Magento 2 using code to display relevant category data in your store.
How to Get Order Details by Order Id in Magento 2
Learn how to retrieve detailed order information using the order ID in Magento 2 to manage customer orders efficiently.
How to Create Custom GraphQL in Magento 2
Learn how to create custom GraphQL queries in Magento 2 for enhanced API integrations.
Get Product Collection with a Specific Attributes in Magento 2
Learn how to retrieve product collections with specific attributes in Magento 2 using custom queries.