1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* |
6
|
|
|
* @category Mygento |
7
|
|
|
* @package Mygento_Yandexdelivery |
8
|
|
|
* @copyright 2017 NKS LLC. (http://www.mygento.ru) |
9
|
|
|
* @license GPLv2 |
10
|
|
|
*/ |
11
|
|
|
class Mygento_Yandexdelivery_Model_Shipping extends Mage_Shipping_Model_Carrier_Abstract implements Mage_Shipping_Model_Carrier_Interface |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
protected $_name = 'yandexdelivery'; |
15
|
|
|
protected $_isFixed = false; |
16
|
|
|
|
17
|
|
|
public function collectRates(Mage_Shipping_Model_Rate_Request $request) |
18
|
|
|
{ |
19
|
|
|
Varien_Profiler::start($this->_name . '_collect_rate'); |
20
|
|
|
|
21
|
|
|
if (!$this->getType()) { |
22
|
|
|
Mage::helper($this->_name)->addLog('Calc -> no code'); |
23
|
|
|
Varien_Profiler::stop($this->_name . '_collect_rate'); |
24
|
|
|
return false; |
25
|
|
|
} |
26
|
|
|
$type = $this->getType(); |
27
|
|
|
|
28
|
|
|
$valid = $this->validateRequest($request); |
29
|
|
|
if ($valid !== true) { |
30
|
|
|
return $valid; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$city = mb_convert_case(trim($request->getDestCity()), MB_CASE_TITLE, "UTF-8"); |
34
|
|
|
|
35
|
|
|
if ($this->getConfig('onlyone')) { |
36
|
|
|
Mage::helper($this->_name)->addLog('Calc -> only one standart item'); |
37
|
|
|
$weight = round($this->getConfig('oneweight') * 1000 / $this->getConfig('weightunit'), 3); |
38
|
|
|
$dimensions = Mage::helper($this->_name)->getStandardSizes(); |
39
|
|
|
} else { |
40
|
|
|
$quote = $this->getQuote(); |
41
|
|
|
$cartItems = $quote->getAllVisibleItems(); |
42
|
|
|
|
43
|
|
|
Mage::helper($this->_name)->addLog('Found ' . count($cartItems) . ' items in quote'); |
44
|
|
|
Mage::helper($this->_name)->addLog('Calc -> every item in cart'); |
45
|
|
|
|
46
|
|
|
$processed_dimensions = Mage::helper($this->_name)->getSizes($cartItems, true); |
47
|
|
|
$weight = $processed_dimensions['weight']; |
48
|
|
|
|
49
|
|
|
$dimensions = Mage::helper($this->_name)->dimenAlgo($processed_dimensions['dimensions']); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$data = [ |
53
|
|
|
'client_id' => $this->getConfig('client_id'), |
54
|
|
|
'sender_id' => $this->getConfig('sender'), |
55
|
|
|
'city_from' => $this->getConfig('homecity'), |
56
|
|
|
'city_to' => $city, |
57
|
|
|
'weight' => $weight, |
58
|
|
|
'total_cost' => $request->getPackageValue(), |
59
|
|
|
'order_cost' => $request->getPackageValue(), |
60
|
|
|
'payment_method' => $this->getConfig('payment_type'), |
61
|
|
|
'height' => $dimensions['C'], |
62
|
|
|
'width' => $dimensions['B'], |
63
|
|
|
'length' => $dimensions['A'], |
64
|
|
|
'to_yd_warehouse' => $this->getConfig('yd_warehouse'), |
65
|
|
|
'assessed_value' => $request->getPackageValue(), |
66
|
|
|
'index_city' => $request->getDestPostcode(), |
67
|
|
|
'delivery_type' => $type |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
$result = Mage::getModel('shipping/rate_result'); |
71
|
|
|
|
72
|
|
|
Mage::helper('yandexdelivery')->__('Will calc type -> ' . $type); |
73
|
|
|
|
74
|
|
|
Varien_Profiler::start($this->_name . '_request'); |
75
|
|
|
$response = Mage::getModel($this->_name . '/carrier')->searchDeliveryList($data); |
76
|
|
|
Varien_Profiler::stop($this->_name . '_request'); |
77
|
|
|
|
78
|
|
|
if ($response->status != "ok") { |
79
|
|
|
$errors = (array) $response->data->errors; |
80
|
|
|
$error_text = Mage::helper('yandexdelivery')->__('Error:') . ' '; |
81
|
|
|
foreach ($errors as $key => $value) { |
82
|
|
|
$error_text .= $key . " — " . $value . "\n"; |
83
|
|
|
} |
84
|
|
|
return $this->returnError($error_text); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->processType($response, $type, $request, $result); |
88
|
|
|
|
89
|
|
|
Mage::helper($this->_name)->addLog('Calculating ended'); |
90
|
|
|
|
91
|
|
|
Varien_Profiler::stop($this->_name . '_collect_rate'); |
92
|
|
|
return $result; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getAllowedMethods() |
96
|
|
|
{ |
97
|
|
|
return array($this->_name => $this->getConfigData('name')); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Validate shipping request before processing |
102
|
|
|
* |
103
|
|
|
* @param Mage_Shipping_Model_Rate_Request $request |
104
|
|
|
* @return boolean |
105
|
|
|
*/ |
106
|
|
|
private function validateRequest(Mage_Shipping_Model_Rate_Request $request) |
107
|
|
|
{ |
108
|
|
|
if (!$this->getConfig('active')) { |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
Mage::helper($this->_name)->addLog('Started calculating'); |
113
|
|
|
|
114
|
|
|
if (strlen($request->getDestCity()) <= 2) { |
115
|
|
|
Mage::helper($this->_name)->addLog('City strlen <= 2, aborting ...'); |
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if (0 >= $request->getPackageWeight()) { |
120
|
|
|
return $this->returnError('Zero weight'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private function returnError($message) |
127
|
|
|
{ |
128
|
|
|
Mage::helper($this->_name)->addLog($message); |
129
|
|
|
if ($this->getConfig('debug')) { |
130
|
|
|
$error = Mage::getModel('shipping/rate_result_error'); |
131
|
|
|
$error->setCarrier($this->_name); |
132
|
|
|
$error->setCarrierTitle($this->getConfigData('title')); |
133
|
|
|
$error->setErrorMessage(Mage::helper($this->_name)->__($message)); |
134
|
|
|
return $error; |
135
|
|
|
} |
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function isTrackingAvailable() |
140
|
|
|
{ |
141
|
|
|
return true; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function isCityRequired() |
145
|
|
|
{ |
146
|
|
|
return true; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function getFormBlock() |
150
|
|
|
{ |
151
|
|
|
return 'Mygento_Yandexdelivery_Block_Shipping'; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
private function processType($response, $type, $request, $result) |
155
|
|
|
{ |
156
|
|
|
|
157
|
|
|
foreach ($response->data as $carrier_offer) { |
158
|
|
|
$price = $request->getFreeShipping() ? 0 : $carrier_offer->cost; |
159
|
|
|
switch ($carrier_offer->type) { |
160
|
|
View Code Duplication |
case "TODOOR": |
|
|
|
|
161
|
|
|
$method = Mage::getModel('shipping/rate_result_method'); |
162
|
|
|
$method->setCarrier($this->_name . '_' . $type); |
163
|
|
|
$method->setCarrierTitle($this->getTypeTitle($type)); |
164
|
|
|
$method->setMethod($carrier_offer->tariffId . '_' . $carrier_offer->direction . '_' . $carrier_offer->delivery->id); |
165
|
|
|
$method->setMethodTitle($carrier_offer->delivery->name); |
166
|
|
|
$method->setPrice($price); |
167
|
|
|
$method->setCost($price); |
168
|
|
|
Mage::dispatchEvent('yandexdelivery_rate_result', ['method' => $method, 'request' => $request]); |
169
|
|
|
$result->append($method); |
170
|
|
|
break; |
171
|
|
|
case 'PICKUP': |
172
|
|
|
foreach ($carrier_offer->pickupPoints as $point) { |
173
|
|
|
$method = Mage::getModel('shipping/rate_result_method'); |
174
|
|
|
$method->setCarrier($this->_name . '_' . $type); |
175
|
|
|
$method->setCarrierTitle($this->getTypeTitle($type)); |
176
|
|
|
$method->setMethod($carrier_offer->tariffId . '_' . $carrier_offer->direction . '_' . $carrier_offer->delivery->id . '_' . $point->id); |
177
|
|
|
$method->setMethodTitle($carrier_offer->delivery->name . ', ' . $point->name); |
178
|
|
|
$method->setPrice($price); |
179
|
|
|
$method->setCost($price); |
180
|
|
|
Mage::dispatchEvent('yandexdelivery_rate_result', ['method' => $method, 'request' => $request]); |
181
|
|
|
$result->append($method); |
182
|
|
|
} |
183
|
|
|
break; |
184
|
|
View Code Duplication |
case 'POST': |
|
|
|
|
185
|
|
|
$method = Mage::getModel('shipping/rate_result_method'); |
186
|
|
|
$method->setCarrier($this->_name . '_' . $type); |
187
|
|
|
$method->setCarrierTitle($this->getTypeTitle($type)); |
188
|
|
|
$method->setMethod($carrier_offer->tariffId . '_' . $carrier_offer->direction . '_' . $carrier_offer->delivery->id); |
189
|
|
|
$method->setMethodTitle($carrier_offer->delivery->name . ', ' . $carrier_offer->tariffName); |
190
|
|
|
$method->setPrice($price); |
191
|
|
|
$method->setCost($price); |
192
|
|
|
Mage::dispatchEvent('yandexdelivery_rate_result', ['method' => $method, 'request' => $request]); |
193
|
|
|
$result->append($method); |
194
|
|
|
break; |
195
|
|
|
default: |
196
|
|
|
break; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
protected function getTypeTitle($type) |
202
|
|
|
{ |
203
|
|
|
return Mage::getStoreConfig('carriers/' . $this->_name . '_' . $type . '/name'); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function getConfig($path) |
207
|
|
|
{ |
208
|
|
|
return Mage::helper($this->_name)->getConfigData($path); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
protected function getQuote() |
212
|
|
|
{ |
213
|
|
|
if (Mage::app()->getStore()->isAdmin()) { |
214
|
|
|
return Mage::getSingleton('adminhtml/session_quote')->getQuote(); |
215
|
|
|
} |
216
|
|
|
return Mage::getSingleton('checkout/session')->getQuote(); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.