How to Get Product Quantity Information in Magento 2 Programmatically

Method to Get Product Quantity Information in Magento 2

create the helper file in your directory.

<?php
namespace VendorName\ModuleName\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);
}
}

Output:

Array
(
[0] => Array
(
[source_code] => source_2
[quantity_per_source] => 1215
[source] => London
[status] => 1
)
[1] => Array
(
[source_code] => source_3
[quantity_per_source] => 12297
[source] => France
[status] => 1
)
)

 

We are providing the best Magento 2 Extentions at the lowest prices. 
Related Products