0

How would I go about displaying a product's warehouse location on the product page? We are using MSI and would like to display which stock source the product is in stock at on the product page.

1

you can use \Magento\InventoryApi\Api\SourceItemRepositoryInterface class with \Magento\Framework\Api\SearchCriteriaBuilder to get source item data by source code and product SKU.

Here are the sample model class

<?php
namespace Mageprince\Testing\Model;
class SourceItemModel
{
   
    private $searchCriteriaBuilder;
    private $sourceItemRepository;
    public function __construct(
        ...
        \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
        \Magento\InventoryApi\Api\SourceItemRepositoryInterface $sourceItemRepository
        ...
    ) {
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->sourceRepository = $sourceRepository;
    }
    public function getSourcesItems($souceCode, $sku)
    {
         $searchCriteria = $this->searchCriteriaBuilder
            ->addFilter('source_code', $souceCode)
            ->addFilter('sku', $sku)
            ->create();
        $sourceItemData = $this->sourceItemRepository->getList($searchCriteria);
        return $sourceItemData->getItems();
    }
}

Now you can use getSourcesItems() function to get all source items by sources code

$sourceCode = 'test_store';
$sku = 'PO2-113'
$sourceItems = $this->getSourcesItems($sourceCode);
foreach ($sourceItems as $sourceItem) {
    print_r($sourceItem->getData());
}

OUTPUT:

Array
(
    [source_item_id] => 125
    [source_code] => test_store
    [sku] => PO2-113
    [quantity] => 10.0000
    [status] => 1
)

for get all MSI Source use below link.

https://mageprince.com/blog/how-to-get-all-list-of-sources-multi-source-inventory-msi-programmatically-in-magento-2/

Prince Patel added lots of things about MSI on his blog.

2
  • That's very close to what I use to show the stock of children products on a grouped product page. Took me a lot of fighting, wish I'd found this answer last summer :D – iphigenie May 31 at 14:56
  • @iphigenie I can understand your words..and thanx for upvote it – Gohil Rajesh May 31 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.