This repository has been archived by the owner on Oct 30, 2019. It is now read-only.
/
Carrier.php
221 lines (187 loc) · 6.61 KB
/
Carrier.php
1
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* @category Thebod
* @package Thebod_Shippingrates
* @copyright Copyright (c) 2012 Bastian Ike (http://thebod.de/)
* @author Bastian Ike <b-ike@b-ike.de>
* @license http://creativecommons.org/licenses/by/3.0/ CC-BY 3.0
*/
class Thebod_Shippingrates_Model_Carrier extends Mage_Shipping_Model_Carrier_Abstract {
protected $_code = 'shippingrates';
/**
* returns notification mail address for given rate
*
* @param string $code
* @return string
*/
public function getNotificationMail($code) {
$data = $this->getConfigData('shippingconfig');
if(!is_array($data)) {
$data = unserialize(base64_decode($data));
}
/* searches correct mail address */
foreach($data['code'] as $k => $v) {
if(($this->_code . '_' . $v) == $code) {
return $data['email'][$k];
}
}
}
/**
* applies filters for rate on request
*
* @param array $rate
* @param Mage_Shipping_Model_Rate_Request $request
* @return boolean
*/
public function checkRate(array $rate, Mage_Shipping_Model_Rate_Request $request) {
if(!Mage::getSingleton('checkout/session')->hasQuote()) {
return true;
}
if(!isset($rate['filter'])) {
return true;
}
$filters = explode(';', $rate['filter']);
$passed = true;
foreach($filters as $filter) {
$filter = explode(':', $filter);
$condition = $filter[0];
$value = isset($filter[1]) && $filter[1] ? $filter[1] : false;
if($value === false) {
continue;
}
switch($condition) {
case 'min_qty':
if($request->getPackageQty() < $value) {
$passed = false;
}
break;
case 'max_qty':
if($request->getPackageQty() > $value) {
$passed = false;
}
break;
case 'min_subtotal':
if($request->getPackageValueWithDiscount() < $value) {
$passed = false;
}
break;
case 'max_subtotal':
if($request->getPackageValueWithDiscount() > $value) {
$passed = false;
}
break;
case 'min_weight':
if($request->getPackageWeight() < $value) {
$passed = false;
}
break;
case 'max_weight':
if($request->getPackageWeight() > $value) {
$passed = false;
}
break;
case 'countries':
$dest = strtolower($request->getDestCountryId());
$allowed = explode(',', strtolower($value));
foreach($allowed as $k => $v) {
$allowed[$k] = trim($v);
}
if(!in_array($dest, $allowed) && count($allowed)) {
$passed = false;
}
break;
}
}
return $passed;
}
/**
* collect shipping rates
*
* @param Mage_Shipping_Model_Rate_Request $request
* @return Mage_Shipping_Model_Rate_Result
*/
public function collectRates(Mage_Shipping_Model_Rate_Request $request) {
if(!$this->getConfigFlag('active')) {
return false;
}
$result = Mage::getModel('shipping/rate_result');
$rates = $this->getRates($this->getConfigData('shippingconfig'));
foreach($rates as $rate) {
if($this->checkRate($rate, $request)) {
$method = Mage::getModel('shipping/rate_result_method');
$method->setCarrier($this->_code);
$method->setCarrierTitle($this->getConfigData('title'));
//$method->setMethod($this->_code . '_' . $rate['code']);
$method->setMethod($rate['code']);
$method->setMethodTitle($rate['title']);
$method->setPrice($rate['price']);
$result->append($method);
}
}
return $result;
}
/**
* return shipping table rates
*
* @param Mage_Shipping_Model_Rate_Request $data
* @return array
*/
public function getRates($data) {
$rates = array();
$methods = array();
if(!is_array($data)) {
$data = unserialize(base64_decode($data));
}
/* rearrange array */
/* $data: array(code => array('', 'a', 'b'), price => array('', 10, 20), description => array('', 'desc1', 'desc2')) */
/* $key: code, then price, then data - $value: array*/
foreach($data as $key => $value) {
/* $value: array, $methodid: id of this method, $methodvalue: value of this entry */
foreach($value as $methodid => $methodvalue) {
/* we ignore this if methodid == 0 */
if($methodid) {
/* methods = array(methodid => array(code => 'a', price => 10, description => 'desc1')) ... */
$methods[$methodid][$key] = $methodvalue;
}
}
}
foreach($methods as $method) {
$code = trim($method['code']);
$price = trim($method['price']);
$filter = trim($method['filter']);
$title = nl2br(trim($method['description']));
$rates[] = array(
'code' => $code,
'title' => $title,
'price' => $price,
'filter' => $filter,
);
}
krsort($rates);
return $rates;
}
/**
* return allowed shipping methods
*
* @return array
*/
public function getAllowedMethods()
{
$allowedMethods = array(
$this->_code => $this->getConfigData('name'),
);
return $allowedMethods;
}
}