How to Get Source Information Assigned to Stock by Priority in Magento 2
Merchants were only able to have one inventory source for their store before Magento introduced the new multi-source inventory (MSI) feature in Magento 2.3 version.
MSI is a considerable improvement to the Magento core, which connects multiple inventory sources to your e-commerce website.
Single source inventory means the business operates only one source location managing on-hand Magento inventory and merchants ship products from one place to every customer worldwide. Therefore, it is easy to deduct the product quantity or add the same in the single-source inventory.
However, while working with multi-source inventory, it is critical to decide which product has to be deducted from which source, especially when a credit memo gets generated and the admin sets “No” in the “Decrease Stock When Order is Placed” option from Stores -> Settings -> Configuration -> Catalog -> Inventory.
For instance, salable quantity is deducted when an order gets placed. Now, what if a credit memo gets generated before generating shipment? When a credit memo is generated directly without shipment generation, it deducts the quantity of any one source from multiple sources. How can you identify that it decreased the quantity of which source?
In that case, Magento assigns priority in the multi-source inventory. The whole system manages the addition and deduction of stock based on priority.
Now, what if you want to keep track of the source information assigned to stock in your Magento 2 store? What if you’re going to identify stock changes in the particular source in ascending or descending order?
In that scenario, you need to get source information assigned to stock by priority in Magento 2 using the below solution:
Method to Get Source Information Assigned to Stock by Priority in Magento 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php namespace Vendor\Module\Helper; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\Inventory\Model\Source\Command\GetSourcesAssignedToStockOrderedByPriority; class GetSourcesAssignedToStockByPriority extends AbstractHelper { /** * @var GetSourcesAssignedToStockOrderedByPriority */ private $getSourcesAssignedToStockOrderedByPriority; public function __construct( GetSourcesAssignedToStockOrderedByPriority $getSourcesAssignedToStockOrderedByPriority, Context $context ) { parent::__construct($context); $this->getSourcesAssignedToStockOrderedByPriority = $getSourcesAssignedToStockOrderedByPriority; } public function getSourcesAssignedToStockByPriority($stockId) { $sources = []; foreach ($this->getSourcesAssignedToStockOrderedByPriority->execute($stockId) as $item) { $sources = $item->getData(); } return $sources; } } |
You can get stock information as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
Array ( [0] => Array ( [source_code] => source_3 [name] => Source 3 [enabled] => 1 [description] => enough amount of stock [latitude] => 99.999999 [longitude] => 234.000000 [country_id] => IN [region_id] => 537 [region] => Bihar [city] => farrukhabad [street] => 150/B [postcode] => 800001 [contact_name] => Ramprakash Mishra [email] => Mishra4544@yahoo.com [phone] => 785426888 [fax] => +1 323 555 1234 [use_default_carrier_config] => 1 [carrier_links] => Array ( ) ) [1] => Array ( [source_code] => source_2 [name] => Source 2 [enabled] => 1 [description] => not enough stock [latitude] => 99.999999 [longitude] => 234.000000 [country_id] => IN [region_id] => 544 [region] => Gujarat [city] => Vapi [street] => 454/w [postcode] => 392001 [contact_name] => Harishankar Sharma [email] => Sharma78@gmail.com [phone] => 9784554444 [fax] => +44 (0)20 7555-123 [use_default_carrier_config] => 1 [carrier_links] => Array ( ) ) ) |
That’s it. Get product salable quantity in Magento 2 to know the sum of available resources, grouped in stocks.
If you have queries, feel free to ask in the Comment section below.
I would like to solve your problem.
Don’t forget to share this post with Magento Community via social media.
Thank you.
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 Uncancel Order Directly From Database in Magento
How to Programmatically Display Recently Viewed Products in Magento 2
Next