/** * 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; }
/** * @param \Praxigento\Odoo\Data\Odoo\Inventory\Product\Warehouse\Lot[] $lots * @param \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem */ public function processLots($lots, $stockItem) { $qtyTotal = 0; $stockItemId = $stockItem->getItemId(); foreach ($lots as $lot) { $qtyTotal += $this->_subLot->processLot($stockItemId, $lot); } /* update stock item qty */ $stockItem->setQty($qtyTotal); $isInStock = $qtyTotal > 0; $stockItem->setIsInStock($isInStock); $this->_mageRepoStockItem->save($stockItem); /* cleanup extra lots */ $this->_subLot->cleanupLots($stockItemId, $lots); }
/** * 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; }
protected function setUp() { $this->objectManagerHelper = new ObjectManagerHelper($this); $this->product = $this->getMock('Magento\\Catalog\\Model\\Product', ['__wakeup', 'getIdBySku'], [], '', false); $this->product->expects($this->any())->method('getIdBySku')->willReturn($this->productId); //getIdBySku $this->productFactory = $this->getMock('Magento\\Catalog\\Model\\ProductFactory', ['create'], [], '', false); $this->productFactory->expects($this->any())->method('create')->will($this->returnValue($this->product)); $this->stock = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\Data\\StockInterface', ['__wakeup'], '', false); $this->stockItem = $this->getMockBuilder('Magento\\CatalogInventory\\Api\\Data\\StockItemInterface')->setMethods(['setProductId', 'getData', 'addData', 'getItemId'])->disableOriginalConstructor()->getMockForAbstractClass(); $this->stockStatus = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\Data\\StockStatusInterface', ['__wakeup'], '', false); $this->stockRegistryProvider = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Model\\Spi\\StockRegistryProviderInterface', ['getStock', 'getStockItem', 'getStockStatus'], '', false); $this->stockRegistryProvider->expects($this->any())->method('getStock')->will($this->returnValue($this->stock)); $this->stockRegistryProvider->expects($this->any())->method('getStockItem')->will($this->returnValue($this->stockItem)); $this->stockRegistryProvider->expects($this->any())->method('getStockStatus')->will($this->returnValue($this->stockStatus)); $this->stockItemRepository = $this->getMockForAbstractClass('\\Magento\\CatalogInventory\\Api\\StockItemRepositoryInterface', ['save'], '', false); $this->stockItemRepository->expects($this->any())->method('save')->will($this->returnValue($this->stockItem)); $this->stockRegistry = $this->objectManagerHelper->getObject('\\Magento\\CatalogInventory\\Model\\StockRegistry', ['stockRegistryProvider' => $this->stockRegistryProvider, 'productFactory' => $this->productFactory, 'stockItemRepository' => $this->stockItemRepository]); }
public function testGetStockItem() { $this->stockItemCriteriaFactory->expects($this->once())->method('create')->willReturn($this->stockItemCriteria); $this->stockItemCriteria->expects($this->once())->method('setProductsFilter')->willReturn(null); $stockItemCollection = $this->getMock('\\Magento\\CatalogInventory\\Model\\ResourceModel\\Stock\\Item\\Collection', ['getFirstItem', '__wakeup', 'getItems'], [], '', false); $stockItemCollection->expects($this->once())->method('getItems')->willReturn([$this->stockItem]); $this->stockItemRepository->expects($this->once())->method('getList')->willReturn($stockItemCollection); $this->stockItem->expects($this->once())->method('getItemId')->willReturn(true); $this->assertEquals($this->stockItem, $this->stockRegistryProvider->getStockItem($this->productId, $this->scopeId)); }
/** * @inheritdoc */ public function updateStockItemBySku($productSku, \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem) { $productId = $this->resolveProductId($productSku); $websiteId = $stockItem->getWebsiteId() ?: null; $origStockItem = $this->getStockItem($productId, $websiteId); $data = $stockItem->getData(); if ($origStockItem->getItemId()) { unset($data['item_id']); } $origStockItem->addData($data); $origStockItem->setProductId($productId); return $this->stockItemRepository->save($origStockItem)->getItemId(); }
private function _createQty($stockItemId, $lotId, $total) { $tbl = Quantity::ENTITY_NAME; $bind = [Quantity::ATTR_STOCK_ITEM_REF => $stockItemId, Quantity::ATTR_LOT_REF => $lotId, Quantity::ATTR_TOTAL => $total]; $result = $this->_repoGeneric->addEntity($tbl, $bind); /* update qty of the stock item */ $stockItem = $this->_repoMageStockItem->get($stockItemId); $qty = $stockItem->getQty(); $qty += $total; $stockItem->setQty($qty); $stockItem->setIsInStock(true); $this->_repoMageStockItem->save($stockItem); 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; }
/** * Prepare stock item data for save * * @param \Magento\Catalog\Model\Product $product * @return $this */ protected function saveStockItemData($product) { $stockItemData = $product->getStockData(); $stockItemData['product_id'] = $product->getId(); if (!isset($stockItemData['website_id'])) { $stockItemData['website_id'] = $this->stockConfiguration->getDefaultScopeId(); } $stockItemData['stock_id'] = $this->stockRegistry->getStock($stockItemData['website_id'])->getStockId(); foreach ($this->paramListToCheck as $dataKey => $configPath) { if (null !== $product->getData($configPath['item']) && null === $product->getData($configPath['config'])) { $stockItemData[$dataKey] = false; } } $originalQty = $product->getData('stock_data/original_inventory_qty'); if (strlen($originalQty) > 0) { $stockItemData['qty_correction'] = (isset($stockItemData['qty']) ? $stockItemData['qty'] : 0) - $originalQty; } // todo resolve issue with builder and identity field name $stockItem = $this->stockRegistry->getStockItem($stockItemData['product_id'], $stockItemData['website_id']); $stockItem->addData($stockItemData); $this->stockItemRepository->save($stockItem); return $this; }
public function testSave() { $this->stockItemRepository->expects($this->any())->method('save')->willReturn($this->item); $this->assertEquals($this->item, $this->item->save()); }