/** * Detect current stock and get appropriate stock item. * * @param \Magento\CatalogInventory\Model\StockRegistryProvider $subject * @param \Closure $proceed * @param int $productId * @param int $scopeId * @return \Magento\CatalogInventory\Api\Data\StockItemInterface */ public function aroundGetStockItem(\Magento\CatalogInventory\Model\StockRegistryProvider $subject, \Closure $proceed, $productId, $scopeId) { $result = $this->_storageStockRegistry->getStockItem($productId, $scopeId); if (null === $result) { $criteria = $this->_factoryStockItemCrit->create(); $criteria->setProductsFilter($productId); $stockId = $this->_toolStockManager->getCurrentStockId(); $criteria->setStockFilter($stockId); $collection = $this->_repoStockItem->getList($criteria); $result = current($collection->getItems()); if ($result && $result->getItemId()) { $this->_storageStockRegistry->setStockItem($productId, $scopeId, $result); } else { $result = $this->_factoryStockItem->create(); } } return $result; }
/** * @param int $productId * @param int $scopeId * @return \Magento\CatalogInventory\Api\Data\StockItemInterface */ public function getStockItem($productId, $scopeId) { $stockItem = $this->stockRegistryStorage->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->stockRegistryStorage->setStockItem($productId, $scopeId, $stockItem); } else { $stockItem = $this->stockItemFactory->create(); } } return $stockItem; }