/** * Convert quote model to order model * * @param Mage_Sales_Model_Quote $quote * @return Mage_Sales_Model_Order */ public function toOrder(Mage_Sales_Model_Quote $quote, $order = null) { if (!$order instanceof Mage_Sales_Model_Order) { $order = Mage::getModel('sales/order'); } /* @var $order Mage_Sales_Model_Order */ $order->setStoreId($quote->getStoreId())->setQuoteId($quote->getId())->setRemoteIp($quote->getRemoteIp())->setCustomerId($quote->getCustomerId())->setCustomerEmail($quote->getCustomerEmail())->setCustomerFirstname($quote->getCustomerFirstname())->setCustomerLastname($quote->getCustomerLastname())->setCustomerGroupId($quote->getCustomerGroupId())->setCustomerTaxClassId($quote->getCustomerTaxClassId())->setCustomerNote($quote->getCustomerNote())->setCustomerNoteNotify($quote->getCustomerNoteNotify())->setCustomerIsGuest($quote->getCustomerIsGuest())->setBaseCurrencyCode($quote->getBaseCurrencyCode())->setStoreCurrencyCode($quote->getStoreCurrencyCode())->setOrderCurrencyCode($quote->getQuoteCurrencyCode())->setStoreToBaseRate($quote->getStoreToBaseRate())->setStoreToOrderRate($quote->getStoreToQuoteRate())->setCouponCode($quote->getCouponCode())->setGiftcertCode($quote->getGiftcertCode())->setIsVirtual($quote->getIsVirtual())->setIsMultiPayment($quote->getIsMultiPayment())->setAppliedRuleIds($quote->getAppliedRuleIds()); Mage::dispatchEvent('sales_convert_quote_to_order', array('order' => $order, 'quote' => $quote)); return $order; }
/** * Return the correct rule set * * @param Mage_Sales_Model_Quote $quote * @param bool $forceReload * * @return Mage_SalesRule_Model_Rule[] */ public function getRules(Mage_Sales_Model_Quote $quote, $forceReload = false) { $websiteId = intval($quote->getStore()->getWebsiteId()); $customerGroupId = intval($quote->getCustomerGroupId()); $couponCode = trim($quote->getCouponCode()); $key = $websiteId . '_' . $customerGroupId . '_' . $couponCode; if ($forceReload) { unset($this->rules[$key]); } if (!isset($this->rules[$key])) { /** @var Mage_SalesRule_Model_Resource_Rule_Collection $rules */ $rules = Mage::getResourceModel('salesrule/rule_collection'); $rules->setValidationFilter($websiteId, $customerGroupId, $couponCode); $this->rules[$key] = $rules->getItems(); } return $this->rules[$key]; }
/** * Merge quotes * * @param Mage_Sales_Model_Quote $quote * @return Mage_Sales_Model_Quote */ public function merge(Mage_Sales_Model_Quote $quote) { Mage::dispatchEvent($this->_eventPrefix . '_merge_before', array($this->_eventObject => $this, 'source' => $quote)); foreach ($quote->getAllVisibleItems() as $item) { $found = false; foreach ($this->getAllItems() as $quoteItem) { if ($quoteItem->compare($item)) { $quoteItem->setQty($quoteItem->getQty() + $item->getQty()); $found = true; break; } } if (!$found) { $newItem = clone $item; $this->addItem($newItem); if ($item->getHasChildren()) { foreach ($item->getChildren() as $child) { $newChild = clone $child; $newChild->setParentItem($newItem); $this->addItem($newChild); } } } } /** * Init shipping and billing address if quote is new */ if (!$this->getId()) { $this->getShippingAddress(); $this->getBillingAddress(); } if ($quote->getCouponCode()) { $this->setCouponCode($quote->getCouponCode()); } Mage::dispatchEvent($this->_eventPrefix . '_merge_after', array($this->_eventObject => $this, 'source' => $quote)); return $this; }
/** * Add summary block to cart * * @param Mage_XmlConnect_Model_Simplexml_Element $xmlObject * @param Mage_Sales_Model_Quote $quote * @return Mage_XmlConnect_Block_Cart */ protected function _cartSummary($xmlObject, $quote) { $cartSummary = $xmlObject->addCustomChild('summary'); $cartSummary->addCustomChild('item', (int) $this->helper('checkout/cart')->getIsVirtualQuote(), array('label' => 'virtual')); $cartSummary->addCustomChild('item', (int) $this->helper('checkout/cart')->getSummaryCount(), array('label' => 'total_qty')); if (strlen($quote->getCouponCode())) { $cartSummary->addCustomChild('item', 1, array('label' => 'has_coupon_code')); } return $this; }
/** * Get the coupon code applied to the quote. * @param Mage_Sales_Model_Quote $quote Quote object to get data from * @return string|null Coupon code applied to the quote */ protected function _extractQuoteCouponData(Mage_Sales_Model_Quote $quote) { return $quote->getCouponCode(); }
/** * Validate current coupon code * * @param Mage_Sales_Model_Quote $quote * @param string $couponCode * * @return bool */ protected function validateCouponCode($quote, $couponCode = '') { $codeLength = strlen($couponCode); $isCodeLengthValid = $codeLength && $codeLength <= 255; // Validate NEW coupon if ($codeLength) { if ($isCodeLengthValid && $couponCode == $quote->getCouponCode()) { Mage::getSingleton('adminhtml/session')->setCouponMessage(Mage::helper('checkout/cart')->__('Coupon code "%s" was applied.', Mage::helper('core')->escapeHtml($couponCode))); return true; } else { // If NEW coupon is not valid add error message Mage::getSingleton('adminhtml/session')->setCouponMessage(Mage::helper('checkout/cart')->__('Coupon code "%s" is not valid.', Mage::helper('core')->escapeHtml($couponCode))); Mage::helper('mageworx_orderspro/edit')->addPendingChanges(Mage::registry('orderspro_order')->getEntityId(), array('coupon_code' => '')); return false; // reset coupon code to empty } } else { Mage::getSingleton('adminhtml/session')->setCouponMessage(Mage::helper('checkout/cart')->__('Coupon code was canceled.')); $quote->setCouponCode(''); return true; } }
/** * Add coupon from this system to quote * * @param Mage_Sales_Model_Quote $quote * @param ShopgateCartBase $order * * @return Mage_Sales_Model_Quote * @throws ShopgateLibraryException */ protected function _setQuoteShopCoupons($quote, $order) { if (count($order->getExternalCoupons()) > 1) { throw new ShopgateLibraryException(ShopgateLibraryException::COUPON_TOO_MANY_COUPONS); } foreach ($order->getExternalCoupons() as $coupon) { /* @var $coupon ShopgateShopgateCoupon */ $couponInfos = $this->jsonDecode($coupon->getInternalInfo(), true); if ($order instanceof ShopgateOrder) { if (!$coupon->getInternalInfo()) { throw new ShopgateLibraryException(ShopgateLibraryException::COUPON_NOT_VALID, 'Field "internal_info" is empty.'); } /** @var Mage_SalesRule_Model_Coupon $mageCoupon */ if ($this->_getConfigHelper()->getIsMagentoVersionLower1410()) { $mageCoupon = Mage::getModel('salesrule/rule')->load($couponInfos["coupon_id"]); } else { $mageCoupon = Mage::getModel('salesrule/coupon')->load($couponInfos["coupon_id"]); } $count = (int) $mageCoupon->getTimesUsed(); $count--; $mageCoupon->setTimesUsed($count); $mageCoupon->save(); } $quote->setCouponCode($coupon->getCode()); foreach ($quote->getAllAddresses() as $address) { $address->setCouponCode($coupon->getCode()); } $quote->setTotalsCollectedFlag(false); $quote->collectTotals(); if ($this->_errorOnInvalidCoupon) { if ($coupon->getCode() != $quote->getCouponCode()) { throw new ShopgateLibraryException(ShopgateLibraryException::COUPON_NOT_VALID, 'Code transferred by Shopgate"' . $coupon->getCode() . '" != "' . $quote->getCouponCode() . '" code in Magento'); } } $quote->save(); } return $quote; }
/** * set the quote information * @param Mage_Sales_Model_Quote $quoteData */ public function __construct(Mage_Sales_Model_Quote $quoteData) { $customerModel = Mage::getModel('customer/customer'); $customerModel->load($quoteData->getCustomerId()); $this->id = $quoteData->getId(); $this->email = $quoteData->getCustomerEmail(); $this->store_name = $quoteData->getStore()->getName(); $created_at = new Zend_Date($quoteData->getCreatedAt(), Zend_Date::ISO_8601); $this->created_date = $created_at->toString(Zend_Date::ISO_8601); if ($quoteData->getShippingAddress()) { $this->delivery_method = $quoteData->getShippingAddress()->getShippingDescription(); $this->delivery_total = $quoteData->getShippingAddress()->getShippingAmount(); } $this->currency = $quoteData->getStoreCurrencyCode(); if ($payment = $quoteData->getPayment()) { $this->payment = $payment->getMethod(); } $this->couponCode = $quoteData->getCouponCode(); /** * custom quote attributes */ $helper = Mage::helper('ddg'); $website = Mage::app()->getStore($quoteData->getStore())->getWebsite(); $customAttributes = $helper->getConfigSelectedCustomQuoteAttributes($website); if ($customAttributes) { $fields = Mage::getResourceModel('ddg_automation/quote')->getQuoteTableDescription(); foreach ($customAttributes as $customAttribute) { if (isset($fields[$customAttribute])) { $field = $fields[$customAttribute]; $value = $this->_getCustomAttributeValue($field, $quoteData); if ($value) { $this->_assignCustom($field, $value); } } } } /** * Billing address. */ if ($quoteData->getBillingAddress()) { $billingData = $quoteData->getBillingAddress()->getData(); $this->billing_address = array('billing_address_1' => $this->_getStreet($billingData['street'], 1), 'billing_address_2' => $this->_getStreet($billingData['street'], 2), 'billing_city' => $billingData['city'], 'billing_region' => $billingData['region'], 'billing_country' => $billingData['country_id'], 'billing_postcode' => $billingData['postcode']); } /** * Shipping address. */ if ($quoteData->getShippingAddress()) { $shippingData = $quoteData->getShippingAddress()->getData(); $this->delivery_address = array('delivery_address_1' => $this->_getStreet($shippingData['street'], 1), 'delivery_address_2' => $this->_getStreet($shippingData['street'], 2), 'delivery_city' => $shippingData['city'], 'delivery_region' => $shippingData['region'], 'delivery_country' => $shippingData['country_id'], 'delivery_postcode' => $shippingData['postcode']); } /** * Quote items. * @var Mage_Sales_Model_Quote_Item $productItem */ foreach ($quoteData->getAllItems() as $productItem) { $product = $productItem->getProduct(); if ($product) { // category names $categoryCollection = $product->getCategoryCollection()->addAttributeToSelect('name'); foreach ($categoryCollection as $cat) { $categories = array(); $categories[] = $cat->getName(); $this->categories[]['Name'] = substr(implode(', ', $categories), 0, 244); } //get attribute set name $attributeSetName = $this->_getAttributeSetName($product); $this->products[] = array('name' => $productItem->getName(), 'sku' => $productItem->getSku(), 'qty' => (int) number_format($productItem->getData('qty'), 2), 'price' => (double) number_format($productItem->getPrice(), 2, '.', ''), 'attribute-set' => $attributeSetName); } else { // when no product information is available limit to this data $this->products[] = array('name' => $productItem->getName(), 'sku' => $productItem->getSku(), 'qty' => (int) number_format($productItem->getData('qty'), 2), 'price' => (double) number_format($productItem->getPrice(), 2, '.', '')); } } $this->quote_subtotal = (double) number_format($quoteData->getData('subtotal'), 2, '.', ''); $discountAmount = $quoteData->getData('subtotal') - $quoteData->getData('subtotal_with_discount'); $this->discount_amount = (double) number_format($discountAmount, 2, '.', ''); $this->quote_total = (double) number_format($quoteData->getData('grand_total'), 2, '.', ''); return true; }
/** * Get coupon codes from quote * @param Mage_Sales_Model_Quote * @return array */ public function getCouponCodesFromQuote(Mage_Sales_Model_Quote $quote) { $couponCode = $quote->getCouponCode(); return $this->getCouponCodes($couponCode); }
/** * Merge quotes * * @param Mage_Sales_Model_Quote $quote * @return Mage_Sales_Model_Quote */ public function merge(Mage_Sales_Model_Quote $quote) { foreach ($quote->getAllVisibleItems() as $item) { $found = false; foreach ($this->getAllItems() as $quoteItem) { if ($quoteItem->compare($item)) { $quoteItem->setQty($quoteItem->getQty() + $item->getQty()); $found = true; break; } } if (!$found) { $newItem = clone $item; $this->addItem($newItem); if ($item->getHasChildren()) { foreach ($item->getChildren() as $child) { $newChild = clone $child; $newChild->setParentItem($newItem); $this->addItem($newChild); } } } } if ($quote->getCouponCode()) { $this->setCouponCode($quote->getCouponCode()); } return $this; }
/** * Get the applied coupon code on the quote for the passed in rule. * * @param Mage_Sales_Model_Quote * @param Mage_SalesRule_Model_Rule * @return string | null */ public function getQuoteCouponCode(Mage_Sales_Model_Quote $quote, Mage_SalesRule_Model_Rule $rule) { /** @var string */ $codeAppliedToQuote = $quote->getCouponCode(); /** @var string */ $codeInRule = $rule->getCouponCode(); return $codeAppliedToQuote === $codeInRule ? $codeAppliedToQuote : $this->getCodeFromCouponPool($rule, $codeAppliedToQuote); }