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_Shipping_Model_Carrier_Tablerate
29: extends Mage_Shipping_Model_Carrier_Abstract
30: implements Mage_Shipping_Model_Carrier_Interface
31: {
32:
33: protected $_code = 'tablerate';
34: protected $_isFixed = true;
35: protected $_default_condition_name = 'package_weight';
36:
37: protected $_conditionNames = array();
38:
39: public function __construct()
40: {
41: parent::__construct();
42: foreach ($this->getCode('condition_name') as $k=>$v) {
43: $this->_conditionNames[] = $k;
44: }
45: }
46:
47: 48: 49: 50: 51: 52:
53: public function collectRates(Mage_Shipping_Model_Rate_Request $request)
54: {
55: if (!$this->getConfigFlag('active')) {
56: return false;
57: }
58:
59:
60: if (!$this->getConfigFlag('include_virtual_price') && $request->getAllItems()) {
61: foreach ($request->getAllItems() as $item) {
62: if ($item->getParentItem()) {
63: continue;
64: }
65: if ($item->getHasChildren() && $item->isShipSeparately()) {
66: foreach ($item->getChildren() as $child) {
67: if ($child->getProduct()->isVirtual()) {
68: $request->setPackageValue($request->getPackageValue() - $child->getBaseRowTotal());
69: }
70: }
71: } elseif ($item->getProduct()->isVirtual()) {
72: $request->setPackageValue($request->getPackageValue() - $item->getBaseRowTotal());
73: }
74: }
75: }
76:
77:
78: $freeQty = 0;
79: if ($request->getAllItems()) {
80: foreach ($request->getAllItems() as $item) {
81: if ($item->getProduct()->isVirtual() || $item->getParentItem()) {
82: continue;
83: }
84:
85: if ($item->getHasChildren() && $item->isShipSeparately()) {
86: foreach ($item->getChildren() as $child) {
87: if ($child->getFreeShipping() && !$child->getProduct()->isVirtual()) {
88: $freeQty += $item->getQty() * ($child->getQty() - (is_numeric($child->getFreeShipping()) ? $child->getFreeShipping() : 0));
89: }
90: }
91: } elseif ($item->getFreeShipping()) {
92: $freeQty += ($item->getQty() - (is_numeric($item->getFreeShipping()) ? $item->getFreeShipping() : 0));
93: }
94: }
95: }
96:
97: if (!$request->getConditionName()) {
98: $request->setConditionName($this->getConfigData('condition_name') ? $this->getConfigData('condition_name') : $this->_default_condition_name);
99: }
100:
101:
102: $oldWeight = $request->getPackageWeight();
103: $oldQty = $request->getPackageQty();
104:
105: $request->setPackageWeight($request->getFreeMethodWeight());
106: $request->setPackageQty($oldQty - $freeQty);
107:
108: $result = Mage::getModel('shipping/rate_result');
109: $rate = $this->getRate($request);
110:
111: $request->setPackageWeight($oldWeight);
112: $request->setPackageQty($oldQty);
113:
114: if (!empty($rate) && $rate['price'] >= 0) {
115: $method = Mage::getModel('shipping/rate_result_method');
116:
117: $method->setCarrier('tablerate');
118: $method->setCarrierTitle($this->getConfigData('title'));
119:
120: $method->setMethod('bestway');
121: $method->setMethodTitle($this->getConfigData('name'));
122:
123: if ($request->getFreeShipping() === true || ($request->getPackageQty() == $freeQty)) {
124: $shippingPrice = 0;
125: } else {
126: $shippingPrice = $this->getFinalPriceWithHandlingFee($rate['price']);
127: }
128:
129: $method->setPrice($shippingPrice);
130: $method->setCost($rate['cost']);
131:
132: $result->append($method);
133: }
134:
135: return $result;
136: }
137:
138: public function getRate(Mage_Shipping_Model_Rate_Request $request)
139: {
140: return Mage::getResourceModel('shipping/carrier_tablerate')->getRate($request);
141: }
142:
143: public function getCode($type, $code='')
144: {
145: $codes = array(
146:
147: 'condition_name'=>array(
148: 'package_weight' => Mage::helper('shipping')->__('Weight vs. Destination'),
149: 'package_value' => Mage::helper('shipping')->__('Price vs. Destination'),
150: 'package_qty' => Mage::helper('shipping')->__('# of Items vs. Destination'),
151: ),
152:
153: 'condition_name_short'=>array(
154: 'package_weight' => Mage::helper('shipping')->__('Weight (and above)'),
155: 'package_value' => Mage::helper('shipping')->__('Order Subtotal (and above)'),
156: 'package_qty' => Mage::helper('shipping')->__('# of Items (and above)'),
157: ),
158:
159: );
160:
161: if (!isset($codes[$type])) {
162: throw Mage::exception('Mage_Shipping', Mage::helper('shipping')->__('Invalid Table Rate code type: %s', $type));
163: }
164:
165: if (''===$code) {
166: return $codes[$type];
167: }
168:
169: if (!isset($codes[$type][$code])) {
170: throw Mage::exception('Mage_Shipping', Mage::helper('shipping')->__('Invalid Table Rate code for type %s: %s', $type, $code));
171: }
172:
173: return $codes[$type][$code];
174: }
175:
176: 177: 178: 179: 180:
181: public function getAllowedMethods()
182: {
183: return array('bestway'=>$this->getConfigData('name'));
184: }
185:
186: }
187: