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.
<?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:
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!
Feel free to share the solution with Magento Community via social media.
Thank You.
Also Read: