Using Source code, you can get details of the Manage source in the Magento 2 MSI module.
Default Source is the out-of-the-box source in the MSI module.
You can fetch lit of information from the source_code in MSI using SourceRepositoryInterface of Inventory-API module.
Example of retrieving the default source data by source_code equals ‘default’.
<?php
namespace Jesadiya\SourceData\Model;
use Exception;
use Psr\Log\LoggerInterface;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
class SourceInfo
{
/**
* @var SourceRepositoryInterface
*/
private $sourceRepository;
/**
* @var LoggerInterface
*/
private $logger;
public function __construct(
SourceRepositoryInterface $sourceRepository,
LoggerInterface $logger
) {
$this->sourceRepository = $sourceRepository;
$this->logger = $logger;
}
/**
* Get source details
*
* @return SourceInterface|null
*/
public function getSourcesDetails()
{
$sourceInfo = null;
$sourceCode = 'default';
try {
$sourceInfo = $this->sourceRepository->get($sourceCode);
} catch (Exception $exception) {
$this->logger->error($exception->getMessage());
$sourceInfo = null;
}
return $sourceInfo;
}
}
You can call a function to fetch information of Source,
$sourceData = $this->getSourcesDetails();
if ($sourceData) {
foreach ($sourceData as $source) {
echo $source['name']; // Default
echo $source['enabled']; // 1
echo $source['description']; //
echo $source['latitude']; // 0.000000
echo $source['longitude']; // 0.000000
echo $source['postcode']; // 78701
}
}
Using the Source code of Inventory, you can get the details information of Source in MSI.
You need to use get(‘source_code’) method of $this->sourceRepository Object to fetch specific source details.
