Get Inventory Source info by Source Name using Multi-Source (MSI) Magento 2.

Retrieve the Inventory Source Details info by Source Name using MSI module Magento 2 is pretty simple.

The source name is the visible entity in the Manage source grid with column ‘name’ to separate out the list of different sources in a system.

You can fetch details info from the column name in MSI using SourceRepositoryInterface and SearchCriteriaBuilder interface.

Magento MSI module natively supports the Default source and its name is “Default Source”.

Example of retrieving the default source data by name column.

<?php
namespace Jesadiya\SourceData\Model;

use Exception;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
use Psr\Log\LoggerInterface;

class SourceByName
{
    /**
     * @var SearchCriteriaBuilder
     */
    private $searchCriteriaBuilder;

    /**
     * @var SourceRepositoryInterface
     */
    private $sourceRepository;

    /**
     * @var LoggerInterface
     */
    private $logger;

    public function __construct(
        SearchCriteriaBuilder $searchCriteriaBuilder,
        SourceRepositoryInterface $sourceRepository,
        LoggerInterface $logger
    ) {
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->sourceRepository = $sourceRepository;
        $this->logger = $logger;
    }

    /**
     * Get source details
     *
     * @return SourceInterface[]|null
     */
    public function getSourcesDetails()
    {
        $searchCriteria = $this->searchCriteriaBuilder
            ->addFilter('name', 'Default Source')->create();
        $sourceInfo = null;
        try {
            $sourceData = $this->sourceRepository->getList($searchCriteria);
            if ($sourceData->getTotalCount()) {
                $sourceInfo = $sourceData->getItems();
            }
        } catch (Exception $exception) {
            $this->logger->error($exception->getMessage());
        }

        return $sourceInfo;
    }
}

You can call a function to fetch a list of information from the source,

$sources = $this->getSourcesDetails();
if ($sourceList) {
    foreach ($sources->getItems() as $sourceName) {
        echo $sourceName->getName() // Default
        echo $sourceName->getEnabled() // 1
        echo $sourceName->getDescription() // Default source
        echo $sourceName->getLatitude() //  0.000000
        echo $sourceName->getLongitude() // 0.000000
        echo $sourceName->getPostcode() // 78701
    }
}

This way, You can get the information of your Inventory Source by Source Name only.