1 | <?php |
||
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 = []) |
||
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) |
||
64 | |||
65 | /** |
||
66 | * Get rule definitions. |
||
67 | * |
||
68 | * @param Commerce_ShippingRuleModel[] $rules |
||
69 | * |
||
70 | * @return array |
||
71 | */ |
||
72 | private function getRuleDefinitions(array $rules) |
||
82 | |||
83 | /** |
||
84 | * Get rule definition. |
||
85 | * |
||
86 | * @param Commerce_ShippingRuleModel $rule |
||
87 | * |
||
88 | * @return array |
||
89 | */ |
||
90 | private function getRuleDefinition(Commerce_ShippingRuleModel $rule) |
||
113 | |||
114 | /** |
||
115 | * Get category definitions. |
||
116 | * |
||
117 | * @param Commerce_ShippingRuleCategoryModel[] $categories |
||
118 | * |
||
119 | * @return array |
||
120 | */ |
||
121 | private function getCategoryDefinitions(array $categories) |
||
131 | |||
132 | /** |
||
133 | * Get category definition. |
||
134 | * |
||
135 | * @param Commerce_ShippingRuleCategoryModel $category |
||
136 | * |
||
137 | * @return array |
||
138 | */ |
||
139 | private function getCategoryDefinition(Commerce_ShippingRuleCategoryModel $category) |
||
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 | $shippingMethods = array(); |
||
163 | foreach (Craft::app()->commerce_shippingMethods->getAllShippingMethods() as $shippingMethod) { |
||
164 | $shippingMethods[$shippingMethod->handle] = $shippingMethod; |
||
165 | } |
||
166 | |||
167 | foreach ($shippingMethodDefinitions as $shippingMethodHandle => $shippingMethodDefinition) { |
||
168 | $shippingMethod = array_key_exists($shippingMethodHandle, $shippingMethods) |
||
169 | ? $shippingMethods[$shippingMethodHandle] |
||
170 | : new Commerce_ShippingMethodModel(); |
||
171 | |||
172 | unset($shippingMethods[$shippingMethodHandle]); |
||
173 | |||
174 | $this->populateShippingMethod($shippingMethod, $shippingMethodDefinition, $shippingMethodHandle); |
||
175 | |||
176 | if (!Craft::app()->commerce_shippingMethods->saveShippingMethod($shippingMethod)) { // Save shippingmethod via craft |
||
177 | $this->addErrors($shippingMethod->getAllErrors()); |
||
178 | |||
179 | continue; |
||
180 | } |
||
181 | |||
182 | $this->populateShippingMethodRules($shippingMethod, $shippingMethodDefinition['rules']); |
||
183 | } |
||
184 | |||
185 | if ($force) { |
||
186 | foreach ($shippingMethods as $shippingMethod) { |
||
187 | Craft::app()->commerce_shippingMethods->deleteShippingMethodById($shippingMethod->id); |
||
188 | } |
||
189 | } |
||
190 | |||
191 | return $this->getResultModel(); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Populate shippingmethod. |
||
196 | * |
||
197 | * @param Commerce_ShippingMethodModel $shippingMethod |
||
198 | * @param array $shippingMethodDefinition |
||
199 | * @param string $shippingMethodHandle |
||
200 | */ |
||
201 | private function populateShippingMethod(Commerce_ShippingMethodModel $shippingMethod, array $shippingMethodDefinition, $shippingMethodHandle) |
||
202 | { |
||
203 | $shippingMethod->setAttributes([ |
||
204 | 'handle' => $shippingMethodHandle, |
||
205 | 'name' => $shippingMethodDefinition['name'], |
||
206 | 'enabled' => $shippingMethodDefinition['enabled'], |
||
207 | ]); |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Populate shipping method rules. |
||
212 | * |
||
213 | * @param Commerce_ShippingMethodModel $shippingMethod |
||
214 | * @param $ruleDefinitions |
||
215 | */ |
||
216 | private function populateShippingMethodRules(Commerce_ShippingMethodModel $shippingMethod, $ruleDefinitions) |
||
258 | |||
259 | /** |
||
260 | * Populate shipping method rule categories. |
||
261 | * |
||
262 | * @param Commerce_ShippingRuleModel $rule |
||
263 | * @param $categoryDefinitions |
||
264 | */ |
||
265 | private function populateShippingMethodRuleCategories(Commerce_ShippingRuleModel $rule, $categoryDefinitions) |
||
288 | |||
289 | /** |
||
290 | * Reset service cache using reflection. |
||
291 | */ |
||
292 | private function resetCraftShippingMethodsServiceCache() |
||
302 | } |
||
303 |