Get Stock Data by given stock id for Multi-Source Inventory Magento 2.
Interface Magento\InventoryApi\Api\StockRepositoryInterface used to get stock repository information related data.
get(int $stockId) method used to fetch Stock data by stock id in MSI module Magento 2.3 and Higher version.
Pass Stock Id as Integer to fetch Stock Information.
<? namespace Jesadiya\StockRepository\Model; use Exception; use Psr\Log\LoggerInterface; use Magento\InventoryApi\Api\StockRepositoryInterface; class Stock { /** * @var StockRepositoryInterface */ private $stockRepository; /** * @var LoggerInterface */ private $logger; public function __construct( StockRepositoryInterface $stockRepository, LoggerInterface $logger ) { $this->stockRepository = $stockRepository; $this->logger = $logger; } /** * @return StockInterface|null */ public function getStockData() { $stockData = null; try { $stockId = 1; // For Default Stock $stockData = $this->stockRepository->get($stockId); } catch (Exception $exception) { $this->logger->error($exception->getMessage()); } return $stockData; } }
Call function to fetch Stock Data,
$stockData = $this->getStockData();
Output
echo $stockData->getName(); echo $stockData->getStockId(); var_dump($stockData->getExtensionAttributes()->getSalesChannels());
You got the Stock name and Extension attribute related to MSI stocks.