stockRepository = $stockRepository; $this->stockFactory = $stockFactory; $this->stockItemRepository = $stockItemRepository; $this->stockItemFactory = $stockItemFactory; $this->stockStatusRepository = $stockStatusRepository; $this->stockStatusFactory = $stockStatusFactory; $this->stockCriteriaFactory = $stockCriteriaFactory; $this->stockItemCriteriaFactory = $stockItemCriteriaFactory; $this->stockStatusCriteriaFactory = $stockStatusCriteriaFactory; } /** * @param int|null $scopeId * @return \Magento\CatalogInventory\Api\Data\StockInterface */ public function getStock($scopeId) { $stock = $this->getStockRegistryStorage()->getStock($scopeId); if (null === $stock) { $criteria = $this->stockCriteriaFactory->create(); $criteria->setScopeFilter($scopeId); $collection = $this->stockRepository->getList($criteria); $stock = current($collection->getItems()); if ($stock && $stock->getStockId()) { $this->getStockRegistryStorage()->setStock($scopeId, $stock); } else { $stock = $this->stockFactory->create(); } } return $stock; } /** * @param int $productId * @param int $scopeId * @return \Magento\CatalogInventory\Api\Data\StockItemInterface */ public function getStockItem($productId, $scopeId) { $stockItem = $this->getStockRegistryStorage()->getStockItem($productId, $scopeId); if (null === $stockItem) { $criteria = $this->stockItemCriteriaFactory->create(); $criteria->setProductsFilter($productId); $collection = $this->stockItemRepository->getList($criteria); $stockItem = current($collection->getItems()); if ($stockItem && $stockItem->getItemId()) { $this->getStockRegistryStorage()->setStockItem($productId, $scopeId, $stockItem); } else { $stockItem = $this->stockItemFactory->create(); } } return $stockItem; } /** * @param int $productId * @param int $scopeId * @return \Magento\CatalogInventory\Api\Data\StockStatusInterface */ public function getStockStatus($productId, $scopeId) { $stockStatus = $this->getStockRegistryStorage()->getStockStatus($productId, $scopeId); if (null === $stockStatus) { $criteria = $this->stockStatusCriteriaFactory->create(); $criteria->setProductsFilter($productId); $criteria->setScopeFilter($scopeId); $collection = $this->stockStatusRepository->getList($criteria); $stockStatus = current($collection->getItems()); if ($stockStatus && $stockStatus->getProductId()) { $this->getStockRegistryStorage()->setStockStatus($productId, $scopeId, $stockStatus); } else { $stockStatus = $this->stockStatusFactory->create(); } } return $stockStatus; } /** * @return StockRegistryStorage */ private function getStockRegistryStorage() { if (null === $this->stockRegistryStorage) { $this->stockRegistryStorage = \Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\CatalogInventory\Model\StockRegistryStorage::class); } return $this->stockRegistryStorage; } }