Пример #1
0
 private function performAction($action, $newState)
 {
     /** @var $transaction Mage_Core_Model_Resource_Transaction */
     $transaction = Mage::getModel('core/resource_transaction');
     $productsAffectedCount = 0;
     $productsDeletedCount = 0;
     $productsExistCount = 0;
     $stockItems = array();
     foreach ($this->order->getItemsCollection()->getItems() as $item) {
         $qty = $item->getChildObject()->getQtyPurchased();
         $products = $this->getItemProductsByAction($item, $action);
         if (count($products) == 0) {
             continue;
         }
         foreach ($products as $key => $productId) {
             /** @var $magentoProduct Ess_M2ePro_Model_Magento_Product */
             $magentoProduct = Mage::getModel('M2ePro/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];
             /** @var $magentoStockItem Ess_M2ePro_Model_Magento_Product_StockItem */
             $magentoStockItem = Mage::getSingleton('M2ePro/Magento_Product_StockItem');
             $magentoStockItem->setStockItem($stockItem);
             if (!$this->changeProductQty($magentoProduct, $magentoStockItem, $action, $qty)) {
                 continue;
             }
             if ($action == self::ACTION_ADD) {
                 unset($products[$key]);
             }
             $productsAffectedCount++;
             if ($this->getFlag('order_reservation')) {
                 $magentoStockItem->getStockItem()->setData(Ess_M2ePro_Helper_Data::CUSTOM_IDENTIFIER . '_order_reservation', true);
             }
             $transaction->addObject($magentoStockItem->getStockItem());
         }
         $item->setReservedProducts($products);
         $transaction->addObject($item);
     }
     unset($stockItems);
     if ($productsExistCount == 0 && $productsDeletedCount == 0) {
         $this->order->setData('reservation_state', self::STATE_UNKNOWN)->save();
         throw new LogicException('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 LogicException('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));
     }
     $this->order->setData('reservation_state', $newState);
     if ($newState == self::STATE_PLACED && !$this->getFlag('order_reservation')) {
         $this->order->setData('reservation_start_date', Mage::helper('M2ePro')->getCurrentGmtDate());
     }
     $transaction->addObject($this->order);
     $transaction->save();
 }