private function clearOrder(\Ess\M2ePro\Model\Order $order) { $order->setMagentoOrder(null); $order->setData('magento_order_id', null); $order->save(); $order->getItemsCollection()->walk('setProduct', array(null)); }
private function performAction($action, $newState) { $productsAffectedCount = 0; $productsDeletedCount = 0; $productsExistCount = 0; $stockItems = array(); foreach ($this->order->getItemsCollection()->getItems() as $item) { if ($action == self::ACTION_SUB) { $qty = $item->getChildObject()->getQtyPurchased(); $item->setData('qty_reserved', $qty); } else { $qty = $item->getQtyReserved(); } $products = $this->getItemProductsByAction($item, $action); if (count($products) == 0) { continue; } foreach ($products as $key => $productId) { /** @var $magentoProduct \Ess\M2ePro\Model\Magento\Product */ $magentoProduct = $this->modelFactory->getObject('Magento\\Product')->setStoreId($this->order->getStoreId())->setProductId($productId); if (!$magentoProduct->exists()) { $productsDeletedCount++; unset($products[$key]); continue; } $productsExistCount++; if (!isset($stockItems[$productId])) { $stockItems[$productId] = $magentoProduct->getStockItem(); } $stockItem = $stockItems[$productId]; $this->stockItem->setStockItem($stockItem); if (!$this->changeProductQty($magentoProduct, $this->stockItem, $action, $qty)) { if ($action == self::ACTION_SUB) { unset($products[$key]); } continue; } if ($action == self::ACTION_ADD) { unset($products[$key]); } $productsAffectedCount++; $this->transaction->addObject($this->stockItem->getStockItem()); } $item->setReservedProducts($products); $this->transaction->addObject($item); } unset($stockItems); if ($productsExistCount == 0 && $productsDeletedCount == 0) { $this->order->setData('reservation_state', self::STATE_UNKNOWN)->save(); throw new \Ess\M2ePro\Model\Exception\Logic('The Order Item(s) was not Mapped to Magento Product(s) or Mapped incorrect.'); } if ($productsExistCount == 0) { $this->order->setData('reservation_state', self::STATE_UNKNOWN)->save(); throw new \Ess\M2ePro\Model\Exception\Logic('Product(s) does not exist.'); } if ($productsDeletedCount > 0) { $this->order->addWarningLog('QTY for %number% Product(s) was not changed. Reason: Product(s) does not exist.', array('!number' => $productsDeletedCount)); } if ($productsAffectedCount <= 0) { return; } $this->order->setData('reservation_state', $newState); if ($newState == self::STATE_PLACED && !$this->getFlag('order_reservation')) { $this->order->setData('reservation_start_date', $this->getHelper('Data')->getCurrentGmtDate()); } $this->transaction->addObject($this->order); $this->transaction->save(); }