Get source item data by source code Multi Source Inventory(MSI) in Magento 2

We can use \Magento\InventoryApi\Api\SourceItemRepositoryInterface class to get source items

Here are the sample model class to get the sources item by source code

<?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)
    {
         $searchCriteria = $this->searchCriteriaBuilder
            ->addFilter('source_code', $souceCode)
            ->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';
$sourceItems = $this->getSourcesItems($sourceCode);

foreach ($sourceItems as $sourceItem) {
    print_r($sourceItem->getData());
}

OUTPUT:

Array
(
    [source_item_id] => 145
    [source_code] => test_store
    [sku] => PIM-1023
    [quantity] => 10.0000
    [status] => 1
)

Thank you for reading. Keep sharing 🙂

Leave a Reply

Close Menu