With the Magento version, 2.3 Multi-source inventory (MSI) Was Introduced.
With the default Magento MSI, you can create multiple sources which contain stores data. like source_code, name, warehouse_id, latitude, longitude, etc.
You can get all sources which are added by Magento admin by class Magento\InventoryApi\Api\SourceRepositoryInterface
Here is the source collection factory class: Magento\Inventory\Model\ResourceModel\Source\CollectionFactory
Here are the sample model class to get the list of sources
<?php namespace Mageprince\Testing\Model; class SourceListModel { private $sourceRepository; public function __construct( ... \Magento\InventoryApi\Api\SourceRepositoryInterface $sourceRepository, ... ) { $this->sourceRepository = $sourceRepository; } public function getSourcesList() { $sourceData = $this->sourceRepository->getList(); return $sourceData->getItems(); } }
Now you can use getSourcesList()
function to get all available sources
$sourceList = $this->getSourceList(); foreach ($sourceList as $source) { print_r($source->getData()); }
Here is the sample output
Array ( [source_code] => Brisbane [name] => Brisbane [warehouse_id] => 10 [enabled] => 1 [latitude] => -27.469783 [longitude] => 153.027336 [country_id] => AU [region_id] => 488 [region] => Queensland [city] => Brisbane City [street] => 189 Elizabeth St [postcode] => 4000 [use_default_carrier_config] => 1 [carrier_links] => Array ( ) ) Array ( [source_code] => Chadstone [name] => Chadstone [warehouse_id] => 8 [enabled] => 1 [latitude] => -37.887295 ... ... ...
You can also filter source collection by SearchCriteriaBuilder
Here is the sample model to get source list by SearchCriteriaBuilder
<?php namespace Mageprince\Testing\Model; class SourceListModel { 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 getSourcesList() { $searchCriteria = $this->searchCriteriaBuilder ->addFilter('source_code', 'My Store') ->create(); $sourceData = $this->sourceRepository->getList($searchCriteria); return $sourceData->getItems(); } }
Hope this will help you to get a list of sources.
Keep sharing because sharing is caring … 🙂