1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: class Mage_Sales_Model_Quote_Address_Total_Discount extends Mage_Sales_Model_Quote_Address_Total_Abstract
29: {
30: public function collect(Mage_Sales_Model_Quote_Address $address)
31: {
32: $quote = $address->getQuote();
33: $eventArgs = array(
34: 'website_id'=>Mage::app()->getStore($quote->getStoreId())->getWebsiteId(),
35: 'customer_group_id'=>$quote->getCustomerGroupId(),
36: 'coupon_code'=>$quote->getCouponCode(),
37: );
38:
39: $address->setFreeShipping(0);
40: $totalDiscountAmount = 0;
41: $subtotalWithDiscount= 0;
42: $baseTotalDiscountAmount = 0;
43: $baseSubtotalWithDiscount= 0;
44:
45: $items = $address->getAllItems();
46: if (!count($items)) {
47: $address->setDiscountAmount($totalDiscountAmount);
48: $address->setSubtotalWithDiscount($subtotalWithDiscount);
49: $address->setBaseDiscountAmount($baseTotalDiscountAmount);
50: $address->setBaseSubtotalWithDiscount($baseSubtotalWithDiscount);
51: return $this;
52: }
53:
54: $hasDiscount = false;
55: foreach ($items as $item) {
56: if ($item->getNoDiscount()) {
57: $item->setDiscountAmount(0);
58: $item->setBaseDiscountAmount(0);
59: $item->setRowTotalWithDiscount($item->getRowTotal());
60: $item->setBaseRowTotalWithDiscount($item->getRowTotal());
61:
62: $subtotalWithDiscount+=$item->getRowTotal();
63: $baseSubtotalWithDiscount+=$item->getBaseRowTotal();
64: }
65: else {
66: 67: 68:
69: if ($item->getParentItemId()) {
70: continue;
71: }
72:
73: 74: 75:
76:
77: if ($item->getHasChildren() && $item->isChildrenCalculated()) {
78: foreach ($item->getChildren() as $child) {
79: $eventArgs['item'] = $child;
80: Mage::dispatchEvent('sales_quote_address_discount_item', $eventArgs);
81:
82: if ($child->getDiscountAmount() || $child->getFreeShipping()) {
83: $hasDiscount = true;
84: }
85:
86: 87: 88:
89: if ($item->getFreeShipping()) {
90: $child->setFreeShipping($item->getFreeShipping());
91: }
92:
93: 94: 95:
96: if (!$child->getDiscountAmount() && $item->getDiscountPercent()) {
97:
98: }
99: $totalDiscountAmount += $child->getDiscountAmount();
100: $baseTotalDiscountAmount += $child->getBaseDiscountAmount();
101:
102: $child->setRowTotalWithDiscount($child->getRowTotal()-$child->getDiscountAmount());
103: $child->setBaseRowTotalWithDiscount($child->getBaseRowTotal()-$child->getBaseDiscountAmount());
104:
105: $subtotalWithDiscount+=$child->getRowTotalWithDiscount();
106: $baseSubtotalWithDiscount+=$child->getBaseRowTotalWithDiscount();
107: }
108: }
109: else {
110: $eventArgs['item'] = $item;
111: Mage::dispatchEvent('sales_quote_address_discount_item', $eventArgs);
112:
113: if ($item->getDiscountAmount() || $item->getFreeShipping()) {
114: $hasDiscount = true;
115: }
116: $totalDiscountAmount += $item->getDiscountAmount();
117: $baseTotalDiscountAmount += $item->getBaseDiscountAmount();
118:
119: $item->setRowTotalWithDiscount($item->getRowTotal()-$item->getDiscountAmount());
120: $item->setBaseRowTotalWithDiscount($item->getBaseRowTotal()-$item->getBaseDiscountAmount());
121:
122: $subtotalWithDiscount+=$item->getRowTotalWithDiscount();
123: $baseSubtotalWithDiscount+=$item->getBaseRowTotalWithDiscount();
124: }
125: }
126: }
127: $address->setDiscountAmount($totalDiscountAmount);
128: $address->setSubtotalWithDiscount($subtotalWithDiscount);
129: $address->setBaseDiscountAmount($baseTotalDiscountAmount);
130: $address->setBaseSubtotalWithDiscount($baseSubtotalWithDiscount);
131:
132: $address->setGrandTotal($address->getGrandTotal() - $address->getDiscountAmount());
133: $address->setBaseGrandTotal($address->getBaseGrandTotal()-$address->getBaseDiscountAmount());
134: return $this;
135: }
136:
137: public function fetch(Mage_Sales_Model_Quote_Address $address)
138: {
139: $amount = $address->getDiscountAmount();
140: if ($amount!=0) {
141: $title = Mage::helper('sales')->__('Discount');
142: $code = $address->getCouponCode();
143: if (strlen($code)) {
144: $title = Mage::helper('sales')->__('Discount (%s)', $code);
145: }
146: $address->addTotal(array(
147: 'code'=>$this->getCode(),
148: 'title'=>$title,
149: 'value'=>-$amount
150: ));
151: }
152: return $this;
153: }
154:
155: }
156: