public function execute() { $orderItemId = $this->getRequest()->getPost('order_item_id'); /** @var $orderItem \Ess\M2ePro\Model\Order\Item */ $orderItem = $this->activeRecordFactory->getObjectLoaded('Order\\Item', $orderItemId); if (!$orderItem->getId()) { $this->setJsonContent(array('error' => $this->__('Please specify Required Options.'))); return $this->getResult(); } $channelOptions = $orderItem->getChildObject()->getVariationChannelOptions(); if (!empty($channelOptions)) { $hash = \Ess\M2ePro\Model\Order\Matching::generateHash($channelOptions); $connWrite = $this->resourceConnection->getConnection(); $connWrite->delete($this->activeRecordFactory->getObject('Order\\Matching')->getResource()->getMainTable(), array('product_id = ?' => $orderItem->getProductId(), 'hash = ?' => $hash)); } $orderItem->unassignProduct(); $orderItem->getOrder()->getLog()->setInitiator(\Ess\M2ePro\Helper\Data::INITIATOR_USER); $orderItem->getOrder()->addSuccessLog('Item "%title%" was successfully Unmapped.', array('title' => $orderItem->getChildObject()->getTitle())); $this->setJsonContent(array('success' => $this->__('Item was successfully Unmapped.'))); return $this->getResult(); }
/** * Associate order item variation with options of magento product * * @throws \LogicException * @throws \Exception */ private function associateVariationWithOptions() { $variationChannelOptions = $this->getChildObject()->getVariationChannelOptions(); $magentoProduct = $this->getMagentoProduct(); // do nothing for amazon & buy order item, if it is mapped to product with required options, // but there is no information available about sold variation //TODO if (empty($variationChannelOptions) && $this->getComponentMode() == 'buy' && ($magentoProduct->isStrictVariationProduct() || $magentoProduct->isProductWithVariations())) { return; } $existOptions = $this->getAssociatedOptions(); $existProducts = $this->getAssociatedProducts(); if (count($existProducts) == 1 && ($magentoProduct->isGroupedType() || $magentoProduct->isConfigurableType())) { // grouped and configurable products can have only one associated product mapped with sold variation // so if count($existProducts) == 1 - there is no need for further actions return; } if (!empty($variationChannelOptions)) { $matchingHash = \Ess\M2ePro\Model\Order\Matching::generateHash($variationChannelOptions); $matchingCollection = $this->activeRecordFactory->getObject('Order\\Matching')->getCollection(); $matchingCollection->addFieldToFilter('product_id', $this->getProductId()); $matchingCollection->addFieldToFilter('component', $this->getComponentMode()); $matchingCollection->addFieldToFilter('hash', $matchingHash); /** @var $matching \Ess\M2ePro\Model\Order\Matching */ $matching = $matchingCollection->getFirstItem(); if ($matching->getId()) { $productDetails = $matching->getOutputVariationOptions(); $this->setAssociatedProducts($productDetails['associated_products']); $this->setAssociatedOptions($productDetails['associated_options']); $this->save(); return; } } $magentoOptions = $this->prepareMagentoOptions($magentoProduct->getVariationInstance()->getVariationsTypeRaw()); $variationProductOptions = $this->getChildObject()->getVariationProductOptions(); /** @var $optionsFinder \Ess\M2ePro\Model\Order\Item\OptionsFinder */ $optionsFinder = $this->modelFactory->getObject('Order\\Item\\OptionsFinder'); $optionsFinder->setProductId($magentoProduct->getProductId()); $optionsFinder->setProductType($magentoProduct->getTypeId()); $optionsFinder->setChannelOptions($variationProductOptions); $optionsFinder->setMagentoOptions($magentoOptions); $productDetails = $optionsFinder->getProductDetails(); if (!isset($productDetails['associated_options'])) { return; } $existOptionsIds = array_keys($existOptions); $foundOptionsIds = array_keys($productDetails['associated_options']); if (count($existOptions) == 0 && count($existProducts) == 0) { // options mapping invoked for the first time, use found options $this->setAssociatedOptions($productDetails['associated_options']); if (isset($productDetails['associated_products'])) { $this->setAssociatedProducts($productDetails['associated_products']); } if ($optionsFinder->hasFailedOptions()) { throw new \Ess\M2ePro\Model\Exception\Logic(sprintf('Product Option(s) "%s" not found.', implode(', ', $optionsFinder->getFailedOptions()))); } $this->save(); return; } if (count(array_diff($foundOptionsIds, $existOptionsIds)) > 0) { // options were already mapped, but not all of them throw new \Ess\M2ePro\Model\Exception\Logic('Selected Options do not match the Product Options.'); } }