How to Get Salable Quantity Information After Product is Saved in Magento 2
The concept of salable quantity is introduced with the release of Magento 2.3.3 with the multi-source inventory system.
Salable quantity is the sum of all available items that allows you to handle all of your warehouses and get changed when the order is placed or the physical quantity in the warehouse changes. Know the difference between Magento 2 Salable Quantity and Quantity.
You might have noticed the ‘salable quantity’ column in the product grid. However, what if you want to programmatically get salable quantity information after product is saved in Magento 2 to maintain the well-structured inventory grid?
For instance, you want to keep track of the changes in the salable quantity in the Magento 2 inventory report when product quantity gets changed from the product page. Get product salable quantity because salable quantity is the sum of available resources, grouped in stocks to get quantity information after product is saved.
Also read:
In such a scenario, use the below code and perform further operations based on the detail of salable quantity information after a product is saved.
Method to Get Salable Quantity Information After Product is Saved in Magento 2
- Create Registration.php file at app/code/Vendor/Module/
123456789<?phpuse Magento\Framework\Component\ComponentRegistrar;ComponentRegistrar::register(ComponentRegistrar::MODULE,'Vendor_Module',__DIR__); - Create module.xml file at app/code/Vendor/Module/etc
123456<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"><module name="Vendor_Module" setup_version="1.0.0"></module></config> - Create di.xml file at app/code/Vendor/Module/etc/
1<!--?xml version="1.0"?--> - Create GetSalableQuantityAfterProductSave.php file at app/code/Vendor/Module/Helper/ and use the below code:
1234567891011121314151617181920212223242526272829303132333435363738394041<?phpnamespace Vendor\Module\Helper;use Magento\Framework\App\Helper\AbstractHelper;use Magento\Framework\App\Helper\Context;use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku;/*** Class GetSalableQuantityAfterProductSave*/class GetSalableQuantityAfterProductSave extends AbstractHelper{/*** @var GetSalableQuantityDataBySku*/private $getSalableQuantityDataBySku;/*** GetSalableQuantityAfterProductSave constructor.* @param GetSalableQuantityDataBySku $getSalableQuantityDataBySku* @param Context $context*/public function __construct(GetSalableQuantityDataBySku $getSalableQuantityDataBySku, Context $context){parent::__construct($context);$this->getSalableQuantityDataBySku = $getSalableQuantityDataBySku;}/*** @param $productSku* @return array*/public function getSalableQuantityAfterProductSave($productSku){return $this->getSalableQuantityDataBySku->execute($productSku);}}Do you still have any queries? Ask in the comments. I’d be happy to help you. 🙂
Feel free to share this solution with your friends via social media. 😇
Thanks for reading! 🍀
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 Dynamically Schedule Cron Job in Magento 2 System Configuration
How to Get All Coupon Code of Particular Cart Price Rules in Magento 2
Next