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.

<?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.

Review other articles maybe it'll help you too.

Recent Articles
Chrome Extension
Copyright © 2024 All rights reserved.
Ads OFF toggle_off