PluginProbe
MailerLite – WooCommerce integration / 3.1.27
MailerLite – WooCommerce integration v3.1.27
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.27, at includes/WooMailerLiteService.php

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