How To

How to Get Product Salable Quantity in Magento 2?

In today’s Magento 2 tutorial, I am going to illustrate how to obtain a salable quantity of product in Magento 2.

The functionality of Salable Quantity was introduced first with the release of Magento 2.3. Salable quantity allows managing the warehouse and avoiding out-of-stock conditions.

When an order is issued the quantity of the product does not reduce. Only the salable quantity is decreased. The quantity decreases after the order is shipped. Hence, the quantity and salable quantity are not always the same.

Solve the issue – quantity availability showing in stock even if the salable quantity is 0

Admin needs to keep an eye on salable quantity to manage inventory. Use the following code to get the salable quantity of product in Magento 2.

Steps to Get Product Salable Quantity in Magento 2:

Step 1: Create the following helper file in your extension

app\code\Vendor\Extension\Helper\Data.php

Then add the below code

<?php

namespace Vendor\Extension\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku;

class Data extends AbstractHelper
{
      private $getSalableQtyDataBySku;

      public function __construct(
             Context $context,
             GetSalableQuantityDataBySku $getSalableQtyDataBySku
      ){
             $this->getSalableQtyDataBySku = $getSalableQtyDataBySku;
             parent::__construct($context);
       }

       public function getSalableQtyBySku($sku)
       {
             $salable = $this->getSalableQuantityDataBySku->execute($sku);
            return json_encode($salable);
       }
}

You will get the following Output:

 [{"stock_name":"Default Stock","qty":100,"manage_stock":true}]

Conclusion:

And that’s it! You can easily fetch the salable quantity of product in Magento 2. If you are unable to get the desired output of a salable quantity, reach out to me via the comment section. Share the solution with your friends and stay updated for more tutorials.

Happy Coding!

Click to rate this post!
[Total: 4 Average: 4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

5 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

1 week ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

1 week ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago