How to Get Product Quantity Information in Magento 2
One of the most troublesome jobs in eCommerce store management is to control all the data in the inventory and get the required one when needed. Every store owner encounters inventory management challenges at one point or another.
However, today’s market is more customer-centric than ever, and having a more thorough inventory strategy to manage inventory in your warehouse is becoming a necessity.
Handling product quantity is the most essential factor for multi-store inventory management especially when your store has more than one warehouse or source. You can also check programmatically whether stock is managed for particular product or is in need to get managed.
Suppose, you have one source in Chicago and the other in New York and you have the same product at both sources. Now, what if you want to keep track of an inventory of both sources? How can you find out the quantity of a particular product at both warehouses? How can you alert the admin about low product quantity?
In such a case, use the below method to get product quantity information in Magento 2.
Method to Get Product Quantity Information in Magento 2
Use the below code in your helper file.
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 |
<?php namespace Vendor\Module\Helper; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\InventoryConfigurableProductAdminUi\Model\GetQuantityInformationPerSource; /** * Class GetQuantityInformationForProduct */ class GetQuantityInformationForProduct extends AbstractHelper { /** * @var GetQuantityInformationPerSource */ private $getQuantityInformationPerSource; /** * GetQuantityInformationForProduct constructor. * @param GetQuantityInformationPerSource $getQuantityInformationPerSource * @param Context $context */ public function __construct(GetQuantityInformationPerSource $getQuantityInformationPerSource, Context $context) { parent::__construct($context); $this->getQuantityInformationPerSource = $getQuantityInformationPerSource; } /** * @param $productSku * @return array */ public function getQuantityInformationForProduct($productSku) { return $this->getQuantityInformationPerSource->execute($productSku); } } |
Once you have set this code, you get the array data as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Array ( [0] => Array ( [source_code] => source_2 [quantity_per_source] => 1215 [source] => New York [status] => 1 ) [1] => Array ( [source_code] => source_3 [quantity_per_source] => 12297 [source] => Chicago [status] => 1 ) ) |
The above log displays that 1215 quantity is there at the New York and 12297 quantity is there at Chicago.
That’s it!
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.
Also Read:
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
Meetanshi Magento Extensions Launches and Updates April[2021]
How to Add Dynamic Mass Action in Admin Grid in Magento 2
Next