PluginProbe
MailerLite – WooCommerce integration / 2.0.4
MailerLite – WooCommerce integration v2.0.4
3.1.27 3.1.25 3.1.26 3.1.24 3.1.23 3.1.22 3.1.21 3.1.20 3.1.19 3.1.18 3.1.17 3.1.16 3.1.15 1.8.7 1.8.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 All 148 releases
woo-mailerlite / includes / classes / process / OrderProcess.php

OrderProcess.php in MailerLite – WooCommerce integration 2.0.4, at includes/classes/process/OrderProcess.php

829 lines 27.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace MailerLite\Includes\Classes\Process;
4
5 use MailerLite\Includes\Classes\Settings\ApiSettings;
6 use MailerLite\Includes\Classes\Settings\MailerLiteSettings;
7 use MailerLite\Includes\Classes\Singleton;
8 use MailerLite\Includes\Shared\Api\ApiType;
9 use MailerLite\Includes\Shared\Api\PlatformAPI;
10
11 class OrderProcess extends Singleton
12 {
13
14 /**
15 * Process order tracking
16 * 1.) Get current data from WooCommerce
17 * 2.) Update subscriber data with updated values
18 * woo_ml_process_order_tracking
19 *
20 * @param $order_id
21 */
22 public function processOrderTracking($order_id)
23 {
24
25 $order = wc_get_order($order_id);
26
27 $order_tracked = $order->get_meta('_woo_ml_order_tracking');
28
29 if ($order_tracked) // Prevent tracking orders multiple times
30 {
31 return;
32 }
33
34 // Step 1: Get order tracking data from order
35 $tracking_data = $this->getOrderTrackingData($order_id);
36
37 if (!empty($tracking_data)) {
38
39 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
40
41 if ($mailerliteClient->getApiType() == ApiType::CLASSIC) {
42 if(get_option('mailerlite_disable_checkout_sync')) {
43 $data = [];
44 $customer_data = $this->getCustomerDataFromOrder($order_id);
45 $data['email'] = $customer_data['email'];
46 $subscribe = $order->get_meta('_woo_ml_subscribe');
47
48 if (isset($_POST['woo_ml_subscribe']) && '1' == $_POST['woo_ml_subscribe'] && $subscribe === "") {
49 $subscribe = true;
50 }
51
52 if ($subscribe === "") {
53 $subscribe = MailerLiteSettings::getInstance()->getMlOption('checkout_hide') === 'yes';
54 }
55
56 if ($subscribe) {
57 $order->add_meta_data('_woo_ml_subscribe', true, true);
58
59 // save instead of save_meta_data for pending payment orders
60 $order->save();
61 }
62 $data['checked_sub_to_mailist'] = $subscribe;
63 $data['checkout_id'] = $_COOKIE['mailerlite_checkout_token'] ?? CheckoutProcess::getInstance()->getSavedCheckoutIdByEmail($customer_data['email']);
64 $data['order_id'] = $order_id;
65 $data['payment_method'] = $order->get_payment_method();
66
67 $subscriber_fields = MailerLiteSettings::getInstance()->getSubscriberFieldsFromCustomerData($customer_data);
68 if (sizeof($subscriber_fields) > 0) {
69 $data['fields'] = $subscriber_fields;
70 }
71 $this->addSubscriberSaveOrder($data, 'order_created');
72 }
73 $ml_subscriber_obj = mailerlite_wp_get_subscriber_by_email($tracking_data['email']);
74
75 // Customer exists on MailerLite
76 if ($ml_subscriber_obj) {
77
78 // Step 2: Update subscriber data via API
79 $customer_data = $this->getCustomerDataFromOrder($order_id);
80 $subscriber_data = [
81 'fields' => [
82 'woo_orders_count' => $tracking_data['orders_count'],
83 'woo_total_spent' => $tracking_data['total_spent'],
84 'woo_last_order' => $tracking_data['last_order'],
85 'woo_last_order_id' => $order_id
86 ]
87 ];
88 $subscriber_data['fields'] = array_merge($subscriber_data['fields'], MailerLiteSettings::getInstance()->getSubscriberFieldsFromCustomerData($customer_data));
89
90 $subscriber_updated = mailerlite_wp_update_subscriber($tracking_data['email'], $subscriber_data);
91
92 if ($subscriber_updated) {
93
94 $this->completeOrderDataSubmitted($order_id);
95 }
96 }
97 }
98 }
99
100 // Mark order data as tracked
101 $this->completeOrderTracking($order_id);
102 }
103
104 /**
105 * Get order tracking data from order
106 * woo_ml_get_order_tracking_data
107 *
108 * @param int $order_id
109 *
110 * @return array
111 * @throws Exception
112 */
113 public function getOrderTrackingData($order_id)
114 {
115
116 if (!is_numeric($order_id)) {
117 return [];
118 }
119
120 $customer_email = '';
121 $order_count = 0;
122 $total_spent = 0;
123 $last_order_date = '';
124
125 $order = wc_get_order($order_id);
126
127 $customer = \WC_Data_Store::load('report-customers')->get_data([
128 'order_before' => null,
129 'order_after' => null,
130 'searchby' => 'email',
131 'search' => $order->get_billing_email(),
132 ])->data;
133
134 $customer_email = $order->get_billing_email();
135
136 if (count($customer)) {
137 foreach ($customer as $value) {
138 if ( ! isset($customer['last_order_date'])) {
139 $customer['last_order_date'] = $value['date_last_order'];
140 }
141
142 if ($customer['last_order_date'] < $value['date_last_order']) {
143 $customer['last_order_date'] = $value['date_last_order'];
144 }
145
146 $order_count += $value['orders_count'] ?? 0;
147 $total_spent += $value['total_spend'] ?? 0;
148 }
149
150 $last_order_date = $customer['last_order_date'];
151 $dateCreated = $order->get_date_created();
152 // sometimes when an order is placed, the last order is not included with the orders count
153 if (isset($dateCreated) && ($last_order_date < $order->get_date_created()->date_i18n('Y-m-d H:i:s'))) {
154 $last_order_date = $order->get_date_created()->date_i18n('Y-m-d H:i:s');
155
156 $order_count++;
157 $total_spent += $order->get_total();
158 }
159 } else {
160 $last_order_date = $order->get_date_created()->date_i18n('Y-m-d H:i:s');
161
162 $order_count++;
163 $total_spent += $order->get_total();
164 }
165
166 return [
167 'email' => $customer_email,
168 'customer_id' => $customer[0]['id'] ?? 0,
169 'orders_count' => $order_count,
170 'total_spent' => $total_spent,
171 'last_order' => $last_order_date,
172 'last_order_id' => $order_id
173 ];
174 }
175
176 /**
177 * Process order create and subscription to newsletter
178 * woo_ml_process_order_subscription
179 *
180 * @param $order_id
181 *
182 * @return void
183 */
184 public function processOrderSubscription($order_id)
185 {
186 $order = wc_get_order($order_id);
187 $customer_data = $this->getCustomerDataFromOrder($order_id);
188
189 $subscribe = $order->get_meta('_woo_ml_subscribe');
190 if (isset($_POST['woo_ml_subscribe']) && '1' == $_POST['woo_ml_subscribe'] && $subscribe === "") {
191 $subscribe = true;
192 }
193 if ($subscribe === "") {
194 $subscribe = MailerLiteSettings::getInstance()->getMlOption('checkout_hide') === 'yes';
195
196 if ($subscribe) {
197 $order->add_meta_data('_woo_ml_subscribe', true, true);
198
199 // save instead of save_meta_data for pending payment orders
200 $order->save();
201 }
202 }
203
204
205 $data = [];
206 $data['email'] = $customer_data['email'];
207 $data['checked_sub_to_mailist'] = $subscribe;
208 $checkout_id = isset($_COOKIE['mailerlite_checkout_token']) ? $_COOKIE['mailerlite_checkout_token'] : CheckoutProcess::getInstance()->getSavedCheckoutIdByEmail($customer_data['email']);
209
210 $data['checkout_id'] = $checkout_id;
211 $data['order_id'] = $order_id;
212 $data['payment_method'] = $order->get_payment_method();
213
214 if ($data['payment_method'] == 'bacs' || $data['payment_method'] == 'cheque') {
215 @setcookie('mailerlite_checkout_email', null, -1, '/');
216 @setcookie('mailerlite_checkout_token', null, -1, '/');
217 @setcookie('mailerlite_accepts_marketing', null, -1, '/');
218 } else {
219 $data['checkout_data'] = CheckoutProcess::getInstance()->getCheckoutData();
220 }
221 $subscriber_fields = MailerLiteSettings::getInstance()->getSubscriberFieldsFromCustomerData($customer_data);
222 if (sizeof($subscriber_fields) > 0) {
223 $data['fields'] = $subscriber_fields;
224 }
225
226 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
227
228 if ($mailerliteClient->getApiType() === ApiType::CLASSIC) {
229
230 $subscriber_result = $this->addSubscriberSaveOrder($data, 'order_created');
231
232 if (isset($subscriber_result->added_to_group)) {
233 if ($subscriber_result->added_to_group) {
234 $this->markOrderSubscribedViaApi($order_id);
235 } else {
236 $this->completeOrderCustomerAlreadySubscribed($order_id);
237 }
238 }
239
240 if (isset($subscriber_result->updated_fields) && $subscriber_result->updated_fields) {
241 $this->markOrderSubscriberUpdated($order_id);
242 }
243 }
244
245 if ($mailerliteClient->getApiType() === ApiType::CURRENT) {
246
247 $shop = get_option('woo_ml_shop_id', false);
248
249 if ($shop === false) {
250
251 return;
252 }
253
254 $subscribe = $this->orderCustomerSubscribe($order_id);
255
256 $cart_details = CartProcess::getInstance()->getCartDetails($order_id);
257
258 //rename zip key for API
259 $zip = $subscriber_fields['zip'] ?? '';
260 unset($subscriber_fields['zip']);
261 $subscriber_fields['z_i_p'] = $zip;
262
263 $order_customer = [
264 'email' => $data['email'],
265 'create_subscriber' => $subscribe,
266 'accepts_marketing' => $subscribe,
267 'subscriber_fields' => $subscriber_fields
268 ];
269
270 $customer_fields = $this->getOrderTrackingData($order_id);
271
272 if ($customer_fields['customer_id'] !== 0) {
273 $order_customer['resource_id'] = (string)$customer_fields['customer_id'];
274 }
275
276 $order_customer['orders_count'] = $customer_fields['orders_count'];
277 $order_customer['total_spent'] = $customer_fields['total_spent'];
278
279 $order_cart = [
280 'items' => $cart_details['items']
281 ];
282
283 // Delete existing cart (abandoned checkout)
284 $checkout_id = $_COOKIE['mailerlite_checkout_token'] ?? CheckoutProcess::getInstance()->getSavedCheckoutIdByEmail($data['email']);
285
286 if ($checkout_id !== null) {
287
288 $mailerliteClient->deleteOrder($shop, $checkout_id);
289
290 @setcookie('mailerlite_checkout_email', null, -1, '/');
291 @setcookie('mailerlite_checkout_token', null, -1, '/');
292 @setcookie('mailerlite_accepts_marketing', null, -1, '/');
293
294 CheckoutProcess::getInstance()->removeCheckout($data['email']);
295 }
296
297 // set order status completed (when processing and paid)
298 if ($cart_details['status'] == 'processing') {
299 $cart_details['status'] = 'completed';
300 }
301
302 // Create order
303 $order_create = $mailerliteClient->syncOrder($shop, $order_id, $order_customer, $order_cart,
304 $cart_details['status'], $cart_details['total_price'], $cart_details['created_at']);
305
306 if ($order_create) {
307
308 $this->completeOrderDataSubmitted($order_id);
309 }
310 }
311 }
312
313 /**
314 * Get customer data from order
315 * woo_ml_get_customer_data_from_order
316 *
317 * @param $order_id
318 *
319 * @return array|bool
320 */
321 public function getCustomerDataFromOrder($order_id)
322 {
323
324 if (empty($order_id)) {
325 return false;
326 }
327
328 $order = wc_get_order($order_id);
329
330 if (method_exists($order, 'get_billing_email')) {
331 $data = array(
332 'email' => $order->get_billing_email(),
333 'name' => "{$order->get_billing_first_name()} {$order->get_billing_last_name()}",
334 'first_name' => $order->get_billing_first_name(),
335 'last_name' => $order->get_billing_last_name(),
336 'company' => $order->get_billing_company(),
337 'city' => $order->get_billing_city(),
338 'postcode' => $order->get_billing_postcode(),
339 'state' => $order->get_billing_state(),
340 'country' => $order->get_billing_country(),
341 'phone' => $order->get_billing_phone()
342 );
343 } else {
344 // NOTE: Only for compatibility with WooCommerce < 3.0
345 $data = array(
346 'email' => $order->billing_email,
347 'name' => "{$order->billing_first_name} {$order->billing_last_name}",
348 'first_name' => $order->billing_first_name,
349 'last_name' => $order->billing_last_name,
350 'company' => $order->billing_company,
351 'city' => $order->billing_city,
352 'postcode' => $order->billing_postcode,
353 'state' => $order->billing_state,
354 'country' => $order->billing_country,
355 'phone' => $order->billing_phone
356 );
357 }
358
359 return $data;
360 }
361
362 /**
363 * Check whether a customer wants to be subscribed to our mailing list or not
364 * woo_ml_order_customer_subscribe
365 *
366 * @param $order_id
367 *
368 * @return bool
369 */
370 public function orderCustomerSubscribe($order_id)
371 {
372
373 $order = wc_get_order($order_id);
374
375 $subscribe = $order->get_meta('_woo_ml_subscribe');
376
377 return ('1' == $subscribe) ? true : false;
378 }
379
380 /**
381 * Mark order as "wants to be subscribed to mailing our list"
382 * woo_ml_set_order_customer_subscribe
383 *
384 * @param $order_id
385 */
386 public function setOrderCustomerSubscribe($order_id)
387 {
388 $order = wc_get_order($order_id);
389
390 $order->add_meta_data('_woo_ml_subscribe', true);
391 $order->save_meta_data();
392 }
393
394 /**
395 * Check whether order data was submitted via API or not
396 * woo_ml_order_data_submitted
397 *
398 * @param $order_id
399 *
400 * @return bool
401 */
402 public function orderDataSubmitted($order_id)
403 {
404
405 $order = wc_get_order($order_id);
406
407 $data_submitted = $order->get_meta('_woo_ml_order_data_submitted');
408
409 return ('1' == $data_submitted) ? true : false;
410 }
411
412 /**
413 * Mark order as "order data submitted"
414 * woo_ml_complete_order_data_submitted
415 *
416 * @param $order_id
417 */
418 public function completeOrderDataSubmitted($order_id)
419 {
420 $order = wc_get_order($order_id);
421
422 $order->add_meta_data('_woo_ml_order_data_submitted', true);
423 $order->save_meta_data();
424 }
425
426 /**
427 * Gets triggered on completed order event. Fetches order data
428 * and passes it along to api
429 * woo_ml_send_completed_order
430 *
431 * @param Integer $order_id
432 *
433 * @return void
434 */
435 public function sendCompletedOrder($order_id)
436 {
437 $order = wc_get_order($order_id);
438 $order_data['order'] = $order->get_data();
439 $order_items = $order->get_items();
440 $customer_email = $order->get_billing_email();
441
442 $saved_checkout = CheckoutProcess::getInstance()->getSavedCheckoutByEmail($customer_email);
443 $order_data['checkout_id'] = !empty($saved_checkout) ? $saved_checkout->checkout_id : null;
444
445 $ignored_products = MailerLiteSettings::getInstance()->remapList(ProductProcess::getInstance()->getIgnoredProductList());
446
447 foreach ($order_items as $key => $value) {
448 $item_data = $value->get_data();
449 $order_data['order']['line_items'][$key] = $item_data;
450 $order_data['order']['line_items'][$key]['ignored_product'] = in_array($item_data['product_id'],
451 $ignored_products) ? 1 : 0;
452 }
453 @setcookie('mailerlite_checkout_email', null, -1, '/');
454 @setcookie('mailerlite_checkout_token', null, -1, '/');
455 @setcookie('mailerlite_accepts_marketing', null, -1, '/');
456
457 if ($order_data['order']['status'] == 'processing') {
458 $order_data['order']['status'] = 'completed';
459 }
460
461 $this->wpSendOrder($order_data);
462
463 if (!empty($saved_checkout)) {
464
465 CheckoutProcess::getInstance()->removeCheckout($customer_email);
466 }
467
468 }
469
470 /**
471 * Check whether order was already tracked or not
472 * woo_ml_order_tracking_completed
473 *
474 * @param $order_id
475 *
476 * @return bool
477 */
478 public function checkOrderTracking($order_id)
479 {
480 $order = wc_get_order($order_id);
481
482 $order_tracked = $order->get_meta('_woo_ml_order_tracked');
483
484 return ('1' == $order_tracked) ? true : false;
485 }
486
487 /**
488 * Mark order as being tracked
489 * woo_ml_complete_order_tracking
490 *
491 * @param $order_id
492 */
493 public function completeOrderTracking($order_id)
494 {
495 $order = wc_get_order($order_id);
496
497 $order->add_meta_data('_woo_ml_order_tracked', true);
498 $order->save_meta_data();
499 }
500
501 /**
502 * On change of order status to processing send order data
503 * woo_ml_payment_status_processing
504 *
505 * @param Integer $order_id
506 *
507 * @return void
508 */
509 public function paymentStatusProcessing($order_id)
510 {
511 $order = wc_get_order($order_id);
512
513 if ($order->get_status() === 'processing') {
514 $data = [];
515 $customer_email = $order->get_billing_email();
516
517 // load the checkout id from the cookie first
518 // if that fails, then check the mailerlite checkouts table
519 $checkoutId = null;
520 if (isset($_COOKIE['mailerlite_checkout_token'])) {
521
522 $checkoutId = $_COOKIE['mailerlite_checkout_token'];
523 } else {
524
525 $saved_checkout = CheckoutProcess::getInstance()->getSavedCheckoutByEmail($customer_email);
526 $checkoutId = !empty($saved_checkout) ? $saved_checkout->checkout_id : null;
527 }
528
529 $data['checkout_id'] = $checkoutId;
530 $data['order_id'] = $order_id;
531 $data['payment_method'] = $order->get_payment_method();
532
533 @setcookie('mailerlite_checkout_email', null, -1, '/');
534 @setcookie('mailerlite_checkout_token', null, -1, '/');
535 @setcookie('mailerlite_accepts_marketing', null, -1, '/');
536
537 $this->addSubscriberSaveOrder($data, 'order_processing');
538 CheckoutProcess::getInstance()->removeCheckout($customer_email);
539
540 if ((int)get_option('woo_mailerlite_platform', 1) === ApiType::CURRENT) {
541
542 $this->processOrderSubscription($order_id);
543 }
544 }
545 }
546
547 /**
548 * Mark order as "subscriber was updated"
549 * woo_ml_complete_order_subscriber_updated
550 *
551 * @param $order_id
552 */
553 public function markOrderSubscriberUpdated($order_id)
554 {
555 $order = wc_get_order($order_id);
556
557 $order->add_meta_data('_woo_ml_subscriber_updated', true);
558 $order->save_meta_data();
559 }
560
561 /**
562 * Check whether a subscriber was updated from order or not
563 * woo_ml_order_subscriber_updated
564 *
565 * @param $order_id
566 *
567 * @return bool
568 */
569 public function checkOrderSubscriberUpdated($order_id)
570 {
571 $order = wc_get_order($order_id);
572
573 $subscriber_updated_from_order = $order->get_meta('_woo_ml_subscriber_updated');
574
575 return ('1' == $subscriber_updated_from_order) ? true : false;
576 }
577
578 /**
579 *
580 * woo_ml_complete_order_customer_already_subscribed
581 *
582 * @param $order_id
583 *
584 * @return void
585 */
586 public function completeOrderCustomerAlreadySubscribed($order_id)
587 {
588 $order = wc_get_order($order_id);
589
590 $order->add_meta_data('_woo_ml_already_subscribed', true);
591 $order->save_meta_data();
592 }
593
594 /**
595 * Mark order as "customer subscribed via API"
596 * woo_ml_complete_order_customer_subscribed
597 *
598 * @param $order_id
599 */
600 public function markOrderSubscribedViaApi($order_id)
601 {
602 $order = wc_get_order($order_id);
603
604 $order->add_meta_data('_woo_ml_subscribed', true);
605 $order->save_meta_data();
606 }
607
608 /**
609 *
610 * woo_ml_order_customer_already_subscribed
611 *
612 * @param $order_id
613 *
614 * @return bool
615 */
616 public function orderCustomerAlreadySubscribed($order_id)
617 {
618
619 $order = wc_get_order($order_id);
620
621 $already_subscribed = $order->get_meta('_woo_ml_already_subscribed');
622
623 return ('1' == $already_subscribed) ? true : false;
624 }
625
626 /**
627 * Check whether a customer was subscribed via API or not
628 *subscribe
629 * woo_ml_order_customer_subscribed
630 *
631 * @param $order_id
632 *
633 * @return bool
634 */
635 public function checkCustomerSubscribed($order_id)
636 {
637
638 $order = wc_get_order($order_id);
639
640 $subscribed = $order->get_meta('_woo_ml_subscribed');
641
642 return ('1' == $subscribed) ? true : false;
643 }
644
645 /**
646 * Sending order data on creation of order and/or order status change to processing
647 * mailerlite_wp_add_subscriber_and_save_order
648 *
649 * @param array $data
650 * @param string $event
651 *
652 * @return bool
653 */
654 public function addSubscriberSaveOrder($data, $event)
655 {
656 if (!ApiSettings::getInstance()->wpApiKeyExists()) {
657 return false;
658 }
659
660 try {
661 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
662
663 $shop_id = '';
664
665 $shop_url = site_url();
666 $data['shop_url'] = home_url();
667 $data['order_url'] = $shop_url . "/wp-admin/post.php?post=" . $data['order_id'] . "&action=edit";
668 //order_created case also takes care of processing sub if they have ticked the box to
669 //receive newsletters
670
671 if ($mailerliteClient->getApiType() === ApiType::CLASSIC) {
672 if ($event === 'order_created') {
673 $result = $mailerliteClient->sendSubscriberData($shop_id, $data);
674
675 if (isset($result->added_to_group) && isset($result->updated_fields)) {
676 return $result;
677 } elseif (isset($result->deactivate) && $result->deactivate) {
678 woo_ml_deactivate_woo_ml_plugin(true);
679
680 return false;
681 } else {
682 return false;
683 }
684 } else {
685
686 $result = $mailerliteClient->sendOrderProcessing($shop_id, $data);
687 if (isset($result->deactivate) && $result->deactivate) {
688 woo_ml_deactivate_woo_ml_plugin(true);
689 }
690
691 if ($event === 'order_processing') {
692 $this->sendCompletedOrder($data['order_id']);
693 $this->processOrderTracking($data['order_id']);
694 }
695
696 return true;
697 }
698 }
699
700 } catch (\Exception $e) {
701 return false;
702 }
703 }
704
705 /**
706 * Sends completed order data to api to be evaluated and saved and/if trigger automations
707 * mailerlite_wp_send_order
708 *
709 * @param array $order_data
710 *
711 * @return bool|void
712 */
713 public function wpSendOrder($order_data)
714 {
715 if (!ApiSettings::getInstance()->wpApiKeyExists()) {
716 return false;
717 }
718
719 try {
720 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
721
722 if ($mailerliteClient->getApiType() == ApiType::CLASSIC) {
723
724 $store = home_url();
725
726 $shop_url = site_url();
727 $order_data['order_url'] = $shop_url . "/wp-admin/post.php?post=" . $order_data['order']['id'] . "&action=edit";
728
729 $result = $mailerliteClient->saveOrder($store, $order_data);
730
731 if (isset($result->deactivate) && $result->deactivate) {
732 woo_ml_deactivate_woo_ml_plugin(true);
733 }
734 }
735
736 if ($mailerliteClient->getApiType() == ApiType::CURRENT) {
737
738 $shop = get_option('woo_ml_shop_id', false);
739
740 if ($shop === false) {
741
742 return false;
743 }
744
745 $subscribe = $this->orderCustomerSubscribe($order_data['order']['id']);
746
747 $cart_details = CartProcess::getInstance()->getCartDetails($order_data['order']['id']);
748
749 $customer_data = $this->getCustomerDataFromOrder($order_data['order']['id']);
750 $subscriber_fields = MailerLiteSettings::getInstance()->getSubscriberFieldsFromCustomerData($customer_data);
751
752 //rename zip key for API
753 $zip = $subscriber_fields['zip'] ?? '';
754 unset($subscriber_fields['zip']);
755 $subscriber_fields['z_i_p'] = $zip;
756
757 $order_customer = [
758 'email' => $order_data['order']['billing']['email'],
759 'create_subscriber' => $subscribe,
760 'accepts_marketing' => $subscribe,
761 'subscriber_fields' => $subscriber_fields
762 ];
763
764 $customer_fields = $this->getOrderTrackingData($order_data['order']['id']);
765
766 if ($customer_fields['customer_id'] !== 0) {
767 $order_customer['resource_id'] = (string)$customer_fields['customer_id'];
768 }
769
770 $order_customer['orders_count'] = $customer_fields['orders_count'];
771 $order_customer['total_spent'] = $customer_fields['total_spent'];
772
773 $order_cart = [
774 'items' => $cart_details['items']
775 ];
776
777 if (is_numeric($order_data['order']['id'])) {
778 $order_data['order']['status'] = 'completed';
779 }
780
781 $mailerliteClient->syncOrder($shop, $order_data['order']['id'], $order_customer, $order_cart,
782 $order_data['order']['status'], $cart_details['total_price'], $cart_details['created_at']);
783
784 if ($mailerliteClient->responseCode() === 201 || $mailerliteClient->responseCode() === 200) {
785
786 $this->orderDataSubmitted($order_data['order']['id']);
787
788 // Delete existing cart (abandoned checkout)
789 if ($order_data['checkout_id'] !== null) {
790
791 $mailerliteClient->deleteOrder($shop, $order_data['checkout_id']);
792 }
793 }
794
795 return true;
796 }
797 } catch (\Exception $e) {
798 return false;
799 }
800 }
801
802 /**
803 * Delete the order on cancellation
804 *
805 * @param $order_id
806 *
807 * @return bool
808 */
809 public function cancelOrder($order_id)
810 {
811 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
812
813 if ($mailerliteClient->getApiType() == ApiType::CURRENT) {
814 $order = wc_get_order($order_id);
815
816 $shop = get_option('woo_ml_shop_id', false);
817
818 if ($shop === false) {
819 return false;
820 }
821
822 if ($order) {
823 $mailerliteClient->deleteOrder($shop, $order_id);
824 }
825 }
826
827 return true;
828 }
829 }