1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Commerce\Services; |
4
|
|
|
|
5
|
|
|
use Craft\Craft; |
6
|
|
|
use Craft\Commerce_ShippingZoneModel; |
7
|
|
|
use NerdsAndCompany\Schematic\Services\Base; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Schematic Commerce Shipping Zones Service. |
11
|
|
|
* |
12
|
|
|
* Sync Craft Setups. |
13
|
|
|
* |
14
|
|
|
* @author Nerds & Company |
15
|
|
|
* @copyright Copyright (c) 2015-2017, Nerds & Company |
16
|
|
|
* @license MIT |
17
|
|
|
* |
18
|
|
|
* @see http://www.nerds.company |
19
|
|
|
*/ |
20
|
|
|
class ShippingZones extends Base |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Export shippingZones. |
24
|
|
|
* |
25
|
|
|
* @param ShippingZoneModel[] $shippingZones |
26
|
|
|
* |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
public function export(array $shippingZones = []) |
30
|
|
|
{ |
31
|
|
|
if (!count($shippingZones)) { |
32
|
|
|
$shippingZones = Craft::app()->commerce_shippingZones->getAllShippingZones(true); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
Craft::log(Craft::t('Exporting Commerce Shipping Zones')); |
36
|
|
|
|
37
|
|
|
$shippingZoneDefinitions = []; |
38
|
|
|
|
39
|
|
|
foreach ($shippingZones as $shippingZone) { |
40
|
|
|
$shippingZoneDefinitions[$shippingZone->name] = $this->getShippingZoneDefinition($shippingZone); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $shippingZoneDefinitions; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get shipping zones definition. |
48
|
|
|
* |
49
|
|
|
* @param Commerce_ShippingZoneModel $shippingZone |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
private function getShippingZoneDefinition(Commerce_ShippingZoneModel $shippingZone) |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
'name' => $shippingZone->name, |
57
|
|
|
'description' => $shippingZone->description, |
58
|
|
|
'countryBased' => $shippingZone->countryBased, |
59
|
|
|
'default' => $shippingZone->default, |
60
|
|
|
'countries' => $this->getCountryDefinitions($shippingZone->getCountries()), |
61
|
|
|
'states' => $this->getStateDefinitions($shippingZone->getStates()), |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get country definitions. |
67
|
|
|
* |
68
|
|
|
* @param Commerce_CountryModel[] $countries |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
private function getCountryDefinitions(array $countries) |
73
|
|
|
{ |
74
|
|
|
$countryDefinitions = []; |
75
|
|
|
|
76
|
|
|
foreach ($countries as $country) { |
77
|
|
|
$countryDefinitions[] = $country->iso; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $countryDefinitions; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get state definitions. |
85
|
|
|
* |
86
|
|
|
* @param Commerce_StateModel[] $states |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
private function getStateDefinitions(array $states) |
91
|
|
|
{ |
92
|
|
|
$stateDefinitions = []; |
93
|
|
|
|
94
|
|
|
foreach ($states as $state) { |
95
|
|
|
$stateDefinitions[] = $state->abbreviation; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $stateDefinitions; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Attempt to import shipping zones. |
103
|
|
|
* |
104
|
|
|
* @param array $shippingZoneDefinitions |
105
|
|
|
* @param bool $force If set to true shipping zones not included in the import will be deleted |
106
|
|
|
* |
107
|
|
|
* @return Result |
108
|
|
|
*/ |
109
|
|
|
public function import(array $shippingZoneDefinitions, $force = false) |
110
|
|
|
{ |
111
|
|
|
Craft::log(Craft::t('Importing Commerce Shipping Zones')); |
112
|
|
|
|
113
|
|
|
$shippingZones = array(); |
114
|
|
|
foreach (Craft::app()->commerce_shippingZones->getAllShippingZones() as $shippingZone) { |
115
|
|
|
$shippingZones[$shippingZone->name] = $shippingZone; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
foreach ($shippingZoneDefinitions as $shippingZoneDefinition) { |
119
|
|
|
$shippingZoneHandle = $shippingZoneDefinition['name']; |
120
|
|
|
$shippingZone = array_key_exists($shippingZoneHandle, $shippingZones) |
121
|
|
|
? $shippingZones[$shippingZoneHandle] |
122
|
|
|
: new Commerce_ShippingZoneModel(); |
123
|
|
|
|
124
|
|
|
unset($shippingZones[$shippingZoneHandle]); |
125
|
|
|
|
126
|
|
|
$this->populateShippingZone($shippingZone, $shippingZoneDefinition, $shippingZoneHandle); |
127
|
|
|
|
128
|
|
|
$countryIds = $this->getCountryIds($shippingZone); |
129
|
|
|
$stateIds = $this->getStateIds($shippingZone); |
130
|
|
|
|
131
|
|
|
if (!Craft::app()->commerce_shippingZones->saveShippingZone($shippingZone, $countryIds, $stateIds)) { // Save shippingzone via craft |
132
|
|
|
$this->addErrors($shippingZone->getAllErrors()); |
133
|
|
|
|
134
|
|
|
continue; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if ($force) { |
139
|
|
|
foreach ($shippingZones as $shippingZone) { |
140
|
|
|
Craft::app()->commerce_shippingZones->deleteShippingZoneById($shippingZone->id); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this->getResultModel(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Populate shippingZone. |
149
|
|
|
* |
150
|
|
|
* @param Commerce_ShippingZoneModel $shippingZone |
151
|
|
|
* @param array $shippingZoneDefinition |
152
|
|
|
* @param string $shippingZoneHandle |
153
|
|
|
*/ |
154
|
|
|
private function populateShippingZone(Commerce_ShippingZoneModel $shippingZone, array $shippingZoneDefinition, $shippingZoneHandle) |
155
|
|
|
{ |
156
|
|
|
$shippingZone->setAttributes([ |
157
|
|
|
'name' => $shippingZoneHandle, |
158
|
|
|
'description' => $shippingZoneDefinition['description'], |
159
|
|
|
'countryBased' => $shippingZoneDefinition['countryBased'], |
160
|
|
|
'default' => $shippingZoneDefinition['default'], |
161
|
|
|
]); |
162
|
|
|
|
163
|
|
|
$countries = array(); |
164
|
|
|
foreach ($shippingZoneDefinition['countries'] as $iso) { |
165
|
|
|
$countries[] = Craft::app()->commerce_countries->getCountryByAttributes(array('iso' => $iso)); |
166
|
|
|
} |
167
|
|
|
$shippingZone->setCountries($countries); |
168
|
|
|
|
169
|
|
|
$states = array(); |
170
|
|
|
foreach ($shippingZoneDefinition['states'] as $abbreviation) { |
171
|
|
|
$states[] = Craft::app()->commerce_states->getStateByAttributes(array('abbreviation' => $abbreviation)); |
172
|
|
|
} |
173
|
|
|
$shippingZone->setStates($states); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get country ids. |
178
|
|
|
* |
179
|
|
|
* @param Commerce_ShippingZoneModel $shippingZone |
180
|
|
|
* |
181
|
|
|
* @return array |
182
|
|
|
*/ |
183
|
|
|
private function getCountryIds(Commerce_ShippingZoneModel $shippingZone) |
184
|
|
|
{ |
185
|
|
|
$countryIds = array(); |
186
|
|
|
foreach ($shippingZone->getCountries() as $country) { |
187
|
|
|
$countryIds[] = $country->id; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $countryIds; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get state ids. |
195
|
|
|
* |
196
|
|
|
* @param Commerce_ShippingZoneModel $shippingZone |
197
|
|
|
* |
198
|
|
|
* @return array |
199
|
|
|
*/ |
200
|
|
|
private function getStateIds(Commerce_ShippingZoneModel $shippingZone) |
201
|
|
|
{ |
202
|
|
|
$stateIds = array(); |
203
|
|
|
foreach ($shippingZone->getStates() as $state) { |
204
|
|
|
$stateIds[] = $state->id; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $stateIds; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|