Esempio n. 1
0
 /**
  * Returns a total discount on the cart from the provided items
  *
  * @param Mage_Sales_Model_Quote_Item $item
  * @param Mage_Sales_Model_Quote_Address $address
  * @param TBT_Rewards_Model_Sales_Rule $rule
  * @param int $qty	max discount qty or unlimited if null
  * @return float
  */
 protected function _getTotalPercentDiscount($item, $address, $rule, $qty = null)
 {
     $all_items = $item->getQuote()->getAllItems();
     $store = $item->getQuote()->getStore();
     $qty = empty($qty) ? $item->getQty() : (int) $qty;
     //@nelkaake -a 1/12/10: Confusing way how this returns in mid-method
     if ($rule->getPointsAction() != TBT_Rewards_Model_Salesrule_Actions::ACTION_DISCOUNT_BY_POINTS_SPENT) {
         $discountPercent = $rule->getDiscountAmount() / 100;
         $totalDiscountOnCart = 0;
         foreach ($all_items as $cartItem) {
             if (!$rule->getActions()->validate($cartItem)) {
                 continue;
             }
             $discountOnItem = $cartItem->getRowTotal() / $item->getQty() * $qty * $discountPercent;
             $totalDiscountOnCart += $discountOnItem;
         }
         return $totalDiscountOnCart;
     }
     // else, this is a by points spent rule:
     $points_spent = Mage::getSingleton('rewards/session')->getPointsSpending();
     $discountPercent = $rule->getDiscountAmount() * floor($points_spent / $rule->getPointsAmount()) / 100;
     $discountPercent = min($discountPercent, 1);
     $totalDiscountOnCart = 0;
     $totalAmountToDiscount = 0;
     foreach ($all_items as $cartItem) {
         if (!$rule->getActions()->validate($cartItem)) {
             continue;
         }
         if (Mage::helper('tax')->discountTax($store) && !Mage::helper('tax')->applyTaxAfterDiscount($store)) {
             $totalAmountToDiscount += $cartItem->getRowTotalInclTax();
             // $cartItem->getTaxAmount();
         } else {
             $totalAmountToDiscount += $cartItem->getRowTotal();
             // $cartItem->getTaxAmount();
         }
         //$cartItem->printPre();
     }
     // @nelkaake -a 16/11/10:
     if ($rule->getApplyToShipping()) {
         $totalAmountToDiscount += $address->getShippingAmount();
     }
     $totalDiscountOnCart = $totalAmountToDiscount * $discountPercent;
     return $totalDiscountOnCart;
 }