1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Commerce\Services; |
4
|
|
|
|
5
|
|
|
use Craft\Craft; |
6
|
|
|
use Craft\Commerce_ShippingMethodModel; |
7
|
|
|
use Craft\Commerce_ShippingRuleModel; |
8
|
|
|
use Craft\Commerce_ShippingRuleCategoryModel; |
9
|
|
|
use Craft\Commerce_ShippingZoneRecord; |
10
|
|
|
use NerdsAndCompany\Schematic\Services\Base; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Schematic Commerce Shipping Methods Service. |
14
|
|
|
* |
15
|
|
|
* Sync Craft Setups. |
16
|
|
|
* |
17
|
|
|
* @author Nerds & Company |
18
|
|
|
* @copyright Copyright (c) 2015-2017, Nerds & Company |
19
|
|
|
* @license MIT |
20
|
|
|
* |
21
|
|
|
* @see http://www.nerds.company |
22
|
|
|
*/ |
23
|
|
|
class ShippingMethods extends Base |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Export shippingMethods. |
27
|
|
|
* |
28
|
|
|
* @param ShippingMethodModel[] $shippingMethods |
29
|
|
|
* |
30
|
|
|
* @return array |
31
|
|
|
*/ |
32
|
|
|
public function export(array $shippingMethods = []) |
33
|
|
|
{ |
34
|
|
|
if (!count($shippingMethods)) { |
35
|
|
|
$shippingMethods = Craft::app()->commerce_shippingMethods->getAllShippingMethods(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
Craft::log(Craft::t('Exporting Commerce Shipping Methods')); |
39
|
|
|
|
40
|
|
|
$shippingMethodDefinitions = []; |
41
|
|
|
|
42
|
|
|
foreach ($shippingMethods as $shippingMethod) { |
43
|
|
|
$shippingMethodDefinitions[$shippingMethod->handle] = $this->getShippingMethodDefinition($shippingMethod); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $shippingMethodDefinitions; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get shipping methods definition. |
51
|
|
|
* |
52
|
|
|
* @param Commerce_ShippingMethodModel $shippingMethod |
53
|
|
|
* |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
private function getShippingMethodDefinition(Commerce_ShippingMethodModel $shippingMethod) |
57
|
|
|
{ |
58
|
|
|
return [ |
59
|
|
|
'name' => $shippingMethod->name, |
60
|
|
|
'enabled' => $shippingMethod->enabled, |
61
|
|
|
'rules' => $this->getRuleDefinitions($shippingMethod->getRules()), |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get rule definitions. |
67
|
|
|
* |
68
|
|
|
* @param Commerce_ShippingRuleModel[] $rules |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
private function getRuleDefinitions(array $rules) |
73
|
|
|
{ |
74
|
|
|
$ruleDefinitions = []; |
75
|
|
|
|
76
|
|
|
foreach ($rules as $rule) { |
77
|
|
|
$ruleDefinitions[$rule->name] = $this->getRuleDefinition($rule); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $ruleDefinitions; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get rule definition. |
85
|
|
|
* |
86
|
|
|
* @param Commerce_ShippingRuleModel $rule |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
private function getRuleDefinition(Commerce_ShippingRuleModel $rule) |
91
|
|
|
{ |
92
|
|
|
return [ |
93
|
|
|
'name' => $rule->name, |
94
|
|
|
'description' => $rule->description, |
95
|
|
|
'shippingZone' => $rule->shippingZone ? $rule->shippingZone->name : null, |
96
|
|
|
'priority' => $rule->priority, |
97
|
|
|
'enabled' => $rule->enabled, |
98
|
|
|
'minQty' => $rule->minQty, |
99
|
|
|
'maxQty' => $rule->maxQty, |
100
|
|
|
'minTotal' => $rule->minTotal, |
101
|
|
|
'maxTotal' => $rule->maxTotal, |
102
|
|
|
'minWeight' => $rule->minWeight, |
103
|
|
|
'maxWeight' => $rule->maxWeight, |
104
|
|
|
'baseRate' => $rule->baseRate, |
105
|
|
|
'perItemRate' => $rule->perItemRate, |
106
|
|
|
'weightRate' => $rule->weightRate, |
107
|
|
|
'percentageRate' => $rule->percentageRate, |
108
|
|
|
'minRate' => $rule->minRate, |
109
|
|
|
'maxRate' => $rule->maxRate, |
110
|
|
|
'categories' => $this->getCategoryDefinitions($rule->getShippingRuleCategories()), |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get category definitions. |
116
|
|
|
* |
117
|
|
|
* @param Commerce_ShippingRuleCategoryModel[] $categories |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
private function getCategoryDefinitions(array $categories) |
122
|
|
|
{ |
123
|
|
|
$categoryDefinitions = []; |
124
|
|
|
|
125
|
|
|
foreach ($categories as $category) { |
126
|
|
|
$categoryDefinitions[$category->getCategory()->handle] = $this->getCategoryDefinition($category); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $categoryDefinitions; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get category definition. |
134
|
|
|
* |
135
|
|
|
* @param Commerce_ShippingRuleCategoryModel $category |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
private function getCategoryDefinition(Commerce_ShippingRuleCategoryModel $category) |
140
|
|
|
{ |
141
|
|
|
return [ |
142
|
|
|
'condition' => $category->condition, |
143
|
|
|
'perItemRate' => $category->perItemRate, |
144
|
|
|
'weightRate' => $category->weightRate, |
145
|
|
|
'percentageRate' => $category->percentageRate, |
146
|
|
|
]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Attempt to import shipping methods. |
151
|
|
|
* |
152
|
|
|
* @param array $shippingMethodDefinitions |
153
|
|
|
* @param bool $force If set to true shipping methods not included in the import will be deleted |
154
|
|
|
* |
155
|
|
|
* @return Result |
156
|
|
|
*/ |
157
|
|
|
public function import(array $shippingMethodDefinitions, $force = false) |
158
|
|
|
{ |
159
|
|
|
Craft::log(Craft::t('Importing Commerce Shipping Methods')); |
160
|
|
|
|
161
|
|
|
$this->resetCraftShippingMethodsServiceCache(); |
162
|
|
|
$this->resetCraftShippingCategoriesServiceCache(); |
163
|
|
|
$shippingMethods = array(); |
164
|
|
|
foreach (Craft::app()->commerce_shippingMethods->getAllShippingMethods() as $shippingMethod) { |
165
|
|
|
$shippingMethods[$shippingMethod->handle] = $shippingMethod; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
foreach ($shippingMethodDefinitions as $shippingMethodHandle => $shippingMethodDefinition) { |
169
|
|
|
$shippingMethod = array_key_exists($shippingMethodHandle, $shippingMethods) |
170
|
|
|
? $shippingMethods[$shippingMethodHandle] |
171
|
|
|
: new Commerce_ShippingMethodModel(); |
172
|
|
|
|
173
|
|
|
unset($shippingMethods[$shippingMethodHandle]); |
174
|
|
|
|
175
|
|
|
$this->populateShippingMethod($shippingMethod, $shippingMethodDefinition, $shippingMethodHandle); |
176
|
|
|
|
177
|
|
|
if (!Craft::app()->commerce_shippingMethods->saveShippingMethod($shippingMethod)) { // Save shippingmethod via craft |
178
|
|
|
$this->addErrors($shippingMethod->getAllErrors()); |
179
|
|
|
|
180
|
|
|
continue; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$this->populateShippingMethodRules($shippingMethod, $shippingMethodDefinition['rules'], $force); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if ($force) { |
187
|
|
|
foreach ($shippingMethods as $shippingMethod) { |
188
|
|
|
Craft::app()->commerce_shippingMethods->delete($shippingMethod); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $this->getResultModel(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Populate shippingmethod. |
197
|
|
|
* |
198
|
|
|
* @param Commerce_ShippingMethodModel $shippingMethod |
199
|
|
|
* @param array $shippingMethodDefinition |
200
|
|
|
* @param string $shippingMethodHandle |
201
|
|
|
*/ |
202
|
|
|
private function populateShippingMethod(Commerce_ShippingMethodModel $shippingMethod, array $shippingMethodDefinition, $shippingMethodHandle) |
203
|
|
|
{ |
204
|
|
|
$shippingMethod->setAttributes([ |
205
|
|
|
'handle' => $shippingMethodHandle, |
206
|
|
|
'name' => $shippingMethodDefinition['name'], |
207
|
|
|
'enabled' => $shippingMethodDefinition['enabled'], |
208
|
|
|
]); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Populate shipping method rules. |
213
|
|
|
* |
214
|
|
|
* @param Commerce_ShippingMethodModel $shippingMethod |
215
|
|
|
* @param array $ruleDefinitions |
216
|
|
|
* @param bool $force |
217
|
|
|
*/ |
218
|
|
|
private function populateShippingMethodRules(Commerce_ShippingMethodModel $shippingMethod, $ruleDefinitions, $force = false) |
219
|
|
|
{ |
220
|
|
|
$rules = array(); |
221
|
|
|
foreach ($shippingMethod->getRules() as $rule) { |
222
|
|
|
$rules[$rule->name] = $rule; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
foreach ($ruleDefinitions as $ruleName => $ruleDef) { |
226
|
|
|
$rule = array_key_exists($ruleName, $rules) ? $rules[$ruleName] : new Commerce_ShippingRuleModel(); |
227
|
|
|
|
228
|
|
|
unset($rules[$ruleName]); |
229
|
|
|
|
230
|
|
|
$shippingZone = Commerce_ShippingZoneRecord::model()->findByAttributes(array('name' => $ruleDef['shippingZone'])); |
231
|
|
|
|
232
|
|
|
$rule->setAttributes([ |
233
|
|
|
'name' => $ruleName, |
234
|
|
|
'description' => $ruleDef['description'], |
235
|
|
|
'shippingZoneId' => $shippingZone ? $shippingZone->id : null, |
236
|
|
|
'methodId' => $shippingMethod->id, |
237
|
|
|
'priority' => $ruleDef['priority'], |
238
|
|
|
'enabled' => $ruleDef['enabled'], |
239
|
|
|
'minQty' => $ruleDef['minQty'], |
240
|
|
|
'maxQty' => $ruleDef['maxQty'], |
241
|
|
|
'minTotal' => $ruleDef['minTotal'], |
242
|
|
|
'maxTotal' => $ruleDef['maxTotal'], |
243
|
|
|
'minWeight' => $ruleDef['minWeight'], |
244
|
|
|
'maxWeight' => $ruleDef['maxWeight'], |
245
|
|
|
'baseRate' => $ruleDef['baseRate'], |
246
|
|
|
'perItemRate' => $ruleDef['perItemRate'], |
247
|
|
|
'weightRate' => $ruleDef['weightRate'], |
248
|
|
|
'percentageRate' => $ruleDef['percentageRate'], |
249
|
|
|
'minRate' => $ruleDef['minRate'], |
250
|
|
|
'maxRate' => $ruleDef['maxRate'], |
251
|
|
|
]); |
252
|
|
|
|
253
|
|
|
$this->populateShippingMethodRuleCategories($rule, $ruleDef['categories']); |
254
|
|
|
|
255
|
|
|
if (!Craft::app()->commerce_shippingRules->saveShippingRule($rule)) { // Save shippingrule via craft |
256
|
|
|
$this->addErrors($rule->getAllErrors()); |
257
|
|
|
|
258
|
|
|
continue; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
if ($force) { |
263
|
|
|
foreach ($rules as $rule) { |
264
|
|
|
Craft::app()->commerce_shippingRules->deleteShippingRuleById($rule->id); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Populate shipping method rule categories. |
271
|
|
|
* |
272
|
|
|
* @param Commerce_ShippingRuleModel $rule |
273
|
|
|
* @param $categoryDefinitions |
274
|
|
|
*/ |
275
|
|
|
private function populateShippingMethodRuleCategories(Commerce_ShippingRuleModel $rule, $categoryDefinitions) |
276
|
|
|
{ |
277
|
|
|
$categories = array(); |
278
|
|
|
foreach ($rule->getShippingRuleCategories() as $category) { |
279
|
|
|
$categories[$category->getCategory()->handle] = $category; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
foreach ($categoryDefinitions as $categoryHandle => $categoryDef) { |
283
|
|
|
$category = array_key_exists($categoryHandle, $categories) ? $categories[$categoryHandle] : new Commerce_ShippingRuleCategoryModel(); |
284
|
|
|
|
285
|
|
|
$category->setAttributes([ |
286
|
|
|
'shippingCategoryId' => Craft::app()->commerce_shippingCategories->getShippingCategoryByHandle($categoryHandle)->id, |
287
|
|
|
'condition' => $categoryDef['condition'], |
288
|
|
|
'perItemRate' => $categoryDef['perItemRate'], |
289
|
|
|
'weightRate' => $categoryDef['weightRate'], |
290
|
|
|
'percentageRate' => $categoryDef['percentageRate'], |
291
|
|
|
]); |
292
|
|
|
|
293
|
|
|
$categories[$category->shippingCategoryId] = $category; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
$rule->setShippingRuleCategories($categories); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Reset service cache using reflection. |
301
|
|
|
*/ |
302
|
|
|
private function resetCraftShippingMethodsServiceCache() |
303
|
|
|
{ |
304
|
|
|
$obj = Craft::app()->commerce_shippingMethods; |
305
|
|
|
$refObject = new \ReflectionObject($obj); |
306
|
|
|
if ($refObject->hasProperty('_shippingMethods')) { |
307
|
|
|
$refProperty = $refObject->getProperty('_shippingMethods'); |
308
|
|
|
$refProperty->setAccessible(true); |
309
|
|
|
$refProperty->setValue($obj, null); |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Reset service cache using reflection. |
315
|
|
|
*/ |
316
|
|
|
private function resetCraftShippingCategoriesServiceCache() |
317
|
|
|
{ |
318
|
|
|
$obj = Craft::app()->commerce_shippingCategories; |
319
|
|
|
$refObject = new \ReflectionObject($obj); |
320
|
|
|
if ($refObject->hasProperty('_fetchedAllShippingCategories')) { |
321
|
|
|
$refProperty = $refObject->getProperty('_fetchedAllShippingCategories'); |
322
|
|
|
$refProperty->setAccessible(true); |
323
|
|
|
$refProperty->setValue($obj, false); |
324
|
|
|
} |
325
|
|
|
if ($refObject->hasProperty('_shippingCategoriesById')) { |
326
|
|
|
$refProperty = $refObject->getProperty('_shippingCategoriesById'); |
327
|
|
|
$refProperty->setAccessible(true); |
328
|
|
|
$refProperty->setValue($obj, array()); |
329
|
|
|
} |
330
|
|
|
if ($refObject->hasProperty('_shippingCategoriesByHandle')) { |
331
|
|
|
$refProperty = $refObject->getProperty('_shippingCategoriesByHandle'); |
332
|
|
|
$refProperty->setAccessible(true); |
333
|
|
|
$refProperty->setValue($obj, array()); |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|