1 | <?php |
||
23 | class ShippingMethods extends Base |
||
24 | { |
||
25 | /** |
||
26 | * Export shippingMethods. |
||
27 | * |
||
28 | * @param ShippingMethodModel[] $shippingMethods |
||
29 | * |
||
30 | * @return array |
||
31 | */ |
||
32 | 2 | public function export(array $shippingMethods = []) |
|
48 | |||
49 | /** |
||
50 | * Get shipping methods definition. |
||
51 | * |
||
52 | * @param Commerce_ShippingMethodModel $shippingMethod |
||
53 | * |
||
54 | * @return array |
||
55 | */ |
||
56 | 2 | private function getShippingMethodDefinition(Commerce_ShippingMethodModel $shippingMethod) |
|
64 | |||
65 | /** |
||
66 | * Get rule definitions. |
||
67 | * |
||
68 | * @param Commerce_ShippingRuleModel[] $rules |
||
69 | * |
||
70 | * @return array |
||
71 | */ |
||
72 | 2 | private function getRuleDefinitions(array $rules) |
|
73 | { |
||
74 | 2 | $ruleDefinitions = []; |
|
75 | |||
76 | 2 | foreach ($rules as $rule) { |
|
77 | 2 | $ruleDefinitions[$rule->name] = $this->getRuleDefinition($rule); |
|
78 | 2 | } |
|
79 | |||
80 | 2 | return $ruleDefinitions; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * Get rule definition. |
||
85 | * |
||
86 | * @param Commerce_ShippingRuleModel $rule |
||
87 | * |
||
88 | * @return array |
||
89 | */ |
||
90 | 2 | private function getRuleDefinition(Commerce_ShippingRuleModel $rule) |
|
91 | { |
||
92 | return [ |
||
93 | 2 | 'name' => $rule->name, |
|
94 | 2 | 'description' => $rule->description, |
|
95 | 2 | 'shippingZone' => $rule->shippingZone ? $rule->shippingZone->name : null, |
|
96 | 2 | 'priority' => $rule->priority, |
|
97 | 2 | 'enabled' => $rule->enabled, |
|
98 | 2 | 'minQty' => $rule->minQty, |
|
99 | 2 | 'maxQty' => $rule->maxQty, |
|
100 | 2 | 'minTotal' => $rule->minTotal, |
|
101 | 2 | 'maxTotal' => $rule->maxTotal, |
|
102 | 2 | 'minWeight' => $rule->minWeight, |
|
103 | 2 | 'maxWeight' => $rule->maxWeight, |
|
104 | 2 | 'baseRate' => $rule->baseRate, |
|
105 | 2 | 'perItemRate' => $rule->perItemRate, |
|
106 | 2 | 'weightRate' => $rule->weightRate, |
|
107 | 2 | 'percentageRate' => $rule->percentageRate, |
|
108 | 2 | 'minRate' => $rule->minRate, |
|
109 | 2 | 'maxRate' => $rule->maxRate, |
|
110 | 2 | 'categories' => $this->getCategoryDefinitions($rule->getShippingRuleCategories()), |
|
111 | 2 | ]; |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * Get category definitions. |
||
116 | * |
||
117 | * @param Commerce_ShippingRuleCategoryModel[] $categories |
||
118 | * |
||
119 | * @return array |
||
120 | */ |
||
121 | 2 | private function getCategoryDefinitions(array $categories) |
|
122 | { |
||
123 | 2 | $categoryDefinitions = []; |
|
124 | |||
125 | 2 | foreach ($categories as $category) { |
|
126 | 2 | $categoryDefinitions[$category->getCategory()->handle] = $this->getCategoryDefinition($category); |
|
127 | 2 | } |
|
128 | |||
129 | 2 | return $categoryDefinitions; |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * Get category definition. |
||
134 | * |
||
135 | * @param Commerce_ShippingRuleCategoryModel $category |
||
136 | * |
||
137 | * @return array |
||
138 | */ |
||
139 | 2 | private function getCategoryDefinition(Commerce_ShippingRuleCategoryModel $category) |
|
140 | { |
||
141 | return [ |
||
142 | 2 | 'condition' => $category->condition, |
|
143 | 2 | 'perItemRate' => $category->perItemRate, |
|
144 | 2 | 'weightRate' => $category->weightRate, |
|
145 | 2 | 'percentageRate' => $category->percentageRate, |
|
146 | 2 | ]; |
|
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 | 4 | public function import(array $shippingMethodDefinitions, $force = false) |
|
193 | |||
194 | /** |
||
195 | * Populate shippingmethod. |
||
196 | * |
||
197 | * @param Commerce_ShippingMethodModel $shippingMethod |
||
198 | * @param array $shippingMethodDefinition |
||
199 | * @param string $shippingMethodHandle |
||
200 | */ |
||
201 | 2 | private function populateShippingMethod(Commerce_ShippingMethodModel $shippingMethod, array $shippingMethodDefinition, $shippingMethodHandle) |
|
209 | |||
210 | /** |
||
211 | * Populate shipping method rules. |
||
212 | * |
||
213 | * @param Commerce_ShippingMethodModel $shippingMethod |
||
214 | * @param array $ruleDefinitions |
||
215 | * @param bool $force |
||
216 | */ |
||
217 | private function populateShippingMethodRules(Commerce_ShippingMethodModel $shippingMethod, $ruleDefinitions, $force = false) |
||
267 | |||
268 | /** |
||
269 | * Populate shipping method rule categories. |
||
270 | * |
||
271 | * @param Commerce_ShippingRuleModel $rule |
||
272 | * @param $categoryDefinitions |
||
273 | */ |
||
274 | private function populateShippingMethodRuleCategories(Commerce_ShippingRuleModel $rule, $categoryDefinitions) |
||
299 | |||
300 | /** |
||
301 | * Reset service cache using reflection. |
||
302 | */ |
||
303 | 4 | private function resetCraftShippingMethodsServiceCache() |
|
313 | |||
314 | /** |
||
315 | * Reset service cache using reflection. |
||
316 | */ |
||
317 | private function resetCraftShippingCategoriesServiceCache() |
||
337 | } |
||
338 |