Inventory Stock in the Multi-Source (MSI) module are used for mapping a sales channel to source locations in Magento 2.
You can see a list of available Stocks from the Stores -> Inventory -> Stocks link.
You can get a list of stocks available for the system using the StockRepositoryInterface of Inventory API module.
I have created a simple Model class to get all the stocks,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | <?php namespace Jesadiya\GetStocks\Model; use Exception; use Psr\Log\LoggerInterface; use Magento\Framework\Api\SearchCriteriaBuilder; use Magento\InventoryApi\Api\Data\StockInterface; use Magento\InventoryApi\Api\StockRepositoryInterface; class StocksList { /** * @var SearchCriteriaBuilder */ private $searchCriteriaBuilder; /** * @var StockRepositoryInterface */ private $stockRepository; /** * @var LoggerInterface */ private $logger; public function __construct( SearchCriteriaBuilder $searchCriteriaBuilder, StockRepositoryInterface $stockRepository, LoggerInterface $logger ) { $this->searchCriteriaBuilder = $searchCriteriaBuilder; $this->stockRepository = $stockRepository; $this->logger = $logger; } /** * @return StockInterface[]|null */ public function getStocksList() { $searchCriteria = $this->searchCriteriaBuilder->create(); $stockInfo = null; try { $stockData = $this->stockRepository->getList($searchCriteria); if ($stockData->getTotalCount()) { $stockInfo = $stockData->getItems(); } } catch (Exception $exception) { $this->logger->error($exception->getMessage()); } return $stockInfo; } } |
Call function to fetch a list of stocks,
1 2 3 4 5 6 7 8 9 | $stocksList = $this->getStocksList(); if ($stocksList) { foreach ($stockData->getItems() as $stocks) { foreach ($stocks as $stock) { echo $stock['stock_id']; // 1 echo $stock['name']; // Default } } } |
Using Inventory Stocks you can retrieve the list of stocks available in the system.