PluginProbe
MailerLite – WooCommerce integration / 3.1.18
MailerLite – WooCommerce integration v3.1.18
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 / WooMailerLiteService.php

WooMailerLiteService.php in MailerLite – WooCommerce integration 3.1.18, at includes/WooMailerLiteService.php

226 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class WooMailerLiteService
4 {
5 /**
6 * @var null|static $instance
7 */
8 protected static $instance = null;
9
10 protected $apiClient;
11
12 public function __construct()
13 {
14 $this->apiClient = WooMailerLiteApi::client();
15 }
16
17 public static function instance()
18 {
19 if (!empty(static::$instance)) {
20 return static::$instance;
21 }
22
23 static::$instance = new static();
24 return static::$instance;
25 }
26
27 /**
28 * Triggered when the cart is created/updated
29 * @return true
30 */
31 public function handleCartUpdated()
32 {
33 WooMailerLiteSession::set('woo_mailerlite_cart_hash', WC()->session->get_customer_id());
34 $cart = WooMailerLiteCart::where('hash', WooMailerLiteSession::getMLCartHash())->first();
35 $data = WooMailerLiteSession::cart();
36 $data = json_decode($data, true);
37 if ($cart && isset($cart->data['checkout_id'])) {
38 $data['checkout_id'] = $cart->data['checkout_id'];
39 } elseif (!isset($data['checkout_id'])) {
40 $data['checkout_id'] = wp_generate_uuid4();
41 }
42
43 $data = json_encode($data);
44 $subscribe = textInput('signup', 'false') === 'true' ? 1 : 0;
45
46 if (!$cart) {
47 WooMailerLiteCart::create([
48 'hash' => WooMailerLiteSession::getMLCartHash(),
49 'email' => WooMailerLiteSession::billingEmail(),
50 'subscribe' => $subscribe,
51 'data' => $data,
52 ]);
53
54 } else {
55 $cart->update([
56 'email' => WooMailerLiteSession::billingEmail(),
57 'subscribe' => $subscribe,
58 'data' => $data,
59 ]);
60 }
61
62 $cacheKey = WooMailerLiteSession::getMLCartStageCacheKey();
63 if (is_user_logged_in() && ! WooMailerLiteCache::get($cacheKey)) {
64 WooMailerLiteCache::set($cacheKey, true, 5);
65 $this->sendCart('cart');
66 }
67
68 return true;
69 }
70
71 /**
72 * Triggered when a logged-in user visits the checkout page.
73 * Updates the cart stage to 'checkout' and syncs to MailerLite
74 * because we already have the email and other details in the session for logged-in users.
75 * @return void
76 */
77 public function handleCheckoutPage()
78 {
79 if (is_user_logged_in()) {
80 $this->sendCart();
81 }
82 }
83
84 /**
85 * Triggered when the checkout form data is changed
86 * @return void
87 */
88 public function setCartEmail()
89 {
90 check_ajax_referer('woo_mailerlite_cart_nonce', 'nonce');
91 $email = emailInput('email');
92 $subscribe = textInput('signup', 'false') === 'true' ? 1 : 0;
93 WooMailerLiteSession::set('woo_mailerlite_checkbox', boolval($subscribe));
94 // set the customer in session
95 $data = WooMailerLiteSession::cart();
96 $data = json_decode($data, true);
97 if (!isset($data['checkout_id'])) {
98 $data['checkout_id'] = wp_generate_uuid4();
99 }
100
101 if (!WooMailerLiteSession::getMLCartHash()) {
102 $this->handleCartUpdated();
103 }
104
105 WooMailerLiteSession::set('woo_mailerlite_customer_data', ['customer' => $_POST, 'cart' => WC()->session->get( 'woo_mailerlite_cart_hash')]);
106 // find the cart by cart id
107 $cart = WooMailerLiteCart::where('hash', WooMailerLiteSession::getMLCartHash())->first();
108
109 // update email in cart
110 if ($cart) {
111 $cart->update([
112 'email' => $email,
113 'subscribe' => $subscribe,
114 ]);
115 } else {
116 // create cart if it doesn't exist
117 WooMailerLiteCart::create([
118 'hash' => WooMailerLiteSession::getMLCartHash(),
119 'email' => $email,
120 'subscribe' => $subscribe,
121 'data' => $data,
122 ]);
123 }
124 $this->sendCart();
125 }
126
127 public function sendCart($stage = 'checkout')
128 {
129 if (WooMailerLiteOptions::get('settings.syncAfterCheckout')) {
130 return true;
131 }
132 $customer = WooMailerLiteSession::getMLCustomer();
133 $checkoutData = WooMailerLiteCheckoutDataService::getCheckoutData($customer['customer']['email'] ?? null);
134 $customerQuery = WooMailerLiteCustomer::where('email', $customer['customer']['email'])->first();
135 try {
136 if (self::instance()->apiClient->isClassic()) {
137 home_url();
138 self::instance()->apiClient->sendCart(home_url(), $checkoutData);
139 return true;
140 }
141
142 if (self::instance()->apiClient->isRewrite()) {
143 $shop = WooMailerLiteOptions::get('shopId');
144
145 if ($shop === false) {
146 return false;
147 }
148
149 $orderCustomer = [
150 'email' => $checkoutData['email'],
151 'accepts_marketing' => $checkoutData['subscribe'] ?? false,
152 'create_subscriber' => $checkoutData['subscribe'] ?? false,
153 ];
154
155
156 if (isset($checkoutData['subscriber_fields'])) {
157 $orderCustomer['subscriber_fields'] = array_merge($orderCustomer['subscriber_fields'] ?? [],
158 $checkoutData['subscriber_fields']);
159 }
160 if ($customerQuery) {
161 $customerQuery->name = $customer['customer']['name'] ?? '';
162 $orderCustomer['subscriber_fields'] = array_merge($orderCustomer['subscriber_fields'] ?? [], $customerQuery->toArray());
163 } else {
164 $orderCustomer['subscriber_fields'] = array_merge($orderCustomer['subscriber_fields'] ?? [],
165 $customer['customer'] ?? []);
166 }
167
168 $orderCustomer['subscriber_fields'] = array_filter(
169 $orderCustomer['subscriber_fields'],
170 function ($v, $k) {
171 return in_array($k, WooMailerLiteOptions::get("settings.syncFields"));
172 },
173 ARRAY_FILTER_USE_BOTH
174 );
175
176 if (isset($checkoutData['language'])) {
177 $orderCustomer['subscriber_fields']['subscriber_language'] = $checkoutData['language'];
178 $orderCustomer['subscriber_fields']['language'] = $checkoutData['language'];
179 }
180
181 $orderCart = [
182 'resource_id' => (string)$checkoutData['id'],
183 'checkout_url' => $checkoutData['abandoned_checkout_url'],
184 'items' => []
185 ];
186
187 if (empty($checkoutData['line_items'])) {
188 self::$instance->apiClient->deleteOrder($checkoutData['id']);
189 return false;
190 }
191
192 foreach ($checkoutData['line_items'] as $item) {
193 $product = wc_get_product($item['product_id']);
194
195 $orderCart['items'][] = [
196 'product_resource_id' => (string)$item['product_id'],
197 'variant' => $product->get_name(),
198 'quantity' => (int)$item['quantity'],
199 'price' => floatval($product->get_price('edit')),
200 ];
201 }
202
203 self::$instance->apiClient->syncOrder(
204 $shop,
205 $checkoutData['id'],
206 $orderCustomer,
207 $orderCart,
208 'pending',
209 $checkoutData['total_price'],
210 $checkoutData['created_at'],
211 $stage
212 );
213 }
214 } catch (\Exception $e) {
215 WooMailerLiteLog()->error('sendCart', [
216 'error' => $e->getMessage(),
217 'trace' => $e->getTraceAsString()
218 ]);
219
220 return false;
221 }
222
223 return true;
224 }
225 }
226