private function initializeQuoteItems() { foreach ($this->proxyOrder->getItems() as $item) { $this->clearQuoteItemsCache(); /** @var $quoteItemBuilder Ess_M2ePro_Model_Magento_Quote_Item */ $quoteItemBuilder = Mage::getModel('M2ePro/Magento_Quote_Item'); $quoteItemBuilder->init($this->quote, $item); $product = $quoteItemBuilder->getProduct(); $request = $quoteItemBuilder->getRequest(); // ---------------------------- $productOriginalPrice = (double) $product->getPrice(); $price = $item->getBasePrice(); $product->setPrice($price); $product->setSpecialPrice($price); // ---------------------------- // see Mage_Sales_Model_Observer::substractQtyFromQuotes $this->quote->setItemsCount($this->quote->getItemsCount() + 1); $this->quote->setItemsQty((double) $this->quote->getItemsQty() + $request->getQty()); $result = $this->quote->addProduct($product, $request); if (is_string($result)) { throw new Exception($result); } $quoteItem = $this->quote->getItemByProduct($product); if ($quoteItem !== false) { $weight = $product->getTypeInstance()->getWeight(); if ($product->isConfigurable()) { // hack: for child product weight was not load $simpleProductId = $product->getCustomOption('simple_product')->getProductId(); $weight = Mage::getResourceModel('catalog/product')->getAttributeRawValue($simpleProductId, 'weight', 0); } $quoteItem->setStoreId($this->quote->getStoreId()); $quoteItem->setOriginalCustomPrice($item->getPrice()); $quoteItem->setOriginalPrice($productOriginalPrice); $quoteItem->setBaseOriginalPrice($productOriginalPrice); $quoteItem->setWeight($weight); $quoteItem->setNoDiscount(1); $giftMessageId = $quoteItemBuilder->getGiftMessageId(); if (!empty($giftMessageId)) { $quoteItem->setGiftMessageId($giftMessageId); } $quoteItem->setAdditionalData($quoteItemBuilder->getAdditionalData($quoteItem)); } } }
/** * Initialize quote items objects * * @throws Exception */ private function initializeQuoteItems() { foreach ($this->proxyOrder->getItems() as $item) { $this->clearQuoteItemsCache(); /** @var $quoteItemBuilder Ess_M2ePro_Model_Magento_Quote_Item */ $quoteItemBuilder = Mage::getModel('M2ePro/Magento_Quote_Item'); $quoteItemBuilder->init($this->quote, $item); $product = $quoteItemBuilder->getProduct(); $request = $quoteItemBuilder->getRequest(); // see Mage_Sales_Model_Observer::substractQtyFromQuotes $this->quote->setItemsCount($this->quote->getItemsCount() + 1); $this->quote->setItemsQty((double) $this->quote->getItemsQty() + $request->getQty()); $result = $this->quote->addProduct($product, $request); if (is_string($result)) { throw new Exception($result); } $quoteItem = $this->quote->getItemByProduct($product); if ($quoteItem !== false) { $quoteItem->setOriginalCustomPrice($item->getPrice()); $quoteItem->setNoDiscount(1); $quoteItem->setGiftMessageId($quoteItemBuilder->getGiftMessageId()); $quoteItem->setAdditionalData($quoteItemBuilder->getAdditionalData($quoteItem)); } } }
/** * Initialize quote items objects * * @throws Exception */ private function initializeQuoteItems() { foreach ($this->proxyOrder->getItems() as $item) { /** @var $quoteItemBuilder Ess_M2ePro_Model_Magento_Quote_Item */ $quoteItemBuilder = Mage::getModel('M2ePro/Magento_Quote_Item'); $quoteItemBuilder->setQuoteBuilder($this)->setProxyItem($item); $product = $quoteItemBuilder->getProduct(); $request = $quoteItemBuilder->getRequest(); $this->quote->setItemsCount($this->quote->getItemsCount() + 1); $this->quote->setItemsQty((double) $this->quote->getItemsQty() + $request->getQty()); $result = $this->quote->addProduct($product, $request); if (is_string($result)) { throw new Exception($result); } $quoteItem = $this->quote->getItemByProduct($product); if ($quoteItem !== false) { $quoteItem->setOriginalCustomPrice($quoteItemBuilder->getChannelCurrencyPrice()); $quoteItem->setNoDiscount(1); $quoteItem->setGiftMessageId($quoteItemBuilder->getGiftMessageId()); $quoteItem->setAdditionalData($quoteItemBuilder->getAdditionalData($quoteItem)); } } }
/** * Get QuoteItem by Product and request info * * @param Mage_Sales_Model_Quote $quote * @param Mage_Catalog_Model_Product $product * @param Varien_Object $requestInfo * @return Mage_Sales_Model_Quote_Item * @throw Mage_Core_Exception */ protected function _getQuoteItemByProduct(Mage_Sales_Model_Quote $quote, Mage_Catalog_Model_Product $product, Varien_Object $requestInfo) { $cartCandidates = $product->getTypeInstance(true)->prepareForCartAdvanced($requestInfo, $product, Mage_Catalog_Model_Product_Type_Abstract::PROCESS_MODE_FULL); /** * Error message */ if (is_string($cartCandidates)) { throw Mage::throwException($cartCandidates); } /** * If prepare process return one object */ if (!is_array($cartCandidates)) { $cartCandidates = array($cartCandidates); } /** @var $item Mage_Sales_Model_Quote_Item */ $item = null; foreach ($cartCandidates as $candidate) { if ($candidate->getParentProductId()) { continue; } $item = $quote->getItemByProduct($candidate); } if (is_null($item)) { $item = Mage::getModel("sales/quote_item"); } return $item; }
protected function _addProductsToQuote($productInCardList, $paramsArray) { $productInCardList = $this->_concatenateProductsInQuote($productInCardList); foreach ($productInCardList as $productItem) { $product = Mage::getModel('catalog/product')->load($productItem['id']); if (!$product->getId()) { throw new LogicException('Product does not exist. Probably it was deleted.'); } if ($product->getStatus() === Mage_Catalog_Model_Product_Status::STATUS_DISABLED) { throw new LogicException('Product is disabled.'); } $productTypeId = $product->getTypeId(); if ($productTypeId == Ess_M2ePro_Model_MagentoProduct::TYPE_GROUPED) { // Grouped product converted to assigned simple if (!isset($productItem['options'])) { throw new LogicException('The item does not have options. At the current version, Order Import for grouped product supports only multi variation listing.'); } $product = $this->_getRelatedProductFromGroupedForEbayData($product, $productItem); if (is_null($product)) { throw new LogicException('There is no associated products found for grouped product.'); } else { $productTypeId = $product->getTypeId(); } } $productItem['price'] = $this->_getConvertedPrice($productItem['price']); $request = new Varien_Object(); $request->setQty($productItem['qty']); switch ($productTypeId) { case Ess_M2ePro_Model_MagentoProduct::TYPE_SIMPLE: $haveRequiredOptionsInstant = $product->getTypeInstance(true)->hasRequiredOptions($product); $haveRequiredOptions = $product->hasRequiredOptions(); if ($haveRequiredOptions && !$product->getRequiredOptions()) { $haveRequiredOptions = false; // important: possible incorect behavior } if ($haveRequiredOptionsInstant || $haveRequiredOptions || $this->_checkSimpleProductHasRequiredCustomOptions($product)) { $customOptionsData = array(); if (isset($productItem['options']) && count($productItem['options'])) { // Have multivariation data for simple product // Use to set Custom Options data $customOptionsData = $this->_getCustomOptionsForEbayData($productItem); $this->_addNotifyMessage('Product has <b>Required Options</b>. Selected Options Values are taken from eBay Multi Variation Data'); } else { // No multivariation data, set first required option $customOptionsData = $this->_getRandomCustomOptions($productItem); $this->_addNotifyMessage('Product has <b>Required Options</b>. First option value is selected.'); } $request->setOptions($customOptionsData['options']); // Dec price for percent change (after apply percent price inc = need price) $productItem['price'] = $productItem['price'] / (1 + $customOptionsData['price_change_percent'] / 100); // Change for custom options price. price_change_fixed low that 0 when option inc price, more 0 - when inc $productItem['price'] += $customOptionsData['price_change_fixed'] / (1 + $customOptionsData['price_change_percent'] / 100); } // end of $haveRequriedOptions break; case Ess_M2ePro_Model_MagentoProduct::TYPE_CONFIGURABLE: if (!isset($productItem['options'])) { throw new LogicException('The item does not have options. At the current version, Order Import for configurable product supports only multi variation listing.'); } $configurableOptions = $this->_getConfigurableAttributeForEbayData($productItem); // Set selected attributes $request->setSuperAttribute($configurableOptions['options']); // Each option value can change total price value, remove // this changes for equal: order price = eBay sold price $productItem['price'] += $configurableOptions['price_change']; break; case Ess_M2ePro_Model_MagentoProduct::TYPE_BUNDLE: if (!isset($productItem['options'])) { throw new LogicException('The item does not have options. At the current version, Order Import for bundle product supports only multi variation listing.'); } $bundleOptions = $this->_getBundleOptionsForEbayData($productItem); $request->setBundleOption($bundleOptions['options']); // $bundleQty = array(); // foreach ($bundleOptions['options'] as $optionId => $optionValue) { // $bundleQty[$optionId] = $productItem['qty']; // } // $request->setBundleOptionQty($bundleQty); // $request->setQty(1); $this->_addNotifyMessage('Price for Bundle item is taken from Magento store.'); break; default: throw new LogicException('At the current version, Order Import does not support product type: ' . $productTypeId . ''); } $product->setPrice($productItem['price']); $product->setSpecialPrice($productItem['price']); $this->_initProductTaxClassId($product, $paramsArray['taxPercent']); $result = $this->_quote->addProduct($product, $request); if (is_string($result)) { throw new Exception($result); } // TODO: ugly hack //if ($productTypeId == Ess_M2ePro_Model_MagentoProduct::TYPE_BUNDLE || // Ess_M2ePro_Model_MagentoProduct::TYPE_CONFIGURABLE || // Ess_M2ePro_Model_MagentoProduct::TYPE_GROUPED) { foreach ($paramsArray['products'] as $tempProduct) { if ($tempProduct['id'] == $product->getId()) { $tempQuoteItem = $this->_quote->getItemByProduct($product); if ($tempQuoteItem !== false) { $tempQuoteItem->setNoDiscount(1); $tempQuoteItem->setOriginalCustomPrice($this->_getConvertedPrice($tempProduct['price'])); } break; } } //} } // foreach $productInCardList }
/** * This is the actual insert command. it steps around the actual "$cart->addProduct()" * method and generates the line item objects directly. It skips and does not generate * any of the events typically included with an add to cart. * If this is an issues, this could be wrapped with such events. * This is taken from Mage_Sales_Model_Quote::addCatalogProductToQuote * * @param Mage_Sales_Model_Quote $quote * @param Mage_Catalog_Model_Product $product * @param int $qty * @return Mage_Core_Model_Abstract|Mage_Sales_Model_Quote_Item */ protected function addCatalogProductToQuote(Mage_Sales_Model_Quote $quote, Mage_Catalog_Model_Product $product, $qty = 1) { $newItem = false; $item = $quote->getItemByProduct($product); if (!$item) { $item = Mage::getModel('sales/quote_item'); $item->setQuote($quote); if (Mage::app()->getStore()->isAdmin()) { $item->setStoreId($quote->getStore()->getId()); } else { $item->setStoreId(Mage::app()->getStore()->getId()); } $newItem = true; } /** * We can't modify existing child items */ if ($item->getId() && $product->getParentProductId()) { return $item; } $item->setOptions($product->getCustomOptions())->setProduct($product); // Add only item that is not in quote already (there can be other new or already saved item if ($newItem) { $quote->addItem($item); } return $item; }
/** * Insert the ordered items to quote * * @param Mage_Sales_Model_Quote $quote * @param ShopgateCartBase $order * * @return Mage_Sales_Model_Quote * @throws ShopgateLibraryException */ protected function _setQuoteItems($quote, $order) { $this->log('_setQuoteItems', ShopgateLogger::LOGTYPE_DEBUG); foreach ($order->getItems() as $item) { /* @var $item ShopgateOrderItem */ if ($item->isSgCoupon()) { /** is a shopgate coupon */ continue; } $orderInfo = $item->getInternalOrderInfo(); $orderInfo = $this->jsonDecode($orderInfo, true); $amountWithTax = $item->getUnitAmountWithTax(); $stackQuantity = 1; if (!empty($orderInfo['stack_quantity']) && $orderInfo['stack_quantity'] > 1) { $stackQuantity = $orderInfo['stack_quantity']; } if ($stackQuantity > 1) { $amountWithTax = $amountWithTax / $stackQuantity; } $pId = $orderInfo["product_id"]; /** @var Mage_Catalog_Model_Product $product */ $product = Mage::getModel('catalog/product')->setStoreId($this->_getConfig()->getStoreViewId())->load($pId); if (!$product->getId()) { throw new ShopgateLibraryException(ShopgateLibraryException::CART_ITEM_PRODUCT_NOT_FOUND, 'product ID: ' . $pId, true); } $itemNumber = $item->getItemNumber(); $productWeight = null; if (strpos($itemNumber, '-') !== false) { $productWeight = $product->getTypeInstance()->getWeight(); $productIds = explode('-', $itemNumber); $parentId = $productIds[0]; $childId = $productIds[1]; /** @var Mage_Catalog_Model_Product $parent */ $parent = Mage::getModel('catalog/product')->setStoreId($this->_getConfig()->getStoreViewId())->load($parentId); if ($parent->isConfigurable() && !$this->_getConfig()->addOnlySimplesToCart()) { $buyObject = $this->_createQuoteItemBuyInfo($item, $parent, $stackQuantity); $superAttributes = $parent->getTypeInstance(true)->getConfigurableAttributesAsArray($parent); $superAttConfig = array(); foreach ($superAttributes as $productAttribute) { $superAttConfig[$productAttribute['attribute_id']] = $product->getData($productAttribute['attribute_code']); } $buyObject->setSuperAttribute($superAttConfig); $product = $parent; } elseif ($parent->isGrouped()) { /** @var Mage_Catalog_Model_Product_Type_Grouped $product */ $product = Mage::getModel('catalog/product')->setStoreId($this->_getConfig()->getStoreViewId())->load($childId); $buyObject = $this->_createQuoteItemBuyInfo($item, $product, $stackQuantity); $associatedProducts = $parent->getTypeInstance(true)->getAssociatedProducts($parent); $superGroup = array(); foreach ($associatedProducts as $associatedProduct) { /** @var Mage_Catalog_Model_Product $associatedProduct */ $superGroup[$associatedProduct->getId()] = 1; } $buyObject->setSuperGroup($superGroup); $buyObject->setSuperProductConfig(array('product_type' => Mage_Catalog_Model_Product_Type_Grouped::TYPE_CODE, 'product_id' => $parent->getId())); } else { $buyObject = $this->_createQuoteItemBuyInfo($item, $product, $stackQuantity); } } else { $buyObject = $this->_createQuoteItemBuyInfo($item, $product, $stackQuantity); } $product->setData('shopgate_item_number', $itemNumber); $product->setData('shopgate_options', $item->getOptions()); $product->setData('shopgate_inputs', $item->getInputs()); $product->setData('shopgate_attributes', $item->getAttributes()); try { /** @var $quotItem Mage_Sales_Model_Quote_Item */ $quoteItem = $quote->addProduct($product, $buyObject); if (!$quoteItem instanceof Varien_Object) { if (Mage::helper('catalog')->__('The text is too long') == $quoteItem) { Mage::throwException(Mage::helper('catalog')->__('The text is too long')); } else { throw new ShopgateLibraryException(ShopgateLibraryException::UNKNOWN_ERROR_CODE, "Error on adding product to quote! Details: " . var_export($quoteItem, true), true); } } $quoteItem = $quote->getItemByProduct($product); if ($this->_getConfig()->useShopgatePrices()) { $quoteItem->setCustomPrice($amountWithTax); $quoteItem->setOriginalCustomPrice($amountWithTax); } $quoteItem->setTaxPercent($item->getTaxPercent()); if (!is_null($productWeight)) { $quoteItem->setWeight($productWeight); } $quoteItem->setRowWeight($quoteItem->getWeight() * $quoteItem->getQuantity()); $quoteItem->setWeeeTaxApplied(serialize(array())); } catch (Exception $e) { $quote->setShopgateError(array($itemNumber => array($e->getCode() => $e->getMessage()))); } } return $quote; }