PluginProbe
MailerLite – WooCommerce integration / 2.0.5
MailerLite – WooCommerce integration v2.0.5
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 / OrderSyncProcess.php

OrderSyncProcess.php in MailerLite – WooCommerce integration 2.0.5, at includes/classes/process/OrderSyncProcess.php

180 lines 5.9 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\MailerLiteSettings;
6 use MailerLite\Includes\Classes\Singleton;
7 use MailerLite\Includes\Shared\Api\ApiType;
8 use MailerLite\Includes\Shared\Api\PlatformAPI;
9
10 class OrderSyncProcess extends Singleton
11 {
12 /**
13 * Call to handle bulk order update event
14 * mailerlite_wp_sync_bulk_order
15 */
16 public function syncBulkOrder($action, $order_ids)
17 {
18 $orderProcess = OrderProcess::getInstance();
19 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
20
21 if ($mailerliteClient->getApiType() == ApiType::CURRENT) {
22
23 foreach ($order_ids as $order_id) {
24
25 $order = wc_get_order($order_id);
26
27 $shop = get_option('woo_ml_shop_id', false);
28
29 if ($shop === false) {
30
31 return false;
32 }
33
34 // we check the action, the new order status is not yet available
35 if ($action === 'mark_cancelled') {
36
37 // woocommerce_order_status_cancelled action will delete the order
38 return false;
39 }
40
41 $subscribe = $orderProcess->orderCustomerSubscribe($order_id);
42
43 $cart_details = CartProcess::getInstance()->getCartDetails($order_id);
44
45 $customer_data = $orderProcess->getCustomerDataFromOrder($order_id);
46 $subscriber_fields = $this->getSubscriberFieldsFromCustomerData($customer_data);
47
48 // rename zip key for API
49 $zip = $subscriber_fields['zip'] ?? '';
50 unset($subscriber_fields['zip']);
51 $subscriber_fields['z_i_p'] = $zip;
52
53 $order_customer = [
54 'email' => $order->get_billing_email(),
55 'create_subscriber' => $subscribe,
56 'accepts_marketing' => $subscribe,
57 'subscriber_fields' => $subscriber_fields
58 ];
59
60 $customer_fields = OrderProcess::getInstance()->getOrderTrackingData($order_id);
61
62 if ($customer_fields['customer_id'] !== 0) {
63 $order_customer['resource_id'] = (string)$customer_fields['customer_id'];
64 }
65
66 $order_customer['orders_count'] = $customer_fields['orders_count'];
67 $order_customer['total_spent'] = $customer_fields['total_spent'];
68
69 $order_cart = [
70 'items' => $cart_details['items']
71 ];
72
73 $order_status = $order->get_status();
74
75 if (is_numeric($order_id)) {
76 $order_status = 'completed';
77 }
78
79 $mailerliteClient->syncOrder(
80 $shop,
81 $order_id,
82 $order_customer,
83 $order_cart,
84 $order_status,
85 $order->get_total(),
86 date('Y-m-d h:m:s', strtotime($order->get_date_created()))
87 );
88 }
89 }
90 }
91
92 /**
93 * Call to handle order update event
94 * mailerlite_wp_sync_order
95 */
96 public function syncOrder($order_id, $post)
97 {
98 $orderProcess = OrderProcess::getInstance();
99 $order = wc_get_order($order_id);
100
101 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
102
103 if ($mailerliteClient->getApiType() == ApiType::CURRENT) {
104
105 $shop = get_option('woo_ml_shop_id', false);
106
107 if ($shop === false) {
108
109 return false;
110 }
111
112 $subscribe = $orderProcess->orderCustomerSubscribe($order_id);
113
114 $cart_details = CartProcess::getInstance()->getCartDetails($order_id);
115
116 $customer_data = $orderProcess->getCustomerDataFromOrder($order_id);
117 $subscriber_fields = $this->getSubscriberFieldsFromCustomerData($customer_data);
118
119 //rename zip key for API
120 $zip = $subscriber_fields['zip'] ?? '';
121 unset($subscriber_fields['zip']);
122 $subscriber_fields['z_i_p'] = $zip;
123
124 $order_customer = [
125 'email' => $order->get_billing_email(),
126 'create_subscriber' => $subscribe,
127 'accepts_marketing' => $subscribe,
128 'subscriber_fields' => $subscriber_fields
129 ];
130
131 $customer_fields = OrderProcess::getInstance()->getOrderTrackingData($order_id);
132
133 if ($customer_fields['customer_id'] !== 0) {
134 $order_customer['resource_id'] = (string)$customer_fields['customer_id'];
135 }
136
137 $order_customer['orders_count'] = $customer_fields['orders_count'];
138 $order_customer['total_spent'] = $customer_fields['total_spent'];
139
140 $order_cart = [
141 'items' => $cart_details['items']
142 ];
143
144 $order_status = $order->get_status();
145
146 if (is_numeric($order_id)) {
147 $order_status = 'completed';
148 }
149
150 // check for latest order status
151 if (isset($post['order_status'])) {
152
153 $order_status = $post['order_status'];
154 }
155
156 // check if order is cancelled
157 if ($order_status === 'wc-cancelled') {
158
159 // woocommerce_order_status_cancelled action will delete the order
160 return true;
161 }
162
163 $mailerliteClient->syncOrder($shop, $order_id, $order_customer, $order_cart, $order_status,
164 $order->get_total(), date('Y-m-d h:m:s', strtotime($order->get_date_created())));
165
166 if ($mailerliteClient->responseCode() === 201) {
167
168 $orderProcess->completeOrderDataSubmitted($order_id);
169
170 return true;
171 }
172 }
173 }
174
175 private function getSubscriberFieldsFromCustomerData($customer_data)
176 {
177 return MailerLiteSettings::getInstance()->getSubscriberFieldsFromCustomerData($customer_data);
178 }
179
180 }