/** * Update stock item on the stock and distribute qty by lots. * * @param \Magento\CatalogInventory\Model\StockManagement $subject * @param \Closure $proceed * @param array $items * @param int $websiteId is not used * @throws \Magento\Framework\Exception\LocalizedException * @return null */ public function aroundRegisterProductsSale(\Magento\CatalogInventory\Model\StockManagement $subject, \Closure $proceed, array $items, $websiteId) { /* This code is moved from original 'registerProductsSale' method. */ /* replace websiteId by stockId */ $stockId = $this->_manStock->getCurrentStockId(); $def = $this->_manTrans->begin(); $lockedItems = $this->_resourceStock->lockProductsStock(array_keys($items), $stockId); $fullSaveItems = $registeredItems = []; foreach ($lockedItems as $lockedItemRecord) { $productId = $lockedItemRecord['product_id']; $orderedQty = $items[$productId]; /** @var \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem */ $stockItem = $this->_providerStockRegistry->getStockItem($productId, $stockId); $stockItemId = $stockItem->getItemId(); $canSubtractQty = $stockItemId && $this->_canSubtractQty($stockItem); if (!$canSubtractQty || !$this->_configStock->isQty($lockedItemRecord['type_id'])) { continue; } if (!$stockItem->hasAdminArea() && !$this->_stockState->checkQty($productId, $orderedQty)) { $this->_manTrans->rollback($def); throw new \Magento\Framework\Exception\LocalizedException(__('Not all of your products are available in the requested quantity.')); } if ($this->_canSubtractQty($stockItem)) { $stockItem->setQty($stockItem->getQty() - $orderedQty); } $registeredItems[$productId] = $orderedQty; if (!$this->_stockState->verifyStock($productId) || $this->_stockState->verifyNotification($productId)) { $fullSaveItems[] = $stockItem; } } $this->_resourceStock->correctItemsQty($registeredItems, $stockId, '-'); $this->_manTrans->commit($def); return $fullSaveItems; }
/** * @param string $productSku * @param int $websiteId * @return \Magento\CatalogInventory\Api\Data\StockItemInterface * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function getStockItemBySku($productSku, $websiteId = null) { //if (!$websiteId) { $websiteId = $this->stockConfiguration->getDefaultWebsiteId(); //} $productId = $this->resolveProductId($productSku); return $this->stockRegistryProvider->getStockItem($productId, $websiteId); }
/** * @param int $productId * @param float $itemQty * @param float $qtyToCheck * @param float $origQty * @param int $scopeId * @return int */ public function checkQuoteItemQty($productId, $itemQty, $qtyToCheck, $origQty, $scopeId = null) { if ($scopeId === null) { $scopeId = $this->stockConfiguration->getDefaultScopeId(); } $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId); return $this->stockStateProvider->checkQuoteItemQty($stockItem, $itemQty, $qtyToCheck, $origQty); }
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)); }
/** * Get back to stock (when order is canceled or whatever else) * * @param int $productId * @param float $qty * @param int $scopeId * @return bool */ public function backItemQty($productId, $qty, $scopeId = null) { //if (!$scopeId) { $scopeId = $this->stockConfiguration->getDefaultScopeId(); //} $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId); if ($stockItem->getItemId() && $this->stockConfiguration->isQty($this->getProductType($productId))) { if ($this->canSubtractQty($stockItem)) { $stockItem->setQty($stockItem->getQty() + $qty); } if ($this->stockConfiguration->getCanBackInStock($stockItem->getStoreId()) && $stockItem->getQty() > $stockItem->getMinQty()) { $stockItem->setIsInStock(true); $stockItem->setStockStatusChangedAutomaticallyFlag(true); } $stockItem->save(); } return true; }
/** * Process Parents by child * * @param int $productId * @param int $websiteId * @return $this */ protected function processParents($productId, $websiteId) { $parentIds = []; foreach ($this->getProductTypeInstances() as $typeInstance) { /* @var $typeInstance AbstractType */ $parentIds = array_merge($parentIds, $typeInstance->getParentIdsByChild($productId)); } if (!$parentIds) { return $this; } foreach ($parentIds as $parentId) { $item = $this->stockRegistryProvider->getStockItem($parentId, $websiteId); $status = \Magento\CatalogInventory\Model\Stock\Status::STATUS_IN_STOCK; $qty = 0; if ($item->getItemId()) { $status = $item->getIsInStock(); $qty = $item->getQty(); } $this->processChildren($parentId, $websiteId, $qty, $status); } }