Beispiel #1
0
 /**
  * Collect address discount amount
  *
  * @param   Mage_Sales_Model_Quote_Address $address
  * @return  Mage_SalesRule_Model_Quote_Discount
  */
 public function collect(Mage_Sales_Model_Quote_Address $address)
 {
     parent::collect($address);
     $quote = $address->getQuote();
     $store = Mage::app()->getStore($quote->getStoreId());
     $this->_calculator->reset($address);
     $items = $this->_getAddressItems($address);
     if (!count($items)) {
         return $this;
     }
     $eventArgs = array('website_id' => $store->getWebsiteId(), 'customer_group_id' => $quote->getCustomerGroupId(), 'coupon_code' => $quote->getCouponCode());
     $this->_calculator->init($store->getWebsiteId(), $quote->getCustomerGroupId(), $quote->getCouponCode());
     $this->_calculator->initTotals($items, $address);
     $address->setDiscountDescription(array());
     $items = $this->_calculator->sortItemsByPriority($items);
     foreach ($items as $item) {
         if ($item->getNoDiscount()) {
             $item->setDiscountAmount(0);
             $item->setBaseDiscountAmount(0);
         } else {
             /**
              * Child item discount we calculate for parent
              */
             if ($item->getParentItemId()) {
                 continue;
             }
             $eventArgs['item'] = $item;
             Mage::dispatchEvent('sales_quote_address_discount_item', $eventArgs);
             if ($item->getHasChildren() && $item->isChildrenCalculated()) {
                 foreach ($item->getChildren() as $child) {
                     $this->_calculator->process($child);
                     $eventArgs['item'] = $child;
                     Mage::dispatchEvent('sales_quote_address_discount_item', $eventArgs);
                     $this->_aggregateItemDiscount($child);
                 }
             } else {
                 $this->_calculator->process($item);
                 $this->_aggregateItemDiscount($item);
             }
         }
     }
     /**
      * process weee amount
      */
     if (Mage::helper('weee')->isEnabled() && Mage::helper('weee')->isDiscounted($store)) {
         $this->_calculator->processWeeeAmount($address, $items);
     }
     /**
      * Process shipping amount discount
      */
     $address->setShippingDiscountAmount(0);
     $address->setBaseShippingDiscountAmount(0);
     if ($address->getShippingAmount()) {
         $this->_calculator->processShippingAmount($address);
         $this->_addAmount(-$address->getShippingDiscountAmount());
         $this->_addBaseAmount(-$address->getBaseShippingDiscountAmount());
     }
     $this->_calculator->prepareDescription($address);
     return $this;
 }
Beispiel #2
0
 public function processShippingAmount(Mage_Sales_Model_Quote_Address $address)
 {
     //parent::process($address);
     parent::processShippingAmount($address);
     $shipping_process = Mage::getStoreConfig('rewardpoints/default/process_shipping', Mage::app()->getStore()->getId());
     if (version_compare(Mage::getVersion(), '1.4.0', '>=') && $shipping_process) {
         Mage::getSingleton('rewardpoints/session')->setShippingChecked(0);
         Mage::getModel('rewardpoints/discount')->applyShipping($address);
     }
 }
 public function processShippingAmount(Mage_Sales_Model_Quote_Address $address)
 {
     if (version_compare(Mage::getVersion(), '1.4.1.0', '>=')) {
         return parent::processShippingAmount($address);
     }
     $shippingAmount = $address->getShippingAmountForDiscount();
     if ($shippingAmount !== null) {
         $baseShippingAmount = $address->getBaseShippingAmountForDiscount();
     } else {
         $shippingAmount = $address->getShippingAmount();
         $baseShippingAmount = $address->getBaseShippingAmount();
     }
     $quote = $address->getQuote();
     $appliedRuleIds = array();
     foreach ($this->_getRules() as $rule) {
         /* @var $rule Mage_SalesRule_Model_Rule */
         if (!$rule->getApplyToShipping() || !$this->_canProcessRule($rule, $address)) {
             continue;
         }
         $discountAmount = 0;
         $baseDiscountAmount = 0;
         $rulePercent = min(100, $rule->getDiscountAmount());
         switch ($rule->getSimpleAction()) {
             case 'to_percent':
                 $rulePercent = max(0, 100 - $rule->getDiscountAmount());
             case 'by_percent':
                 $discountAmount = ($shippingAmount - $address->getShippingDiscountAmount()) * $rulePercent / 100;
                 $baseDiscountAmount = ($baseShippingAmount - $address->getBaseShippingDiscountAmount()) * $rulePercent / 100;
                 $discountPercent = min(100, $address->getShippingDiscountPercent() + $rulePercent);
                 $address->setShippingDiscountPercent($discountPercent);
                 break;
             case 'to_fixed':
                 $quoteAmount = $quote->getStore()->convertPrice($rule->getDiscountAmount());
                 $discountAmount = $shippingAmount - $quoteAmount;
                 $baseDiscountAmount = $baseShippingAmount - $rule->getDiscountAmount();
                 break;
             case 'by_fixed':
                 $quoteAmount = $quote->getStore()->convertPrice($rule->getDiscountAmount());
                 $discountAmount = $quoteAmount;
                 $baseDiscountAmount = $rule->getDiscountAmount();
                 break;
             case 'cart_fixed':
                 $cartRules = $address->getCartFixedRules();
                 if (!isset($cartRules[$rule->getId()])) {
                     $cartRules[$rule->getId()] = $rule->getDiscountAmount();
                 }
                 if ($cartRules[$rule->getId()] > 0) {
                     $quoteAmount = $quote->getStore()->convertPrice($cartRules[$rule->getId()]);
                     $discountAmount = min($shippingAmount - $address->getShippingDiscountAmount(), $quoteAmount);
                     $baseDiscountAmount = min($baseShippingAmount - $address->getBaseShippingDiscountAmount(), $cartRules[$rule->getId()]);
                     $cartRules[$rule->getId()] -= $baseDiscountAmount;
                 }
                 $address->setCartFixedRules($cartRules);
                 break;
         }
         $discountAmount = min($address->getShippingDiscountAmount() + $discountAmount, $shippingAmount);
         $baseDiscountAmount = min($address->getBaseShippingDiscountAmount() + $baseDiscountAmount, $baseShippingAmount);
         $address->setShippingDiscountAmount($discountAmount);
         $address->setBaseShippingDiscountAmount($baseDiscountAmount);
         $appliedRuleIds[$rule->getRuleId()] = $rule->getRuleId();
         // MODIFICATION BY AMASTY
         $currentCouponCode = explode(',', $this->getCouponCode());
         if ($currentCouponCode && $rule->getCouponCode() && in_array($rule->getCouponCode(), $currentCouponCode)) {
             $address->setCouponCode($this->getCouponCode());
         }
         // MODIFICATION BY AMASTY END
         if ($rule->getCouponCode() && strtolower($rule->getCouponCode()) == strtolower($this->getCouponCode())) {
             $address->setCouponCode($this->getCouponCode());
         }
         $this->_addDiscountDescription($address, $rule);
         if ($rule->getStopRulesProcessing()) {
             break;
         }
     }
     $address->setAppliedRuleIds($this->mergeIds($address->getAppliedRuleIds(), $appliedRuleIds));
     $quote->setAppliedRuleIds($this->mergeIds($quote->getAppliedRuleIds(), $appliedRuleIds));
     return $this;
 }