How to get qty from all assigned sources (Multi warehouse inventory) programmatically i.e sources and corresponding quantities.
2 Answers
$sourceItemsBySku = $this->getSourceItemsBySku->execute($product->getSku());
$config['stockData'][$productId] = [];
foreach ($sourceItemsBySku as $sourceItem) {
$source = $sourceItem->getSourceCode();
$quantity = $sourceItem->getQuantity();
$config['stockData'][$productId][$source] = $quantity;
}
You need to use Source Item management APIs: https://devdocs.magento.com/guides/v2.3/rest/modules/inventory/manage-source-items.html
For example, to retrieve all Quantities among all Sources where SKU = new_product2 located
you can do with next REST request:
GET /V1/inventory/source-items?searchCriteria[filter_groups][0][filters][0][field]=sku&searchCriteria[filter_groups][0][filters][0][value]=new_product2&searchCriteria[filter_groups][0][filters][0][condition_type]=eq
Or in the code you can just call next service: namespace Magento\InventoryApi\Api;
/**
* Sugar service for find SourceItems by SKU
*
* @api
*/
interface GetSourceItemsBySkuInterface
{
/**
* @param string $sku
* @return \Magento\InventoryApi\Api\Data\SourceItemInterface[]
*/
public function execute(string $sku): array;
}