예제 #1
0
 protected function resetItem(Mage_Sales_Model_Quote_Item_Abstract $item)
 {
     $item->setDiscountAmount(0.0);
     $item->setBaseDiscountAmount(0.0);
     $item->setRowTotalWithDiscount($item->getRowTotal());
     $item->setBaseRowTotalWithDiscount($item->getBaseRowTotal());
     $item->setDiscountPercent(0);
     $item->setAppliedRuleIds('');
     $item->setFreeShipping(false);
 }
예제 #2
0
 public function process(Mage_Sales_Model_Quote_Item_Abstract $item)
 {
     $item->setFreeShipping(false);
     $item->setDiscountAmount(0);
     $item->setBaseDiscountAmount(0);
     $item->setDiscountPercent(0);
     $quote = $item->getQuote();
     $address = $item->getAddress();
     if (!$address) {
         $address = $item->getQuote()->getShippingAddress();
     }
     $customerId = $quote->getCustomerId();
     $ruleCustomer = Mage::getModel('salesrule/rule_customer');
     $appliedRuleIds = array();
     foreach ($this->_rules as $rule) {
         /**
          * already tried to validate and failed
          */
         if ($rule->getIsValid() === false) {
             continue;
         }
         if ($rule->getIsValid() !== true) {
             /**
              * too many times used in general
              */
             if ($rule->getUsesPerCoupon() && $rule->getTimesUsed() >= $rule->getUsesPerCoupon()) {
                 $rule->setIsValid(false);
                 continue;
             }
             /**
              * too many times used for this customer
              */
             if ($ruleId = $rule->getId()) {
                 $ruleCustomer->loadByCustomerRule($customerId, $ruleId);
                 if ($ruleCustomer->getId()) {
                     if ($ruleCustomer->getTimesUsed() >= $rule->getUsesPerCustomer()) {
                         continue;
                     }
                 }
             }
             $rule->afterLoad();
             /**
              * quote does not meet rule's conditions
              */
             if (!$rule->validate($address)) {
                 $rule->setIsValid(false);
                 continue;
             }
             /**
              * passed all validations, remember to be valid
              */
             $rule->setIsValid(true);
         }
         /**
          * although the rule is valid, this item is not marked for action
          */
         if (!$rule->getActions()->validate($item)) {
             continue;
         }
         $qty = $rule->getDiscountQty() ? min($item->getQty(), $rule->getDiscountQty()) : $item->getQty();
         $rulePercent = $rule->getDiscountAmount();
         switch ($rule->getSimpleAction()) {
             case 'to_percent':
                 $rulePercent = max(0, 100 - $rule->getDiscountAmount());
                 //no break;
             //no break;
             case 'by_percent':
                 $discountAmount = $qty * $item->getCalculationPrice() * $rulePercent / 100;
                 $baseDiscountAmount = $qty * $item->getBaseCalculationPrice() * $rulePercent / 100;
                 if (!$rule->getDiscountQty()) {
                     $discountPercent = min(100, $item->getDiscountPercent() + $rulePercent);
                     $item->setDiscountPercent($discountPercent);
                 }
                 break;
             case 'to_fixed':
                 $quoteAmount = $quote->getStore()->convertPrice($rule->getDiscountAmount());
                 $discountAmount = $qty * ($item->getCalculationPrice() - $quoteAmount);
                 $baseDiscountAmount = $qty * ($item->getBaseCalculationPrice() - $rule->getDiscountAmount());
                 break;
             case 'by_fixed':
                 $quoteAmount = $quote->getStore()->convertPrice($rule->getDiscountAmount());
                 $discountAmount = $qty * $quoteAmount;
                 $baseDiscountAmount = $qty * $rule->getDiscountAmount();
                 break;
         }
         $discountAmount = $quote->getStore()->roundPrice($discountAmount);
         $baseDiscountAmount = $quote->getStore()->roundPrice($baseDiscountAmount);
         $discountAmount = min($discountAmount, $item->getRowTotal());
         $baseDiscountAmount = min($baseDiscountAmount, $item->getBaseRowTotal());
         $item->setDiscountAmount($discountAmount);
         $item->setBaseDiscountAmount($baseDiscountAmount);
         switch ($rule->getSimpleFreeShipping()) {
             case Mage_SalesRule_Model_Rule::FREE_SHIPPING_ITEM:
                 $item->setFreeShipping($rule->getDiscountQty() ? $rule->getDiscountQty() : true);
                 break;
             case Mage_SalesRule_Model_Rule::FREE_SHIPPING_ADDRESS:
                 $address->setFreeShipping(true);
                 break;
         }
         $appliedRuleIds[$rule->getRuleId()] = $rule->getRuleId();
         if ($rule->getStopRulesProcessing()) {
             break;
         }
         if ($rule->getCouponCode() && $rule->getCouponCode() == $this->getCouponCode()) {
             $address->setCouponCode($this->getCouponCode());
         }
     }
     $item->setAppliedRuleIds(join(',', $appliedRuleIds));
     $address->setAppliedRuleIds($this->mergeIds($address->getAppliedRuleIds(), $appliedRuleIds));
     $quote->setAppliedRuleIds($this->mergeIds($quote->getAppliedRuleIds(), $appliedRuleIds));
     return $this;
 }
예제 #3
0
 public function originalProcessBefore141(Mage_Sales_Model_Quote_Item_Abstract $item)
 {
     $item->setFreeShipping(false);
     $item->setDiscountAmount(0);
     $item->setBaseDiscountAmount(0);
     $item->setDiscountPercent(0);
     $quote = $item->getQuote();
     //@nelkaake I know it's wierd that these are still here despite the function not being called unless Mage < 141, but it is possible that this is needed for Magent 1.4.0 only.
     if (Mage::helper('rewards')->isBaseMageVersionAtLeast('1.4.1.0')) {
         $address = $this->_getAddress($item);
         //Clearing applied rule ids for quote and address
         if ($this->_isFirstTimeProcessRun !== true) {
             $this->_isFirstTimeProcessRun = true;
             $quote->setAppliedRuleIds('');
             $address->setAppliedRuleIds('');
         }
     } else {
         if ($item instanceof Mage_Sales_Model_Quote_Address_Item) {
             $address = $item->getAddress();
         } elseif ($quote->isVirtual()) {
             $address = $quote->getBillingAddress();
         } else {
             $address = $quote->getShippingAddress();
         }
     }
     $customerId = $quote->getCustomerId();
     $ruleCustomer = Mage::getModel('salesrule/rule_customer');
     $appliedRuleIds = array();
     foreach ($this->_getRules2() as $rule) {
         /* @var $rule Mage_SalesRule_Model_Rule */
         /**
          * already tried to validate and failed
          */
         if ($rule->getIsValid() === false) {
             continue;
         }
         // WDCA CODE BEGIN
         $rewards_rule = $this->getRule($rule->getId());
         // WDCA CODE END
         if ($rule->getIsValid() !== true) {
             /**
              * too many times used in general
              */
             if ($rule->getUsesPerCoupon() && $rule->getTimesUsed() >= $rule->getUsesPerCoupon()) {
                 $rule->setIsValid(false);
                 continue;
             }
             /**
              * too many times used for this customer
              */
             $ruleId = $rule->getId();
             if ($ruleId && $rule->getUsesPerCustomer()) {
                 $ruleCustomer->loadByCustomerRule($customerId, $ruleId);
                 if ($ruleCustomer->getId()) {
                     if ($ruleCustomer->getTimesUsed() >= $rule->getUsesPerCustomer()) {
                         continue;
                     }
                 }
             }
             $rule->afterLoad();
             /**
              * quote does not meet rule's conditions
              */
             if (!$rule->validate($address)) {
                 $rule->setIsValid(false);
                 continue;
             }
             /**
              * passed all validations, remember to be valid
              */
             $rule->setIsValid(true);
         }
         /**
          * although the rule is valid, this item is not marked for action
          */
         if (!$rule->getActions()->validate($item)) {
             continue;
         }
         //@nelkaake Added on Thursday August 19, 2010:
         if (Mage::helper('rewards')->isBaseMageVersionAtLeast('1.4.1')) {
             $qty = $item->getTotalQty();
         } else {
             $qty = $item->getQty();
             if ($item->getParentItem()) {
                 $qty *= $item->getParentItem()->getQty();
             }
         }
         $qty = $rule->getDiscountQty() ? min($qty, $rule->getDiscountQty()) : $qty;
         $rulePercent = min(100, $rule->getDiscountAmount());
         $discountAmount = 0;
         $baseDiscountAmount = 0;
         switch ($rule->getSimpleAction()) {
             case 'to_percent':
                 $rulePercent = max(0, 100 - $rule->getDiscountAmount());
                 //no break;
             //no break;
             case 'by_percent':
                 if ($step = $rule->getDiscountStep()) {
                     $qty = floor($qty / $step) * $step;
                 }
                 $discountAmount = ($qty * $item->getCalculationPrice() - $item->getDiscountAmount()) * $rulePercent / 100;
                 $baseDiscountAmount = ($qty * $item->getBaseCalculationPrice() - $item->getBaseDiscountAmount()) * $rulePercent / 100;
                 if (!$rule->getDiscountQty() || $rule->getDiscountQty() > $qty) {
                     $discountPercent = min(100, $item->getDiscountPercent() + $rulePercent);
                     $item->setDiscountPercent($discountPercent);
                 }
                 break;
             case 'to_fixed':
                 $quoteAmount = $quote->getStore()->convertPrice($rule->getDiscountAmount());
                 $discountAmount = $qty * ($item->getCalculationPrice() - $quoteAmount);
                 $baseDiscountAmount = $qty * ($item->getBaseCalculationPrice() - $rule->getDiscountAmount());
                 break;
             case 'by_fixed':
                 if ($step = $rule->getDiscountStep()) {
                     $qty = floor($qty / $step) * $step;
                 }
                 $quoteAmount = $quote->getStore()->convertPrice($rule->getDiscountAmount());
                 $discountAmount = $qty * $quoteAmount;
                 $baseDiscountAmount = $qty * $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($item->getRowTotal(), $quoteAmount);
                     $baseDiscountAmount = min($item->getBaseRowTotal(), $cartRules[$rule->getId()]);
                     $cartRules[$rule->getId()] -= $baseDiscountAmount;
                 }
                 $address->setCartFixedRules($cartRules);
                 break;
             case 'buy_x_get_y':
                 $x = $rule->getDiscountStep();
                 $y = $rule->getDiscountAmount();
                 if (!$x || $y >= $x) {
                     break;
                 }
                 $buy = 0;
                 $free = 0;
                 while ($buy + $free < $qty) {
                     $buy += $x;
                     if ($buy + $free >= $qty) {
                         break;
                     }
                     $free += min($y, $qty - $buy - $free);
                     if ($buy + $free >= $qty) {
                         break;
                     }
                 }
                 $discountAmount = $free * $item->getCalculationPrice();
                 $baseDiscountAmount = $free * $item->getBaseCalculationPrice();
                 break;
         }
         $result = new Varien_Object(array('discount_amount' => $discountAmount, 'base_discount_amount' => $baseDiscountAmount));
         Mage::dispatchEvent('salesrule_validator_process', array('rule' => $rule, 'item' => $item, 'address' => $address, 'quote' => $quote, 'qty' => $qty, 'result' => $result));
         $discountAmount = $result->getDiscountAmount();
         $baseDiscountAmount = $result->getBaseDiscountAmount();
         $discountAmount = $quote->getStore()->roundPrice($discountAmount);
         $baseDiscountAmount = $quote->getStore()->roundPrice($baseDiscountAmount);
         $discountAmount = min($item->getDiscountAmount() + $discountAmount, $item->getRowTotal());
         $baseDiscountAmount = min($item->getBaseDiscountAmount() + $baseDiscountAmount, $item->getBaseRowTotal());
         $item->setDiscountAmount($discountAmount);
         $item->setBaseDiscountAmount($baseDiscountAmount);
         //@nelkaake 2/6/2010 2:34:20 PM : WDCA CODE BEGIN (just added the if statement)
         if (!$rewards_rule->isPointsRule()) {
             switch ($rule->getSimpleFreeShipping()) {
                 case Mage_SalesRule_Model_Rule::FREE_SHIPPING_ITEM:
                     $item->setFreeShipping($rule->getDiscountQty() ? $rule->getDiscountQty() : true);
                     break;
                 case Mage_SalesRule_Model_Rule::FREE_SHIPPING_ADDRESS:
                     $address->setFreeShipping(true);
                     break;
             }
         }
         //@nelkaake 2/6/2010 2:34:38 PM : WDCA CODE END
         $appliedRuleIds[$rule->getRuleId()] = $rule->getRuleId();
         //@nelkaake Added on Monday August 9, 2010:
         //@nelkaake Changed on Monday August 23, 2010:
         if (Mage::helper('rewards')->isBaseMageVersionAtLeast('1.4.1.0')) {
             $this->_maintainAddressCouponCode($address, $rule);
         } else {
             if ($rule->getCouponCode() && strtolower($rule->getCouponCode()) == strtolower($this->getCouponCode())) {
                 $address->setCouponCode($this->getCouponCode());
             }
         }
         if (Mage::helper('rewards')->isBaseMageVersionAtLeast('1.4')) {
             $this->_addDiscountDescription($address, $rule);
         }
         if ($rule->getStopRulesProcessing()) {
             //@nelkaake Don't break the loop if this is a rewards rule:
             if ($rewards_rule->isRedemptionRule()) {
             } else {
                 break;
             }
         }
     }
     $item->setAppliedRuleIds(join(',', $appliedRuleIds));
     $address->setAppliedRuleIds($this->mergeIds($address->getAppliedRuleIds(), $appliedRuleIds));
     $quote->setAppliedRuleIds($this->mergeIds($quote->getAppliedRuleIds(), $appliedRuleIds));
     return $this;
 }
예제 #4
0
 /**
  * Quote item free shipping ability check
  * This process not affect information about applied rules, coupon code etc.
  * This information will be added during discount amounts processing
  *
  * @param   Mage_Sales_Model_Quote_Item_Abstract $item
  * @return  Mage_SalesRule_Model_Validator
  */
 public function processFreeShipping(Mage_Sales_Model_Quote_Item_Abstract $item)
 {
     $address = $item->getAddress();
     $item->setFreeShipping(false);
     foreach ($this->_getRules() as $rule) {
         /* @var $rule Mage_SalesRule_Model_Rule */
         if (!$this->_canProcessRule($rule, $address)) {
             continue;
         }
         if (!$rule->getActions()->validate($item)) {
             continue;
         }
         switch ($rule->getSimpleFreeShipping()) {
             case Mage_SalesRule_Model_Rule::FREE_SHIPPING_ITEM:
                 $item->setFreeShipping($rule->getDiscountQty() ? $rule->getDiscountQty() : true);
                 break;
             case Mage_SalesRule_Model_Rule::FREE_SHIPPING_ADDRESS:
                 $address->setFreeShipping(true);
                 break;
         }
         if ($rule->getStopRulesProcessing()) {
             break;
         }
     }
     return $this;
 }
예제 #5
0
 public function process(Mage_Sales_Model_Quote_Item_Abstract $item)
 {
     $item->setFreeShipping(false);
     $item->setDiscountAmount(0);
     $item->setBaseDiscountAmount(0);
     $item->setDiscountPercent(0);
     $quote = $item->getQuote();
     if ($item instanceof Mage_Sales_Model_Quote_Address_Item) {
         $address = $item->getAddress();
     } elseif ($quote->isVirtual()) {
         $address = $quote->getBillingAddress();
     } else {
         $address = $quote->getShippingAddress();
     }
     $customerId = $quote->getCustomerId();
     $ruleCustomer = Mage::getModel('salesrule/rule_customer');
     $appliedRuleIds = array();
     foreach ($this->_rules as $rule) {
         /* @var $rule Mage_SalesRule_Model_Rule */
         /**
          * already tried to validate and failed
          */
         if ($rule->getIsValid() === false) {
             continue;
         }
         if ($rule->getIsValid() !== true) {
             /**
              * too many times used in general
              */
             if ($rule->getUsesPerCoupon() && $rule->getTimesUsed() >= $rule->getUsesPerCoupon()) {
                 $rule->setIsValid(false);
                 continue;
             }
             /**
              * too many times used for this customer
              */
             $ruleId = $rule->getId();
             if ($ruleId && $rule->getUsesPerCustomer()) {
                 $ruleCustomer->loadByCustomerRule($customerId, $ruleId);
                 if ($ruleCustomer->getId()) {
                     if ($ruleCustomer->getTimesUsed() >= $rule->getUsesPerCustomer()) {
                         continue;
                     }
                 }
             }
             $rule->afterLoad();
             /**
              * quote does not meet rule's conditions
              */
             if (!$rule->validate($address)) {
                 $rule->setIsValid(false);
                 continue;
             }
             /**
              * passed all validations, remember to be valid
              */
             $rule->setIsValid(true);
         }
         /**
          * although the rule is valid, this item is not marked for action
          */
         if (!$rule->getActions()->validate($item)) {
             continue;
         }
         $qty = $item->getQty();
         if ($item->getParentItem()) {
             $qty *= $item->getParentItem()->getQty();
         }
         $qty = $rule->getDiscountQty() ? min($qty, $rule->getDiscountQty()) : $qty;
         $rulePercent = min(100, $rule->getDiscountAmount());
         $discountAmount = 0;
         $baseDiscountAmount = 0;
         switch ($rule->getSimpleAction()) {
             case 'to_percent':
                 $rulePercent = max(0, 100 - $rule->getDiscountAmount());
                 //no break;
             //no break;
             case 'by_percent':
                 if ($step = $rule->getDiscountStep()) {
                     $qty = floor($qty / $step) * $step;
                 }
                 $discountAmount = ($qty * $item->getCalculationPrice() - $item->getDiscountAmount()) * $rulePercent / 100;
                 $baseDiscountAmount = ($qty * $item->getBaseCalculationPrice() - $item->getBaseDiscountAmount()) * $rulePercent / 100;
                 if (!$rule->getDiscountQty() || $rule->getDiscountQty() > $qty) {
                     $discountPercent = min(100, $item->getDiscountPercent() + $rulePercent);
                     $item->setDiscountPercent($discountPercent);
                 }
                 break;
             case 'to_fixed':
                 $quoteAmount = $quote->getStore()->convertPrice($rule->getDiscountAmount());
                 $discountAmount = $qty * ($item->getCalculationPrice() - $quoteAmount);
                 $baseDiscountAmount = $qty * ($item->getBaseCalculationPrice() - $rule->getDiscountAmount());
                 break;
             case 'by_fixed':
                 if ($step = $rule->getDiscountStep()) {
                     $qty = floor($qty / $step) * $step;
                 }
                 $quoteAmount = $quote->getStore()->convertPrice($rule->getDiscountAmount());
                 $discountAmount = $qty * $quoteAmount;
                 $baseDiscountAmount = $qty * $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($item->getRowTotal(), $quoteAmount);
                     $baseDiscountAmount = min($item->getBaseRowTotal(), $cartRules[$rule->getId()]);
                     $cartRules[$rule->getId()] -= $baseDiscountAmount;
                 }
                 $address->setCartFixedRules($cartRules);
                 break;
             case 'buy_x_get_y':
                 $x = $rule->getDiscountStep();
                 $y = $rule->getDiscountAmount();
                 if (!$x || $y >= $x) {
                     break;
                 }
                 $buy = 0;
                 $free = 0;
                 while ($buy + $free < $qty) {
                     $buy += $x;
                     if ($buy + $free >= $qty) {
                         break;
                     }
                     $free += min($y, $qty - $buy - $free);
                     if ($buy + $free >= $qty) {
                         break;
                     }
                 }
                 $discountAmount = $free * $item->getCalculationPrice();
                 $baseDiscountAmount = $free * $item->getBaseCalculationPrice();
                 break;
         }
         $result = new Varien_Object(array('discount_amount' => $discountAmount, 'base_discount_amount' => $baseDiscountAmount));
         Mage::dispatchEvent('salesrule_validator_process', array('rule' => $rule, 'item' => $item, 'address' => $address, 'quote' => $quote, 'qty' => $qty, 'result' => $result));
         $discountAmount = $result->getDiscountAmount();
         $baseDiscountAmount = $result->getBaseDiscountAmount();
         $discountAmount = $quote->getStore()->roundPrice($discountAmount);
         $baseDiscountAmount = $quote->getStore()->roundPrice($baseDiscountAmount);
         $discountAmount = min($item->getDiscountAmount() + $discountAmount, $item->getRowTotal());
         $baseDiscountAmount = min($item->getBaseDiscountAmount() + $baseDiscountAmount, $item->getBaseRowTotal());
         $item->setDiscountAmount($discountAmount);
         $item->setBaseDiscountAmount($baseDiscountAmount);
         switch ($rule->getSimpleFreeShipping()) {
             case Mage_SalesRule_Model_Rule::FREE_SHIPPING_ITEM:
                 $item->setFreeShipping($rule->getDiscountQty() ? $rule->getDiscountQty() : true);
                 break;
             case Mage_SalesRule_Model_Rule::FREE_SHIPPING_ADDRESS:
                 $address->setFreeShipping(true);
                 break;
         }
         $appliedRuleIds[$rule->getRuleId()] = $rule->getRuleId();
         if ($rule->getCouponCode() && strtolower($rule->getCouponCode()) == strtolower($this->getCouponCode())) {
             $address->setCouponCode($this->getCouponCode());
         }
         if ($rule->getStopRulesProcessing()) {
             break;
         }
     }
     $item->setAppliedRuleIds(join(',', $appliedRuleIds));
     $address->setAppliedRuleIds($this->mergeIds($address->getAppliedRuleIds(), $appliedRuleIds));
     $quote->setAppliedRuleIds($this->mergeIds($quote->getAppliedRuleIds(), $appliedRuleIds));
     return $this;
 }