How to Get Product Salable Quantity in Magento 2
The release of Magento 2.3.3 introduced the concept of salable quantity.
If you have downloaded the Magento 2.3.3 version, you might have seen the salable quantity and quantity in the product grid.
Salable Quantity is the sum of available resources, grouped in stocks. Salable quantity gets changed when the order is placed or the physical quantity in the warehouse changes.
Now, the merchants can link multiple inventory sources to one or multiple websites and manage their inventory in more than one location to accurately reflect their physical inventories without any 3rd party extensions!
While working on inventory management, a need may arrive to apply validation or conditions based on salable quantity.
For instance, the admin may want to hide the SKU and stock status if the salable quantity is zero and if it is greater than zero, show the SKU and stock status in Magento 2 store.
In the above scenario, we have to pass the condition on a salable quantity. To do so, we need to get product salable quantity in Magento 2.
The product grid displays the salable quantity in Magento 2 store:
Use the below method and get the salable quantity programmatically in Magento 2.
Method to Get Product Salable Quantity in Magento 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku; private $getSalableQuantityDataBySku; public function __construct(GetSalableQuantityDataBySku $getSalableQuantityDataBySku) { $this->getSalableQuantityDataBySku = $getSalableQuantityDataBySku; } public function getProductSalableQty() { $sku = "simple_blue"; $salable = $this->getSalableQuantityDataBySku->execute($sku); echo json_encode($salable); } |
1 2 3 4 5 6 |
<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $StockState = $objectManager->get('\Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku'); $qty = $StockState->execute($_product->getSku()); echo($qty[0]['qty']); ?> |
If you have any doubts regarding this post, just mention them in the Comments section below.
I would be happy to help.
Feel free to share the solution with Magento Community via social media.
Thank You.
Related Posts:
- Magento 2 Salable Quantity vs Quantity – Know the Difference
- A Complete Guide on Magento 2 MSI – Multi Source Inventory
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Add Custom Shipping Method Under Shipment Tracking Dropdown in Magento 2
How to Unlock Admin User in Magento 2
Next