/** * Get stock items by product ID (mage). * @param int $prodId * @return \Magento\CatalogInventory\Api\Data\StockItemInterface[] */ private function _getStockItems($prodId) { /** @var StockItemCriteriaInterface $crit */ $crit = $this->_manObj->create(StockItemCriteriaInterface::class); $crit->setProductsFilter($prodId); /* get all stock items and create map by stock id (warehouse ID)*/ /** @var \Magento\CatalogInventory\Api\Data\StockItemCollectionInterface $list */ $list = $this->_mageRepoStockItem->getList($crit); $result = $list->getItems(); return $result; }
/** * @inheritdoc */ public function getLowStockItems($websiteId, $qty, $currentPage = 1, $pageSize = 0) { $criteria = $this->criteriaFactory->create(); $criteria->setLimit($currentPage, $pageSize); $criteria->setWebsiteFilter($websiteId); $criteria->setQtyFilter('>=', $qty); $criteria->addField('qty'); return $this->stockItemRepository->getList($criteria); }
/** * 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) { $key = $scopeId . '/' . $productId; if (!isset($this->stockItems[$key])) { $criteria = $this->stockItemCriteriaFactory->create(); $criteria->setProductsFilter($productId); $collection = $this->stockItemRepository->getList($criteria); $stockItem = current($collection->getItems()); if ($stockItem && $stockItem->getItemId()) { $this->stockItems[$key] = $stockItem; } else { return $this->stockItemFactory->create(); } } return $this->stockItems[$key]; }
/** * @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; }
private function _createMageStockItem($stockId, $prodId) { /* check if stock item already exist */ /** @var $criteria \Magento\CatalogInventory\Api\StockItemCriteriaInterface */ $criteria = $this->_manObj->create(\Magento\CatalogInventory\Api\StockItemCriteriaInterface::class); $criteria->addFilter('byStock', StockItemInterface::STOCK_ID, $stockId); $criteria->addFilter('byProduct', StockItemInterface::PRODUCT_ID, $prodId); $list = $this->_repoMageStockItem->getList($criteria); $items = $list->getItems(); /** @var $stockItem StockItemInterface */ if (count($items)) { $stockItem = reset($items); $result = $stockItem->getItemId(); } else { $stockItem = $this->_manObj->create(StockItemInterface::class); $stockItem->setStockId($stockId); $stockItem->setProductId($prodId); $saved = $this->_repoMageStockItem->save($stockItem); $result = $saved->getItemId(); } return $result; }