PluginProbe
MailerLite – WooCommerce integration / 2.0.2
MailerLite – WooCommerce integration v2.0.2
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.2, at includes/classes/process/OrderSyncProcess.php

191 lines 6.2 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 if (strval($cart_details['customer_id']) !== "0") {
61
62 $order_customer['resource_id'] = (string)$cart_details['customer_id'];
63
64 // check customer fields
65 $wc_customer = new \WC_Customer($cart_details['customer_id']);
66
67 if ( ! empty($wc_customer->get_email())) {
68
69 $order_customer['orders_count'] = $wc_customer->get_order_count();
70 $order_customer['total_spent'] = $wc_customer->get_total_spent();
71 }
72 }
73
74 $order_cart = [
75 'items' => $cart_details['items']
76 ];
77
78 $order_status = $order->get_status();
79
80 if (is_numeric($order_id)) {
81 $order_status = 'completed';
82 }
83
84 $mailerliteClient->syncOrder(
85 $shop,
86 $order_id,
87 $order_customer,
88 $order_cart,
89 $order_status,
90 $order->get_total(),
91 date('Y-m-d h:m:s', strtotime($order->get_date_created()))
92 );
93 }
94 }
95 }
96
97 /**
98 * Call to handle order update event
99 * mailerlite_wp_sync_order
100 */
101 public function syncOrder($order_id, $post)
102 {
103 $orderProcess = OrderProcess::getInstance();
104 $order = wc_get_order($order_id);
105
106 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
107
108 if ($mailerliteClient->getApiType() == ApiType::CURRENT) {
109
110 $shop = get_option('woo_ml_shop_id', false);
111
112 if ($shop === false) {
113
114 return false;
115 }
116
117 $subscribe = $orderProcess->orderCustomerSubscribe($order_id);
118
119 $cart_details = CartProcess::getInstance()->getCartDetails($order_id);
120
121 $customer_data = $orderProcess->getCustomerDataFromOrder($order_id);
122 $subscriber_fields = $this->getSubscriberFieldsFromCustomerData($customer_data);
123
124 //rename zip key for API
125 $zip = $subscriber_fields['zip'] ?? '';
126 unset($subscriber_fields['zip']);
127 $subscriber_fields['z_i_p'] = $zip;
128
129 $order_customer = [
130 'email' => $order->get_billing_email(),
131 'create_subscriber' => $subscribe,
132 'accepts_marketing' => $subscribe,
133 'subscriber_fields' => $subscriber_fields
134 ];
135
136 if (strval($cart_details['customer_id']) !== "0") {
137
138 $order_customer['resource_id'] = (string)$cart_details['customer_id'];
139
140 // check customer fields
141 $wc_customer = new \WC_Customer($cart_details['customer_id']);
142
143 if ( ! empty($wc_customer->get_email())) {
144
145 $order_customer['orders_count'] = $wc_customer->get_order_count();
146 $order_customer['total_spent'] = $wc_customer->get_total_spent();
147 }
148
149 }
150
151 $order_cart = [
152 'items' => $cart_details['items']
153 ];
154
155 $order_status = $order->get_status();
156
157 if (is_numeric($order_id)) {
158 $order_status = 'completed';
159 }
160
161 // check for latest order status
162 if (isset($post['order_status'])) {
163
164 $order_status = $post['order_status'];
165 }
166
167 // check if order is cancelled
168 if ($order_status === 'wc-cancelled') {
169
170 // woocommerce_order_status_cancelled action will delete the order
171 return true;
172 }
173
174 $mailerliteClient->syncOrder($shop, $order_id, $order_customer, $order_cart, $order_status,
175 $order->get_total(), date('Y-m-d h:m:s', strtotime($order->get_date_created())));
176
177 if ($mailerliteClient->responseCode() === 201) {
178
179 $orderProcess->completeOrderDataSubmitted($order_id);
180
181 return true;
182 }
183 }
184 }
185
186 private function getSubscriberFieldsFromCustomerData($customer_data)
187 {
188 return MailerLiteSettings::getInstance()->getSubscriberFieldsFromCustomerData($customer_data);
189 }
190
191 }