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

We can use Repository \Magento\InventoryApi\Api\SourceRepositoryInterface to get the source by code

Here is the sample model class

<?php

namespace Mageprince\Testing\Model;

class SourceModel
{
    private $sourceRepository;

    public function __construct(
        ...
        \Magento\InventoryApi\Api\SourceRepositoryInterface $sourceRepository
        ...
    ) {
        $this->sourceRepository = $sourceRepository;
    }

    public function getSourcesByCode($sourceCode)
    {
        return $this->sourceRepository->get($storeCode);
    }
}

Now you can use getSourcesByCode() function to get the source data by code.

$sourceCode = 'test_store';
$sourceData = $this->getSourcesByCode($sourceCode);

echo $sourceData->getSourceCode();  // test_store
echo $sourceData->isEnabled();      // 1
echo $sourceData->getDescription(); // Test Description
echo $sourceData->getLatitude();    // -37.888995
echo $sourceData->getLongitude();   // 145.083531
echo $sourceData->getPostcode();    // 6589

Hope this will help you. Keep Sharing.

Happy Coding 🙂