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_Shipping extends Mage_Sales_Model_Quote_Address_Total_Abstract
29: {
30: public function __construct()
31: {
32: $this->setCode('shipping');
33: }
34:
35: 36: 37: 38: 39: 40:
41: public function collect(Mage_Sales_Model_Quote_Address $address)
42: {
43: parent::collect($address);
44:
45: $oldWeight = $address->getWeight();
46: $address->setWeight(0);
47: $address->setFreeMethodWeight(0);
48: $this->_setAmount(0)
49: ->_setBaseAmount(0);
50:
51: $items = $this->_getAddressItems($address);
52: if (!count($items)) {
53: return $this;
54: }
55:
56: $method = $address->getShippingMethod();
57: $freeAddress= $address->getFreeShipping();
58:
59: $addressWeight = $address->getWeight();
60: $freeMethodWeight = $address->getFreeMethodWeight();
61:
62: $addressQty = 0;
63:
64: foreach ($items as $item) {
65: 66: 67:
68: if ($item->getProduct()->isVirtual()) {
69: continue;
70: }
71:
72: 73: 74:
75: if ($item->getParentItem()) {
76: continue;
77: }
78:
79: if ($item->getHasChildren() && $item->isShipSeparately()) {
80: foreach ($item->getChildren() as $child) {
81: if ($child->getProduct()->isVirtual()) {
82: continue;
83: }
84: $addressQty += $child->getTotalQty();
85:
86: if (!$item->getProduct()->getWeightType()) {
87: $itemWeight = $child->getWeight();
88: $itemQty = $child->getTotalQty();
89: $rowWeight = $itemWeight*$itemQty;
90: $addressWeight += $rowWeight;
91: if ($freeAddress || $child->getFreeShipping()===true) {
92: $rowWeight = 0;
93: } elseif (is_numeric($child->getFreeShipping())) {
94: $freeQty = $child->getFreeShipping();
95: if ($itemQty>$freeQty) {
96: $rowWeight = $itemWeight*($itemQty-$freeQty);
97: }
98: else {
99: $rowWeight = 0;
100: }
101: }
102: $freeMethodWeight += $rowWeight;
103: $item->setRowWeight($rowWeight);
104: }
105: }
106: if ($item->getProduct()->getWeightType()) {
107: $itemWeight = $item->getWeight();
108: $rowWeight = $itemWeight*$item->getQty();
109: $addressWeight+= $rowWeight;
110: if ($freeAddress || $item->getFreeShipping()===true) {
111: $rowWeight = 0;
112: } elseif (is_numeric($item->getFreeShipping())) {
113: $freeQty = $item->getFreeShipping();
114: if ($item->getQty()>$freeQty) {
115: $rowWeight = $itemWeight*($item->getQty()-$freeQty);
116: }
117: else {
118: $rowWeight = 0;
119: }
120: }
121: $freeMethodWeight+= $rowWeight;
122: $item->setRowWeight($rowWeight);
123: }
124: }
125: else {
126: if (!$item->getProduct()->isVirtual()) {
127: $addressQty += $item->getQty();
128: }
129: $itemWeight = $item->getWeight();
130: $rowWeight = $itemWeight*$item->getQty();
131: $addressWeight+= $rowWeight;
132: if ($freeAddress || $item->getFreeShipping()===true) {
133: $rowWeight = 0;
134: } elseif (is_numeric($item->getFreeShipping())) {
135: $freeQty = $item->getFreeShipping();
136: if ($item->getQty()>$freeQty) {
137: $rowWeight = $itemWeight*($item->getQty()-$freeQty);
138: }
139: else {
140: $rowWeight = 0;
141: }
142: }
143: $freeMethodWeight+= $rowWeight;
144: $item->setRowWeight($rowWeight);
145: }
146: }
147:
148: if (isset($addressQty)) {
149: $address->setItemQty($addressQty);
150: }
151:
152: $address->setWeight($addressWeight);
153: $address->setFreeMethodWeight($freeMethodWeight);
154:
155: $address->collectShippingRates();
156:
157: $this->_setAmount(0)
158: ->_setBaseAmount(0);
159:
160: $method = $address->getShippingMethod();
161:
162: if ($method) {
163: foreach ($address->getAllShippingRates() as $rate) {
164: if ($rate->getCode()==$method) {
165: $amountPrice = $address->getQuote()->getStore()->convertPrice($rate->getPrice(), false);
166: $this->_setAmount($amountPrice);
167: $this->_setBaseAmount($rate->getPrice());
168: $shippingDescription = $rate->getCarrierTitle() . ' - ' . $rate->getMethodTitle();
169: $address->setShippingDescription(trim($shippingDescription, ' -'));
170: break;
171: }
172: }
173: }
174:
175: return $this;
176: }
177:
178: 179: 180: 181: 182: 183:
184: public function fetch(Mage_Sales_Model_Quote_Address $address)
185: {
186: $amount = $address->getShippingAmount();
187: if ($amount != 0 || $address->getShippingDescription()) {
188: $title = Mage::helper('sales')->__('Shipping & Handling');
189: if ($address->getShippingDescription()) {
190: $title .= ' (' . $address->getShippingDescription() . ')';
191: }
192: $address->addTotal(array(
193: 'code' => $this->getCode(),
194: 'title' => $title,
195: 'value' => $address->getShippingAmount()
196: ));
197: }
198: return $this;
199: }
200:
201: 202: 203: 204: 205:
206: public function getLabel()
207: {
208: return Mage::helper('sales')->__('Shipping');
209: }
210: }
211: