PluginProbe
AliNext – WooCommerce Dropshipping Plugin for AliExpress / trunk
AliNext – WooCommerce Dropshipping Plugin for AliExpress vtrunk
ali2woo-lite / includes / classes / service / OrderShippingDataService.php

OrderShippingDataService.php in AliNext – WooCommerce Dropshipping Plugin for AliExpress trunk, at includes/classes/service/OrderShippingDataService.php

326 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Builds order shipping data for external API.
5 *
6 * @author Ali2Woo Team
7 */
8
9 namespace AliNext_Lite;;
10
11 use WC_Order;
12 use WooCommerceOrderItem;
13
14 class OrderShippingDataService
15 {
16 public function buildOrderContent(WC_Order $order, int $postId): array
17 {
18 $def_prefship = get_setting('fulfillment_prefship');
19 $def_customer_note = get_setting('fulfillment_custom_note');
20 $def_phone_number = get_setting('fulfillment_phone_number');
21 $def_phone_code = get_setting('fulfillment_phone_code');
22
23 $content = array(
24 'id' => $postId,
25 'defaultShipping' => $def_prefship,
26 'note' => $def_customer_note !== "" ? $def_customer_note : $this->get_customer_note($order),
27 'products' => array(),
28 'countryRegion' => $this->get_country_region($order),
29 'region' => strtolower($this->get_region($order)),
30 'city' => $this->get_city($order),
31 'contactName' => $this->get_contactName($order),
32 'address1' => $this->get_address1($order),
33 'address2' => $this->get_address2($order),
34 'mobile' => $def_phone_number !== "" ? $def_phone_number : $this->get_phone($order),
35 'mobile_code' => $def_phone_code !== "" ? $def_phone_code : '',
36 'zip' => $this->get_zip($order),
37 'autopay' => false,
38 'awaitingpay' => false,
39 'cpf' => $this->get_cpf($order),
40 'storeurl' => get_site_url(),
41 'currency' => $this->get_currency($order),
42 );
43
44 $items = $order->get_items();
45
46 $k = 0;
47 $total = 0;
48 foreach ($items as $item) {
49
50 $normalized_item = new WooCommerceOrderItem($item);
51 $product_id = $normalized_item->get_product_id();
52 $variation_id = $normalized_item->get_variation_id();
53 $quantity = $normalized_item->get_quantity();
54
55 $external_id = get_post_meta($product_id, '_a2w_external_id', true);
56
57 if ($external_id) {
58
59 $skuArray = $this->getSkuArray($normalized_item);
60
61 if (empty($skuArray) && $variation_id && $variation_id > 0) {
62 throw new \RuntimeException('', -2);
63 }
64
65 $original_url = get_post_meta($product_id, '_a2w_product_url', true);
66
67 if (empty($original_url)) {
68 throw new \RuntimeException('', -3);
69 }
70
71 $shipping_service_name = $normalized_item->get_ali_shipping_code();
72
73 $content['products'][$k] = array(
74 'url' => $original_url,
75 'productId' => $external_id,
76 'originalId' => $product_id,
77 'qty' => $quantity,
78 'sku' => $skuArray,
79 'shipping' => $shipping_service_name,
80 );
81
82 $k++;
83 }
84
85 $total++;
86 }
87
88 if ($k < 1) {
89 throw new \RuntimeException('', -4);
90 }
91
92 $action = $k === $total ? 'upd_ord_status' : '';
93
94 return array('state' => 'ok', 'data' => array('content' => $content, 'id' => $postId), 'action' => $action);
95 }
96
97 private function format_field($str): string
98 {
99 $str = trim($str);
100
101 if (!empty($str)) {
102 $str = ucwords(strtolower($str));
103 }
104
105 return $str;
106 }
107
108 private function get_currency($order): string
109 {
110 return strtolower($order->get_currency());
111 }
112
113 private function get_cpf($order)
114 {
115 $b_cpf = $order->get_meta('_billing_cpf');
116 $s_cpf = $order->get_meta('_shipping_cpf');
117
118 $cpf = $b_cpf ?: ($s_cpf ?: '');
119
120 return $cpf ? preg_replace("/[^0-9]/", "", $cpf) : '';
121 }
122
123 private function get_phone($order)
124 {
125 if (WC()->version < '3.0.0') {
126 $result = $order->billing_phone ?: $order->shipping_phone;
127 } else {
128 $result = $order->get_billing_phone();
129 }
130
131 return preg_replace('/[^0-9]+/', '', $result);
132 }
133
134 private function get_customer_note($order)
135 {
136 if (WC()->version < '3.0.0') {
137 $result = $order->customer_note;
138 } else {
139 $result = $order->get_customer_note();
140 }
141
142 return $this->translitirate($result);
143 }
144
145 private function get_country_region($order)
146 {
147 if (WC()->version < '3.0.0') {
148 $result = $order->shipping_country ? $this->format_field_country($order->shipping_country) : $this->format_field_country($order->billing_country);
149 } else {
150 $result = $order->get_shipping_country() ? $this->format_field_country($order->get_shipping_country()) : $this->format_field_country($order->get_billing_country());
151 }
152
153 return $this->translitirate($result);
154 }
155
156 private function get_region($order)
157 {
158 if (WC()->version < '3.0.0') {
159 $result = $order->shipping_state ? $this->format_field_state($order->shipping_country, $order->shipping_state) : $this->format_field_state($order->billing_country, $order->billing_state);
160 } else {
161 $result = $order->get_shipping_state() ? $this->format_field_state($order->get_shipping_country(), $order->get_shipping_state()) : $this->format_field_state($order->get_billing_country(), $order->get_billing_state());
162 }
163
164 return $this->translitirate($result);
165 }
166
167 private function get_city($order)
168 {
169
170 if (WC()->version < '3.0.0') {
171 $result = $order->shipping_city ? $this->format_field($order->shipping_city) : $this->format_field($order->billing_city);
172 } else {
173 $result = $order->get_shipping_city() ? $this->format_field($order->get_shipping_city()) : $this->format_field($order->get_billing_city());
174 }
175
176 return $this->translitirate($result);
177 }
178
179 private function get_contactName($order)
180 {
181 if (WC()->version < '3.0.0') {
182 if ($order->shipping_first_name) {
183 $result = $order->shipping_first_name . ' ' . $order->shipping_last_name;
184 } else {
185 $result = $order->billing_first_name . ' ' . $order->billing_last_name;
186 }
187
188 } else {
189 $result2 = $order->get_shipping_first_name() . ' ' .
190 $order->get_shipping_last_name() . ' ' . $order->get_meta('_shipping_third_name');
191 $result3 = $order->get_billing_first_name() . ' ' .
192 $order->get_billing_last_name() . ' ' . $order->get_meta('_billing_third_name');
193 $result = $order->get_shipping_first_name() ? $result2 : $result3;
194 }
195
196 return $this->translitirate($result);
197 }
198
199 private function get_address_number($order)
200 {
201 $b_number = $order->get_meta('_billing_number');
202 $s_number = $order->get_meta('_shipping_number');
203
204 $number = $b_number ?: ($s_number ?: '');
205
206 return $number ? preg_replace("/[^0-9]/", "", $number) : '';
207 }
208
209 private function get_address1($order)
210 {
211 if (WC()->version < '3.0.0') {
212 $result = $order->shipping_address_1 ?: $order->billing_address_1;
213 } else {
214 $result = $order->get_shipping_address_1() ?
215 $order->get_shipping_address_1() : $order->get_billing_address_1();
216 }
217
218 $result = $result . " " . $this->get_address_number($order);
219
220 return $this->translitirate($result);
221 }
222
223 private function get_address2($order)
224 {
225 if (WC()->version < '3.0.0') {
226 $result = $order->shipping_address_2 ?: $order->billing_address_2;
227 } else {
228 $result = $order->get_shipping_address_2() ?
229 $order->get_shipping_address_2() : $order->get_billing_address_2();
230 }
231
232 return $this->translitirate($result);
233 }
234
235 private function get_zip($order)
236 {
237 if (WC()->version < '3.0.0') {
238 $result = $order->shipping_postcode ?: $order->billing_postcode;
239 } else {
240 $result = $order->get_shipping_postcode() ?
241 $order->get_shipping_postcode() : $order->get_billing_postcode();
242 }
243
244 return $result;
245 }
246
247 private function format_field_country($str): string
248 {
249 $str = trim($str);
250
251 if (!empty($str)) {
252 $str = strtoupper($str);
253 }
254
255 if ($str === "GB") {
256 $str = "UK";
257 }
258
259 if ($str == "RS") {
260 $str = "SRB";
261 }
262
263 if ($str == "ME") {
264 $str = "MNE";
265 }
266
267 return $str;
268 }
269
270 private function format_field_state($country_code, $state_code): string
271 {
272 $condition = isset(WC()->countries->states[$country_code]) &&
273 isset(WC()->countries->states[$country_code][$state_code]);
274 if ($condition) {
275 $result = $this->format_field(WC()->countries->states[$country_code][$state_code]);
276 } else {
277 $result = $state_code;
278 }
279
280 return html_entity_decode($result, ENT_QUOTES, 'UTF-8');
281 }
282
283 private function getSkuArray($item): array
284 {
285 if ($item->get_variation_id() !== 0) {
286 $variation_id = $item->get_variation_id();
287 $sku = $this->getSkuArrayByVariationID($variation_id);
288
289 } else {
290 $product_id = $item->get_product_id();
291 $sku = $this->getSkuArrayByVariationID($product_id);
292 }
293 return $sku;
294 }
295
296 private function getSkuArrayByVariationID($variation_id): array
297 {
298 $sku = array();
299
300 $external_var_data = get_post_meta($variation_id, '_aliexpress_sku_props', true);
301
302 if (empty($external_var_data)) {
303 return $sku;
304 }
305
306 if ($external_var_data) {
307 $items = explode(';', $external_var_data);
308
309 foreach ($items as $item) {
310 list(, $sku[]) = explode(':', $item);
311 }
312 }
313
314 return $sku;
315 }
316
317 private function translitirate($result)
318 {
319 if (get_setting('order_translitirate')) {
320 $result = Utils::safeTransliterate($result);
321 }
322
323 return $result;
324 }
325 }
326