Exemplo n.º 1
0
 /**
  * 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
     if (empty($variationChannelOptions) && $this->isComponentModeBuy() && ($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);
         /** @var Ess_M2ePro_Model_Mysql4_Order_Matching_Collection $matchingCollection */
         $matchingCollection = Mage::getModel('M2ePro/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 = Mage::getModel('M2ePro/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.');
     }
 }
 public function unassignProductAction()
 {
     $orderItemId = $this->getRequest()->getPost('order_item_id');
     /** @var $orderItem Ess_M2ePro_Model_Order_Item */
     $orderItem = Mage::getModel('M2ePro/Order_Item')->load($orderItemId);
     $this->getResponse()->setHeader('Content-type', 'application/json');
     if (!$orderItem->getId()) {
         $this->getResponse()->setBody(json_encode(array('error' => Mage::helper('M2ePro')->__('Please specify Required Options.'))));
         return;
     }
     $channelOptions = $orderItem->getChildObject()->getVariationChannelOptions();
     if (!empty($channelOptions)) {
         $hash = Ess_M2ePro_Model_Order_Matching::generateHash($channelOptions);
         /** @var $connWrite Varien_Db_Adapter_Pdo_Mysql */
         $connWrite = Mage::getSingleton('core/resource')->getConnection('core_write');
         $connWrite->delete(Mage::getResourceModel('M2ePro/Order_Matching')->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->getResponse()->setBody(json_encode(array('success' => Mage::helper('M2ePro')->__('Item was successfully Unmapped.'))));
 }