Example #1
0
 /**
  * Subtract product qtys from stock.
  * Return array of items that require full save
  *
  * @param string[] $items
  * @param int $websiteId
  * @return StockItemInterface[]
  * @throws \Magento\Framework\Exception\LocalizedException
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function registerProductsSale($items, $websiteId = null)
 {
     //if (!$websiteId) {
     $websiteId = $this->stockConfiguration->getDefaultWebsiteId();
     //}
     $this->getResource()->beginTransaction();
     $lockedItems = $this->getResource()->lockProductsStock(array_keys($items), $websiteId);
     $fullSaveItems = $registeredItems = [];
     foreach ($lockedItems as $lockedItemRecord) {
         $productId = $lockedItemRecord['product_id'];
         /** @var StockItemInterface $stockItem */
         $orderedQty = $items[$productId];
         $stockItem = $this->stockRegistryProvider->getStockItem($productId, $websiteId);
         $canSubtractQty = $stockItem->getItemId() && $this->canSubtractQty($stockItem);
         if (!$canSubtractQty || !$this->stockConfiguration->isQty($lockedItemRecord['type_id'])) {
             continue;
         }
         if (!$stockItem->hasAdminArea() && !$this->stockState->checkQty($productId, $orderedQty, $stockItem->getWebsiteId())) {
             $this->getResource()->commit();
             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, $stockItem->getWebsiteId()) || $this->stockState->verifyNotification($productId, $stockItem->getWebsiteId())) {
             $fullSaveItems[] = $stockItem;
         }
     }
     $this->getResource()->correctItemsQty($registeredItems, $websiteId, '-');
     $this->getResource()->commit();
     return $fullSaveItems;
 }
 /**
  * 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;
 }