Get source by name Multi Source Inventory(MSI) Magento 2

You can get the source by using the repository \Magento\InventoryApi\Api\SourceRepositoryInterface and filter source name by using \Magento\Framework\Api\SearchCriteriaBuilder

Magento has default source with name “Default Source”. You can’t delete this source.

You can get the source by its name, code, description, latitude, longitude and postcode

Here is the example of retrieving the source data


namespace Mageprince\Testing\Model;

class SourceModel
    private $searchCriteriaBuilder;

    private $sourceRepository;

    public function __construct(
        \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
        \Magento\InventoryApi\Api\SourceRepositoryInterface $sourceRepository
    ) {
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->sourceRepository = $sourceRepository;

    public function getSources($sourceName)
         $searchCriteria = $this->searchCriteriaBuilder
            ->addFilter('name', $sourceName)
            //->addFilter('source_code', $souceCode)    // Filter by source code
            //->addFilter('enabled', $enabled)          // Filter by enable
            //->addFilter('description', $description)  // Filter by description
            //->addFilter('latitude', $lat)             // Filter by latitude
            //->addFilter('longitude', $lag)            // Filter by longitude
            //->addFilter('postcode', $postcode)        // Filter by postcode

        $sourceData = $this->sourceRepository->getList($searchCriteria);
        return $sourceData->getItems();

Now you can use getSources() function to get the source data by sources name.

$sourceName = 'My Store';

$sourceData = $this->getSources($sourceName)

foreach ($sourceData as $source) {
    echo $sourceName->getName() // My Store
    echo $sourceName->getEnabled() // 1
    echo $sourceName->getDescription() // Test Description
    echo $sourceName->getLatitude() //  0.000000
    echo $sourceName->getLongitude() // 0.000000
    echo $sourceName->getPostcode() // 382415

You can also use other filters as described in code.

Hope this will help you. Fill free to comment below if you have any queries.

Thank you for reading. Keep sharing 🙂