PluginProbe
MailerLite – WooCommerce integration / 3.1.16
MailerLite – WooCommerce integration v3.1.16
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 / controllers / WooMailerLiteOrderController.php

WooMailerLiteOrderController.php in MailerLite – WooCommerce integration 3.1.16, at includes/controllers/WooMailerLiteOrderController.php

247 lines 10.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class WooMailerLiteOrderController extends WooMailerLiteController
4 {
5
6 public function handleOrderStatusChanged($orderId)
7 {
8 try {
9
10 if (WooMailerLiteCache::get('order_sent:'.$orderId)) {
11 return true;
12 }
13 $order = wc_get_order($orderId);
14 if (!$order->get_billing_email()) {
15 return true;
16 }
17
18 $this->persistLanguageToOrder($order);
19
20 if (WooMailerLiteSession::getMLCartHash()) {
21 $order->add_meta_data('_woo_ml_cart_hash', WooMailerLiteSession::getMLCartHash());
22 }
23 $customer = WooMailerLiteCustomer::selectAll(false)->where('email', $order->get_billing_email())->first();
24 if (!$customer) {
25 $customer = WooMailerLiteCustomer::selectAll(false)
26 ->getFromOrder()
27 ->where("email", $order->get_billing_email())
28 ->whereIn("status", WooMailerLiteOptions::allSupportedOrderStatuses())
29 ->first();
30 }
31 $cart = WooMailerLiteCart::where('email', $order->get_billing_email())->first();
32 if (!$cart) {
33 $cart = WooMailerLiteCart::where('hash', WooMailerLiteSession::getMLCartHash())
34 ->withoutPrefix(function($query) use ($order) {
35 $query->orWhere('hash', $order->get_meta('_woo_ml_cart_hash'));
36 })->first();
37 if ($cart instanceof WooMailerLiteCart) {
38 $cart->update([
39 'email' => $order->get_billing_email(),
40 ]);
41 }
42 }
43 if (!$cart && !$customer) {
44 return true;
45 }
46
47 if ((isset($cart->subscribe) && $cart->subscribe) || WooMailerLiteOptions::get("settings.checkoutHidden")) {
48 $order->add_meta_data('_woo_ml_subscribe', true);
49 }
50
51 $syncFields = WooMailerLiteOptions::get('syncFields', []);
52 if (empty($syncFields)) {
53 $syncFields = [
54 'name',
55 'email',
56 'company',
57 'city',
58 'zip',
59 'state',
60 'country',
61 'phone'
62 ];
63 WooMailerLiteOptions::update('syncFields', $syncFields);
64 }
65 $filteredCustomerData = array_filter($customer ? $customer->toArray() : [], function($value) {
66 return !is_null($value) && trim($value) !== '';
67 });
68
69 $syncFields[] = 'last_name';
70 if (!in_array('name', $syncFields)) {
71 $syncFields[] = 'name';
72 }
73
74 $customerFields = array_intersect_key($filteredCustomerData, array_flip($syncFields));
75 if (WooMailerLiteOptions::get('settings.languageField')) {
76 $customerFields['subscriber_language'] = $order->get_meta('_woo_ml_language');
77 }
78
79 $subscribe = false;
80 $email = $customer->email ?? $order->get_billing_email();
81 $subscribeCacheKey = 'woo_ml_subscribe_checkbox:'.$email;
82 if ($cart && isset($cart->subscribe)) {
83 $subscribe = $cart->subscribe;
84 }
85
86 if (WooMailerLiteOptions::get("settings.checkoutHidden") || $order->get_meta('_woo_ml_subscribe')) {
87 $subscribe = true;
88 }
89
90 if (WooMailerLiteCache::get($subscribeCacheKey) === null) {
91 WooMailerLiteCache::set($subscribeCacheKey, $subscribe, 20);
92 }
93
94 if (WooMailerLiteCache::get($subscribeCacheKey) === true) {
95 $subscribe = true;
96 }
97
98 $orderCustomer = [
99 'email' => $customer->email ?? $order->get_billing_email(),
100 'create_subscriber' => $subscribe,
101 'accepts_marketing' => $subscribe,
102 'subscriber_fields' => $customerFields,
103 'total_spent' => ($customer->total_spent ?? $order->get_total()),
104 'orders_count' => ($customer->orders_count ?? 1),
105 'last_order_id' => $customer->last_order_id ?? null,
106 'last_order' => $customer->last_order ?? null
107 ];
108 $items = [];
109
110 foreach ($order->get_items() as $item) {
111 if ($item->get_product_id() !== 0) {
112 $items[] = [
113 'product_resource_id' => (string)$item->get_product_id(),
114 'variant' => $item->get_name(),
115 'quantity' => $item->get_quantity(),
116 'price' => (float)$item->get_product()->get_price()
117 ];
118 }
119 }
120 $cartData = [
121 'items' => $items
122 ];
123
124 if ($this->apiClient()->isClassic()) {
125 $orderData['order'] = $order->get_data();
126 $orderData['checkout_id'] = $cart->data['checkout_id'] ?? null;
127 $orderData['order_url'] = home_url() . "/wp-admin/post.php?post=" . $orderId . "&action=edit";
128 $customerFields['woo_total_spent'] = ($customer->total_spent ?? $order->get_total());
129 $customerFields['woo_orders_count'] = ($customer->orders_count ?? 1);
130 $customerFields['woo_last_order_id'] = $customer->last_order_id ?? null;
131 $customerFields['woo_last_order'] = $customer->last_order ?? nulL;
132 $data = [
133 'email' => $customer->email,
134 'checked_sub_to_mailist' => (bool)$subscribe ?? (bool)$cart->subscribe,
135 'checkout_id' => $cart->data['checkout_id'] ?? null,
136 'order_id' => $orderId,
137 'payment_method' => $order->get_payment_method(),
138 'fields' => $customerFields,
139 'shop_url' => home_url(),
140 'order_url' => home_url() . "/wp-admin/post.php?post=" . $orderId . "&action=edit",
141 'checkout_data' => WooMailerLiteCheckoutDataService::getCheckoutData($order->get_billing_email())
142 ];
143 $this->apiClient()->sendOrderProcessing($data);
144 if (WooMailerLiteOptions::isCompleteOrderStatus($order->get_status()) && $order->get_items()) {
145 $order_items = $order->get_items();
146 foreach ($order_items as $key => $value) {
147 $item_data = $value->get_data();
148 $orderData['order']['line_items'][$key] = $item_data;
149 $orderData['order']['line_items'][$key]['ignored_product'] = in_array($item_data['product_id'],
150 array_map('strval', array_keys(WooMailerLiteOptions::get('ignored_products', [])))) ? 1 : 0;
151 }
152 $orderData['order']['status'] = 'completed';
153 WooMailerLiteCache::set('order_sent:'.$orderId, true, 20);
154 $response = $this->apiClient()->syncOrder(home_url(), $orderData);
155 $this->apiClient()->sendSubscriberData($data);
156 }
157
158 } else {
159 $date = null;
160 if ($order->get_date_created()) {
161 $date = $order->get_date_created()->format('Y-m-d H:i:s');
162 }
163
164 $stage = WooMailerLiteOptions::isCompleteOrderStatus($order->get_status()) ? null : 'checkout';
165 $response = $this->apiClient()->syncOrder(
166 WooMailerLiteOptions::get('shopId'),
167 $orderId,
168 $orderCustomer,
169 $cartData,
170 $order->get_status(),
171 $order->get_total(),
172 $date,
173 $stage
174 );
175 }
176
177 if (isset($response) && $response->success) {
178 $order->add_meta_data('_woo_ml_order_data_submitted', true);
179 if (!empty($cart) && $cart instanceof WooMailerLiteCart) {
180 if ($this->apiClient()->isRewrite()) {
181 $this->apiClient()->deleteOrder($cart->data['checkout_id']);
182 }
183 if (WooMailerLiteOptions::isCompleteOrderStatus($order->get_status())) {
184 $cart->delete();
185 }
186 }
187
188 $createdAt = null;
189 $updatedAt = null;
190
191 if ($this->apiClient()->isClassic()) {
192 $response = $this->apiClient()->searchSubscriber($customer->email);
193
194 // For classic response, we have accesss to the date_created and date_updated fields
195 if ($response->success) {
196 $createdAt = $response->data->date_created;
197 $updatedAt = $response->data->date_updated;
198 }
199 }
200
201 // For rewrite response, we have accesss to the subscriber object
202 if (isset($response->data->customer->subscriber)) {
203 $createdAt = $response->data->customer->subscriber->created_at;
204 $updatedAt = $response->data->customer->subscriber->updated_at;
205 }
206
207 // We will only set the order meta when we have these dates
208 if ($createdAt && $updatedAt) {
209 $createdAt = strtotime($createdAt);
210 $updatedAt = strtotime($updatedAt);
211
212 // If the created and updated timestamps are within 60 seconds, the subscriber was just created
213 if (abs($createdAt - $updatedAt) < 60) {
214 $order->add_meta_data('_woo_ml_subscribed', true);
215 } else {
216 $order->add_meta_data('_woo_ml_already_subscribed', true);
217 $order->add_meta_data('_woo_ml_subscriber_updated', true);
218 }
219 }
220
221 $order->add_meta_data('_woo_ml_order_tracked', true);
222 }
223 $order->save();
224 } catch(\Throwable $e) {
225 WooMailerLiteLog()->error('handleOrderStatusChanged', [
226 'error' => $e->getMessage(),
227 'trace' => $e->getTraceAsString(),
228 'orderId' => $orderId
229 ]);
230
231 return true;
232 }
233 }
234
235 private function persistLanguageToOrder($order): void
236 {
237 $key = '_woo_ml_language';
238 if (isset(WC()->session)) {
239 $language = WC()->session->get($key);
240 if ($language && !$order->get_meta($key)) {
241 $order->add_meta_data($key, $language);
242 $order->save();
243 }
244 }
245 }
246 }
247