| 1 |
<?php |
| 2 |
|
| 3 |
namespace MailerLite\Includes\Classes\Process; |
| 4 |
|
| 5 |
use MailerLite\Includes\Classes\Settings\ApiSettings; |
| 6 |
use MailerLite\Includes\Classes\Settings\MailerLiteSettings; |
| 7 |
use MailerLite\Includes\Classes\Singleton; |
| 8 |
use MailerLite\Includes\Shared\Api\ApiType; |
| 9 |
use MailerLite\Includes\Shared\Api\PlatformAPI; |
| 10 |
|
| 11 |
class OrderProcess extends Singleton |
| 12 |
{ |
| 13 |
|
| 14 |
/** |
| 15 |
* Process order tracking |
| 16 |
* 1.) Get current data from WooCommerce |
| 17 |
* 2.) Update subscriber data with updated values |
| 18 |
* woo_ml_process_order_tracking |
| 19 |
* |
| 20 |
* @param $order_id |
| 21 |
*/ |
| 22 |
public function processOrderTracking($order_id) |
| 23 |
{ |
| 24 |
|
| 25 |
$order = wc_get_order($order_id); |
| 26 |
|
| 27 |
$order_tracked = $order->get_meta('_woo_ml_order_tracking'); |
| 28 |
|
| 29 |
if ($order_tracked) // Prevent tracking orders multiple times |
| 30 |
{ |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
// Step 1: Get order tracking data from order |
| 35 |
$tracking_data = $this->getOrderTrackingData($order_id); |
| 36 |
|
| 37 |
if (!empty($tracking_data)) { |
| 38 |
|
| 39 |
$mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); |
| 40 |
|
| 41 |
if ($mailerliteClient->getApiType() == ApiType::CLASSIC) { |
| 42 |
if(get_option('mailerlite_disable_checkout_sync')) { |
| 43 |
$data = []; |
| 44 |
$customer_data = $this->getCustomerDataFromOrder($order_id); |
| 45 |
$data['email'] = $customer_data['email']; |
| 46 |
$subscribe = $order->get_meta('_woo_ml_subscribe'); |
| 47 |
|
| 48 |
if (isset($_POST['woo_ml_subscribe']) && '1' == $_POST['woo_ml_subscribe'] && $subscribe === "") { |
| 49 |
$subscribe = true; |
| 50 |
} |
| 51 |
|
| 52 |
if ($subscribe === "") { |
| 53 |
$subscribe = MailerLiteSettings::getInstance()->getMlOption('checkout_hide') === 'yes'; |
| 54 |
} |
| 55 |
|
| 56 |
if ($subscribe) { |
| 57 |
$order->add_meta_data('_woo_ml_subscribe', true, true); |
| 58 |
|
| 59 |
// save instead of save_meta_data for pending payment orders |
| 60 |
$order->save(); |
| 61 |
} |
| 62 |
$data['checked_sub_to_mailist'] = $subscribe; |
| 63 |
$data['checkout_id'] = $_COOKIE['mailerlite_checkout_token'] ?? CheckoutProcess::getInstance()->getSavedCheckoutIdByEmail($customer_data['email']); |
| 64 |
$data['order_id'] = $order_id; |
| 65 |
$data['payment_method'] = $order->get_payment_method(); |
| 66 |
|
| 67 |
$subscriber_fields = MailerLiteSettings::getInstance()->getSubscriberFieldsFromCustomerData($customer_data); |
| 68 |
if (sizeof($subscriber_fields) > 0) { |
| 69 |
$data['fields'] = $subscriber_fields; |
| 70 |
} |
| 71 |
$this->addSubscriberSaveOrder($data, 'order_created'); |
| 72 |
} |
| 73 |
$ml_subscriber_obj = mailerlite_wp_get_subscriber_by_email($tracking_data['email']); |
| 74 |
|
| 75 |
// Customer exists on MailerLite |
| 76 |
if ($ml_subscriber_obj) { |
| 77 |
|
| 78 |
// Step 2: Update subscriber data via API |
| 79 |
$customer_data = $this->getCustomerDataFromOrder($order_id); |
| 80 |
$subscriber_data = [ |
| 81 |
'fields' => [ |
| 82 |
'woo_orders_count' => $tracking_data['orders_count'], |
| 83 |
'woo_total_spent' => $tracking_data['total_spent'], |
| 84 |
'woo_last_order' => $tracking_data['last_order'], |
| 85 |
'woo_last_order_id' => $order_id |
| 86 |
] |
| 87 |
]; |
| 88 |
$orderStatus = $order->get_status(); |
| 89 |
if ($orderStatus == 'completed' || $orderStatus == 'processing') { |
| 90 |
if (isset($tracking_data['customer_id']) && (int)$tracking_data['customer_id']) { |
| 91 |
$lastSyncedCustomer = get_option('woo_ml_last_synced_customer', 0); |
| 92 |
if (($lastSyncedCustomer > 0) && ($lastSyncedCustomer < $tracking_data['customer_id'])) { |
| 93 |
update_option('woo_ml_last_synced_customer', $tracking_data['customer_id']); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
$subscriber_data['fields'] = array_merge($subscriber_data['fields'], MailerLiteSettings::getInstance()->getSubscriberFieldsFromCustomerData($customer_data)); |
| 98 |
|
| 99 |
$subscriber_updated = mailerlite_wp_update_subscriber($tracking_data['email'], $subscriber_data); |
| 100 |
|
| 101 |
if ($subscriber_updated) { |
| 102 |
|
| 103 |
$this->completeOrderDataSubmitted($order_id); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// Mark order data as tracked |
| 110 |
$this->completeOrderTracking($order_id); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Get order tracking data from order |
| 115 |
* woo_ml_get_order_tracking_data |
| 116 |
* |
| 117 |
* @param int $order_id |
| 118 |
* |
| 119 |
* @return array |
| 120 |
* @throws Exception |
| 121 |
*/ |
| 122 |
public function getOrderTrackingData($order_id) |
| 123 |
{ |
| 124 |
|
| 125 |
if (!is_numeric($order_id)) { |
| 126 |
return []; |
| 127 |
} |
| 128 |
|
| 129 |
$customer_email = ''; |
| 130 |
$order_count = 0; |
| 131 |
$total_spent = 0; |
| 132 |
$last_order_date = ''; |
| 133 |
|
| 134 |
$order = wc_get_order($order_id); |
| 135 |
|
| 136 |
$customer = \WC_Data_Store::load('report-customers')->get_data([ |
| 137 |
'order_before' => null, |
| 138 |
'order_after' => null, |
| 139 |
'searchby' => 'email', |
| 140 |
'search' => $order->get_billing_email(), |
| 141 |
])->data; |
| 142 |
|
| 143 |
$customer_email = $order->get_billing_email(); |
| 144 |
|
| 145 |
if (count($customer)) { |
| 146 |
foreach ($customer as $value) { |
| 147 |
if ( ! isset($customer['last_order_date'])) { |
| 148 |
$customer['last_order_date'] = $value['date_last_order']; |
| 149 |
} |
| 150 |
|
| 151 |
if ($customer['last_order_date'] < $value['date_last_order']) { |
| 152 |
$customer['last_order_date'] = $value['date_last_order']; |
| 153 |
} |
| 154 |
|
| 155 |
$order_count += $value['orders_count'] ?? 0; |
| 156 |
$total_spent += $value['total_spend'] ?? 0; |
| 157 |
} |
| 158 |
|
| 159 |
$last_order_date = $customer['last_order_date']; |
| 160 |
$dateCreated = $order->get_date_created(); |
| 161 |
// sometimes when an order is placed, the last order is not included with the orders count |
| 162 |
if (isset($dateCreated) && ($last_order_date < $order->get_date_created()->date_i18n('Y-m-d H:i:s'))) { |
| 163 |
$last_order_date = $order->get_date_created()->date_i18n('Y-m-d H:i:s'); |
| 164 |
|
| 165 |
$order_count++; |
| 166 |
$total_spent += $order->get_total(); |
| 167 |
} |
| 168 |
} else { |
| 169 |
$last_order_date = $order->get_date_created()->date_i18n('Y-m-d H:i:s'); |
| 170 |
|
| 171 |
$order_count++; |
| 172 |
$total_spent += $order->get_total(); |
| 173 |
} |
| 174 |
|
| 175 |
return [ |
| 176 |
'email' => $customer_email, |
| 177 |
'customer_id' => $customer[0]['id'] ?? 0, |
| 178 |
'orders_count' => $order_count, |
| 179 |
'total_spent' => $total_spent, |
| 180 |
'last_order' => $last_order_date, |
| 181 |
'last_order_id' => $order_id |
| 182 |
]; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Process order create and subscription to newsletter |
| 187 |
* woo_ml_process_order_subscription |
| 188 |
* |
| 189 |
* @param $order_id |
| 190 |
* |
| 191 |
* @return void |
| 192 |
*/ |
| 193 |
public function processOrderSubscription($order_id) |
| 194 |
{ |
| 195 |
$order = wc_get_order($order_id); |
| 196 |
$customer_data = $this->getCustomerDataFromOrder($order_id); |
| 197 |
|
| 198 |
$subscribe = $order->get_meta('_woo_ml_subscribe'); |
| 199 |
if (isset($_POST['woo_ml_subscribe']) && '1' == $_POST['woo_ml_subscribe'] && $subscribe === "") { |
| 200 |
$subscribe = true; |
| 201 |
} |
| 202 |
if ($subscribe === "") { |
| 203 |
$subscribe = MailerLiteSettings::getInstance()->getMlOption('checkout_hide') === 'yes'; |
| 204 |
|
| 205 |
if ($subscribe) { |
| 206 |
$order->add_meta_data('_woo_ml_subscribe', true, true); |
| 207 |
|
| 208 |
// save instead of save_meta_data for pending payment orders |
| 209 |
$order->save(); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
$data = []; |
| 215 |
$data['email'] = $customer_data['email']; |
| 216 |
$data['checked_sub_to_mailist'] = $subscribe; |
| 217 |
$checkout_id = isset($_COOKIE['mailerlite_checkout_token']) ? $_COOKIE['mailerlite_checkout_token'] : CheckoutProcess::getInstance()->getSavedCheckoutIdByEmail($customer_data['email']); |
| 218 |
|
| 219 |
$data['checkout_id'] = $checkout_id; |
| 220 |
$data['order_id'] = $order_id; |
| 221 |
$data['payment_method'] = $order->get_payment_method(); |
| 222 |
|
| 223 |
if ($data['payment_method'] == 'bacs' || $data['payment_method'] == 'cheque') { |
| 224 |
@setcookie('mailerlite_checkout_email', null, -1, '/'); |
| 225 |
@setcookie('mailerlite_checkout_token', null, -1, '/'); |
| 226 |
@setcookie('mailerlite_accepts_marketing', null, -1, '/'); |
| 227 |
} else { |
| 228 |
$data['checkout_data'] = CheckoutProcess::getInstance()->getCheckoutData(); |
| 229 |
} |
| 230 |
$subscriber_fields = MailerLiteSettings::getInstance()->getSubscriberFieldsFromCustomerData($customer_data); |
| 231 |
if (sizeof($subscriber_fields) > 0) { |
| 232 |
$data['fields'] = $subscriber_fields; |
| 233 |
} |
| 234 |
|
| 235 |
$mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); |
| 236 |
|
| 237 |
if ($mailerliteClient->getApiType() === ApiType::CLASSIC) { |
| 238 |
|
| 239 |
$subscriber_result = $this->addSubscriberSaveOrder($data, 'order_created'); |
| 240 |
|
| 241 |
if (isset($subscriber_result->added_to_group)) { |
| 242 |
if ($subscriber_result->added_to_group) { |
| 243 |
$this->markOrderSubscribedViaApi($order_id); |
| 244 |
} else { |
| 245 |
$this->completeOrderCustomerAlreadySubscribed($order_id); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
if (isset($subscriber_result->updated_fields) && $subscriber_result->updated_fields) { |
| 250 |
$this->markOrderSubscriberUpdated($order_id); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
if ($mailerliteClient->getApiType() === ApiType::CURRENT) { |
| 255 |
|
| 256 |
$shop = get_option('woo_ml_shop_id', false); |
| 257 |
|
| 258 |
if ($shop === false) { |
| 259 |
|
| 260 |
return; |
| 261 |
} |
| 262 |
|
| 263 |
$subscribe = $this->orderCustomerSubscribe($order_id); |
| 264 |
|
| 265 |
$cart_details = CartProcess::getInstance()->getCartDetails($order_id); |
| 266 |
|
| 267 |
//rename zip key for API |
| 268 |
$zip = $subscriber_fields['zip'] ?? ''; |
| 269 |
unset($subscriber_fields['zip']); |
| 270 |
$subscriber_fields['z_i_p'] = $zip; |
| 271 |
|
| 272 |
$order_customer = [ |
| 273 |
'email' => $data['email'], |
| 274 |
'create_subscriber' => $subscribe, |
| 275 |
'accepts_marketing' => $subscribe, |
| 276 |
'subscriber_fields' => $subscriber_fields |
| 277 |
]; |
| 278 |
|
| 279 |
$customer_fields = $this->getOrderTrackingData($order_id); |
| 280 |
|
| 281 |
if ($customer_fields['customer_id'] !== 0) { |
| 282 |
$order_customer['resource_id'] = (string)$customer_fields['customer_id']; |
| 283 |
} |
| 284 |
|
| 285 |
$order_customer['orders_count'] = $customer_fields['orders_count']; |
| 286 |
$order_customer['total_spent'] = $customer_fields['total_spent']; |
| 287 |
|
| 288 |
$order_cart = [ |
| 289 |
'items' => $cart_details['items'] |
| 290 |
]; |
| 291 |
|
| 292 |
// Delete existing cart (abandoned checkout) |
| 293 |
$checkout_id = $_COOKIE['mailerlite_checkout_token'] ?? CheckoutProcess::getInstance()->getSavedCheckoutIdByEmail($data['email']); |
| 294 |
|
| 295 |
if ($checkout_id !== null) { |
| 296 |
|
| 297 |
$mailerliteClient->deleteOrder($shop, $checkout_id); |
| 298 |
|
| 299 |
@setcookie('mailerlite_checkout_email', null, -1, '/'); |
| 300 |
@setcookie('mailerlite_checkout_token', null, -1, '/'); |
| 301 |
@setcookie('mailerlite_accepts_marketing', null, -1, '/'); |
| 302 |
|
| 303 |
CheckoutProcess::getInstance()->removeCheckout($data['email']); |
| 304 |
} |
| 305 |
|
| 306 |
// set order status completed (when processing and paid) |
| 307 |
if ($cart_details['status'] == 'processing') { |
| 308 |
$cart_details['status'] = 'completed'; |
| 309 |
} |
| 310 |
|
| 311 |
// Create order |
| 312 |
$order_create = $mailerliteClient->syncOrder($shop, $order_id, $order_customer, $order_cart, |
| 313 |
$cart_details['status'], $cart_details['total_price'], $cart_details['created_at']); |
| 314 |
|
| 315 |
if ($order_create) { |
| 316 |
|
| 317 |
$this->completeOrderDataSubmitted($order_id); |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Get customer data from order |
| 324 |
* woo_ml_get_customer_data_from_order |
| 325 |
* |
| 326 |
* @param $order_id |
| 327 |
* |
| 328 |
* @return array|bool |
| 329 |
*/ |
| 330 |
public function getCustomerDataFromOrder($order_id) |
| 331 |
{ |
| 332 |
|
| 333 |
if (empty($order_id)) { |
| 334 |
return false; |
| 335 |
} |
| 336 |
|
| 337 |
$order = wc_get_order($order_id); |
| 338 |
|
| 339 |
if (method_exists($order, 'get_billing_email')) { |
| 340 |
$data = array( |
| 341 |
'email' => $order->get_billing_email(), |
| 342 |
'name' => "{$order->get_billing_first_name()} {$order->get_billing_last_name()}", |
| 343 |
'first_name' => $order->get_billing_first_name(), |
| 344 |
'last_name' => $order->get_billing_last_name(), |
| 345 |
'company' => $order->get_billing_company(), |
| 346 |
'city' => $order->get_billing_city(), |
| 347 |
'postcode' => $order->get_billing_postcode(), |
| 348 |
'state' => $order->get_billing_state(), |
| 349 |
'country' => $order->get_billing_country(), |
| 350 |
'phone' => $order->get_billing_phone() |
| 351 |
); |
| 352 |
} else { |
| 353 |
// NOTE: Only for compatibility with WooCommerce < 3.0 |
| 354 |
$data = array( |
| 355 |
'email' => $order->billing_email, |
| 356 |
'name' => "{$order->billing_first_name} {$order->billing_last_name}", |
| 357 |
'first_name' => $order->billing_first_name, |
| 358 |
'last_name' => $order->billing_last_name, |
| 359 |
'company' => $order->billing_company, |
| 360 |
'city' => $order->billing_city, |
| 361 |
'postcode' => $order->billing_postcode, |
| 362 |
'state' => $order->billing_state, |
| 363 |
'country' => $order->billing_country, |
| 364 |
'phone' => $order->billing_phone |
| 365 |
); |
| 366 |
} |
| 367 |
|
| 368 |
return $data; |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* Check whether a customer wants to be subscribed to our mailing list or not |
| 373 |
* woo_ml_order_customer_subscribe |
| 374 |
* |
| 375 |
* @param $order_id |
| 376 |
* |
| 377 |
* @return bool |
| 378 |
*/ |
| 379 |
public function orderCustomerSubscribe($order_id) |
| 380 |
{ |
| 381 |
|
| 382 |
$order = wc_get_order($order_id); |
| 383 |
|
| 384 |
$subscribe = $order->get_meta('_woo_ml_subscribe'); |
| 385 |
|
| 386 |
return ('1' == $subscribe) ? true : false; |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* Mark order as "wants to be subscribed to mailing our list" |
| 391 |
* woo_ml_set_order_customer_subscribe |
| 392 |
* |
| 393 |
* @param $order_id |
| 394 |
*/ |
| 395 |
public function setOrderCustomerSubscribe($order_id) |
| 396 |
{ |
| 397 |
$order = wc_get_order($order_id); |
| 398 |
|
| 399 |
$order->add_meta_data('_woo_ml_subscribe', true); |
| 400 |
$order->save_meta_data(); |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Check whether order data was submitted via API or not |
| 405 |
* woo_ml_order_data_submitted |
| 406 |
* |
| 407 |
* @param $order_id |
| 408 |
* |
| 409 |
* @return bool |
| 410 |
*/ |
| 411 |
public function orderDataSubmitted($order_id) |
| 412 |
{ |
| 413 |
|
| 414 |
$order = wc_get_order($order_id); |
| 415 |
|
| 416 |
$data_submitted = $order->get_meta('_woo_ml_order_data_submitted'); |
| 417 |
|
| 418 |
return ('1' == $data_submitted) ? true : false; |
| 419 |
} |
| 420 |
|
| 421 |
/** |
| 422 |
* Mark order as "order data submitted" |
| 423 |
* woo_ml_complete_order_data_submitted |
| 424 |
* |
| 425 |
* @param $order_id |
| 426 |
*/ |
| 427 |
public function completeOrderDataSubmitted($order_id) |
| 428 |
{ |
| 429 |
$order = wc_get_order($order_id); |
| 430 |
|
| 431 |
$order->add_meta_data('_woo_ml_order_data_submitted', true); |
| 432 |
$order->save_meta_data(); |
| 433 |
} |
| 434 |
|
| 435 |
/** |
| 436 |
* Gets triggered on completed order event. Fetches order data |
| 437 |
* and passes it along to api |
| 438 |
* woo_ml_send_completed_order |
| 439 |
* |
| 440 |
* @param Integer $order_id |
| 441 |
* |
| 442 |
* @return void |
| 443 |
*/ |
| 444 |
public function sendCompletedOrder($order_id) |
| 445 |
{ |
| 446 |
$order = wc_get_order($order_id); |
| 447 |
$order_data['order'] = $order->get_data(); |
| 448 |
$order_items = $order->get_items(); |
| 449 |
$customer_email = $order->get_billing_email(); |
| 450 |
|
| 451 |
$saved_checkout = CheckoutProcess::getInstance()->getSavedCheckoutByEmail($customer_email); |
| 452 |
$order_data['checkout_id'] = !empty($saved_checkout) ? $saved_checkout->checkout_id : null; |
| 453 |
|
| 454 |
$ignored_products = MailerLiteSettings::getInstance()->remapList(ProductProcess::getInstance()->getIgnoredProductList()); |
| 455 |
|
| 456 |
foreach ($order_items as $key => $value) { |
| 457 |
$item_data = $value->get_data(); |
| 458 |
$order_data['order']['line_items'][$key] = $item_data; |
| 459 |
$order_data['order']['line_items'][$key]['ignored_product'] = in_array($item_data['product_id'], |
| 460 |
$ignored_products) ? 1 : 0; |
| 461 |
} |
| 462 |
@setcookie('mailerlite_checkout_email', null, -1, '/'); |
| 463 |
@setcookie('mailerlite_checkout_token', null, -1, '/'); |
| 464 |
@setcookie('mailerlite_accepts_marketing', null, -1, '/'); |
| 465 |
|
| 466 |
if ($order_data['order']['status'] == 'processing') { |
| 467 |
$order_data['order']['status'] = 'completed'; |
| 468 |
} |
| 469 |
|
| 470 |
$this->wpSendOrder($order_data); |
| 471 |
|
| 472 |
if (!empty($saved_checkout)) { |
| 473 |
|
| 474 |
CheckoutProcess::getInstance()->removeCheckout($customer_email); |
| 475 |
} |
| 476 |
|
| 477 |
} |
| 478 |
|
| 479 |
/** |
| 480 |
* Check whether order was already tracked or not |
| 481 |
* woo_ml_order_tracking_completed |
| 482 |
* |
| 483 |
* @param $order_id |
| 484 |
* |
| 485 |
* @return bool |
| 486 |
*/ |
| 487 |
public function checkOrderTracking($order_id) |
| 488 |
{ |
| 489 |
$order = wc_get_order($order_id); |
| 490 |
|
| 491 |
$order_tracked = $order->get_meta('_woo_ml_order_tracked'); |
| 492 |
|
| 493 |
return ('1' == $order_tracked) ? true : false; |
| 494 |
} |
| 495 |
|
| 496 |
/** |
| 497 |
* Mark order as being tracked |
| 498 |
* woo_ml_complete_order_tracking |
| 499 |
* |
| 500 |
* @param $order_id |
| 501 |
*/ |
| 502 |
public function completeOrderTracking($order_id) |
| 503 |
{ |
| 504 |
$order = wc_get_order($order_id); |
| 505 |
|
| 506 |
$order->add_meta_data('_woo_ml_order_tracked', true); |
| 507 |
$order->save_meta_data(); |
| 508 |
} |
| 509 |
|
| 510 |
/** |
| 511 |
* On change of order status to processing send order data |
| 512 |
* woo_ml_payment_status_processing |
| 513 |
* |
| 514 |
* @param Integer $order_id |
| 515 |
* |
| 516 |
* @return void |
| 517 |
*/ |
| 518 |
public function paymentStatusProcessing($order_id) |
| 519 |
{ |
| 520 |
$order = wc_get_order($order_id); |
| 521 |
|
| 522 |
if ($order->get_status() === 'processing') { |
| 523 |
$data = []; |
| 524 |
$customer_email = $order->get_billing_email(); |
| 525 |
|
| 526 |
// load the checkout id from the cookie first |
| 527 |
// if that fails, then check the mailerlite checkouts table |
| 528 |
$checkoutId = null; |
| 529 |
if (isset($_COOKIE['mailerlite_checkout_token'])) { |
| 530 |
|
| 531 |
$checkoutId = $_COOKIE['mailerlite_checkout_token']; |
| 532 |
} else { |
| 533 |
|
| 534 |
$saved_checkout = CheckoutProcess::getInstance()->getSavedCheckoutByEmail($customer_email); |
| 535 |
$checkoutId = !empty($saved_checkout) ? $saved_checkout->checkout_id : null; |
| 536 |
} |
| 537 |
|
| 538 |
$data['checkout_id'] = $checkoutId; |
| 539 |
$data['order_id'] = $order_id; |
| 540 |
$data['payment_method'] = $order->get_payment_method(); |
| 541 |
|
| 542 |
@setcookie('mailerlite_checkout_email', null, -1, '/'); |
| 543 |
@setcookie('mailerlite_checkout_token', null, -1, '/'); |
| 544 |
@setcookie('mailerlite_accepts_marketing', null, -1, '/'); |
| 545 |
|
| 546 |
$this->addSubscriberSaveOrder($data, 'order_processing'); |
| 547 |
CheckoutProcess::getInstance()->removeCheckout($customer_email); |
| 548 |
|
| 549 |
if ((int)get_option('woo_mailerlite_platform', 1) === ApiType::CURRENT) { |
| 550 |
|
| 551 |
$this->processOrderSubscription($order_id); |
| 552 |
} |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
/** |
| 557 |
* Mark order as "subscriber was updated" |
| 558 |
* woo_ml_complete_order_subscriber_updated |
| 559 |
* |
| 560 |
* @param $order_id |
| 561 |
*/ |
| 562 |
public function markOrderSubscriberUpdated($order_id) |
| 563 |
{ |
| 564 |
$order = wc_get_order($order_id); |
| 565 |
|
| 566 |
$order->add_meta_data('_woo_ml_subscriber_updated', true); |
| 567 |
$order->save_meta_data(); |
| 568 |
} |
| 569 |
|
| 570 |
/** |
| 571 |
* Check whether a subscriber was updated from order or not |
| 572 |
* woo_ml_order_subscriber_updated |
| 573 |
* |
| 574 |
* @param $order_id |
| 575 |
* |
| 576 |
* @return bool |
| 577 |
*/ |
| 578 |
public function checkOrderSubscriberUpdated($order_id) |
| 579 |
{ |
| 580 |
$order = wc_get_order($order_id); |
| 581 |
|
| 582 |
$subscriber_updated_from_order = $order->get_meta('_woo_ml_subscriber_updated'); |
| 583 |
|
| 584 |
return ('1' == $subscriber_updated_from_order) ? true : false; |
| 585 |
} |
| 586 |
|
| 587 |
/** |
| 588 |
* |
| 589 |
* woo_ml_complete_order_customer_already_subscribed |
| 590 |
* |
| 591 |
* @param $order_id |
| 592 |
* |
| 593 |
* @return void |
| 594 |
*/ |
| 595 |
public function completeOrderCustomerAlreadySubscribed($order_id) |
| 596 |
{ |
| 597 |
$order = wc_get_order($order_id); |
| 598 |
|
| 599 |
$order->add_meta_data('_woo_ml_already_subscribed', true); |
| 600 |
$order->save_meta_data(); |
| 601 |
} |
| 602 |
|
| 603 |
/** |
| 604 |
* Mark order as "customer subscribed via API" |
| 605 |
* woo_ml_complete_order_customer_subscribed |
| 606 |
* |
| 607 |
* @param $order_id |
| 608 |
*/ |
| 609 |
public function markOrderSubscribedViaApi($order_id) |
| 610 |
{ |
| 611 |
$order = wc_get_order($order_id); |
| 612 |
|
| 613 |
$order->add_meta_data('_woo_ml_subscribed', true); |
| 614 |
$order->save_meta_data(); |
| 615 |
} |
| 616 |
|
| 617 |
/** |
| 618 |
* |
| 619 |
* woo_ml_order_customer_already_subscribed |
| 620 |
* |
| 621 |
* @param $order_id |
| 622 |
* |
| 623 |
* @return bool |
| 624 |
*/ |
| 625 |
public function orderCustomerAlreadySubscribed($order_id) |
| 626 |
{ |
| 627 |
|
| 628 |
$order = wc_get_order($order_id); |
| 629 |
|
| 630 |
$already_subscribed = $order->get_meta('_woo_ml_already_subscribed'); |
| 631 |
|
| 632 |
return ('1' == $already_subscribed) ? true : false; |
| 633 |
} |
| 634 |
|
| 635 |
/** |
| 636 |
* Check whether a customer was subscribed via API or not |
| 637 |
*subscribe |
| 638 |
* woo_ml_order_customer_subscribed |
| 639 |
* |
| 640 |
* @param $order_id |
| 641 |
* |
| 642 |
* @return bool |
| 643 |
*/ |
| 644 |
public function checkCustomerSubscribed($order_id) |
| 645 |
{ |
| 646 |
|
| 647 |
$order = wc_get_order($order_id); |
| 648 |
|
| 649 |
$subscribed = $order->get_meta('_woo_ml_subscribed'); |
| 650 |
|
| 651 |
return ('1' == $subscribed) ? true : false; |
| 652 |
} |
| 653 |
|
| 654 |
/** |
| 655 |
* Sending order data on creation of order and/or order status change to processing |
| 656 |
* mailerlite_wp_add_subscriber_and_save_order |
| 657 |
* |
| 658 |
* @param array $data |
| 659 |
* @param string $event |
| 660 |
* |
| 661 |
* @return bool |
| 662 |
*/ |
| 663 |
public function addSubscriberSaveOrder($data, $event) |
| 664 |
{ |
| 665 |
if (!ApiSettings::getInstance()->wpApiKeyExists()) { |
| 666 |
return false; |
| 667 |
} |
| 668 |
|
| 669 |
try { |
| 670 |
$mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); |
| 671 |
|
| 672 |
$shop_id = ''; |
| 673 |
|
| 674 |
$shop_url = site_url(); |
| 675 |
$data['shop_url'] = home_url(); |
| 676 |
$data['order_url'] = $shop_url . "/wp-admin/post.php?post=" . $data['order_id'] . "&action=edit"; |
| 677 |
//order_created case also takes care of processing sub if they have ticked the box to |
| 678 |
//receive newsletters |
| 679 |
|
| 680 |
if ($mailerliteClient->getApiType() === ApiType::CLASSIC) { |
| 681 |
if ($event === 'order_created') { |
| 682 |
$result = $mailerliteClient->sendSubscriberData($shop_id, $data); |
| 683 |
|
| 684 |
if (isset($result->added_to_group) && isset($result->updated_fields)) { |
| 685 |
return $result; |
| 686 |
} elseif (isset($result->deactivate) && $result->deactivate) { |
| 687 |
woo_ml_deactivate_woo_ml_plugin(true); |
| 688 |
|
| 689 |
return false; |
| 690 |
} else { |
| 691 |
return false; |
| 692 |
} |
| 693 |
} else { |
| 694 |
|
| 695 |
$result = $mailerliteClient->sendOrderProcessing($shop_id, $data); |
| 696 |
if (isset($result->deactivate) && $result->deactivate) { |
| 697 |
woo_ml_deactivate_woo_ml_plugin(true); |
| 698 |
} |
| 699 |
|
| 700 |
if ($event === 'order_processing') { |
| 701 |
$this->sendCompletedOrder($data['order_id']); |
| 702 |
$this->processOrderTracking($data['order_id']); |
| 703 |
} |
| 704 |
|
| 705 |
return true; |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
} catch (\Exception $e) { |
| 710 |
return false; |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
/** |
| 715 |
* Sends completed order data to api to be evaluated and saved and/if trigger automations |
| 716 |
* mailerlite_wp_send_order |
| 717 |
* |
| 718 |
* @param array $order_data |
| 719 |
* |
| 720 |
* @return bool|void |
| 721 |
*/ |
| 722 |
public function wpSendOrder($order_data) |
| 723 |
{ |
| 724 |
if (!ApiSettings::getInstance()->wpApiKeyExists()) { |
| 725 |
return false; |
| 726 |
} |
| 727 |
|
| 728 |
try { |
| 729 |
$mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); |
| 730 |
|
| 731 |
if ($mailerliteClient->getApiType() == ApiType::CLASSIC) { |
| 732 |
|
| 733 |
$store = home_url(); |
| 734 |
|
| 735 |
$shop_url = site_url(); |
| 736 |
$order_data['order_url'] = $shop_url . "/wp-admin/post.php?post=" . $order_data['order']['id'] . "&action=edit"; |
| 737 |
|
| 738 |
$result = $mailerliteClient->saveOrder($store, $order_data); |
| 739 |
|
| 740 |
if (isset($result->deactivate) && $result->deactivate) { |
| 741 |
woo_ml_deactivate_woo_ml_plugin(true); |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
if ($mailerliteClient->getApiType() == ApiType::CURRENT) { |
| 746 |
|
| 747 |
$shop = get_option('woo_ml_shop_id', false); |
| 748 |
|
| 749 |
if ($shop === false) { |
| 750 |
|
| 751 |
return false; |
| 752 |
} |
| 753 |
|
| 754 |
$subscribe = $this->orderCustomerSubscribe($order_data['order']['id']); |
| 755 |
|
| 756 |
$cart_details = CartProcess::getInstance()->getCartDetails($order_data['order']['id']); |
| 757 |
|
| 758 |
$customer_data = $this->getCustomerDataFromOrder($order_data['order']['id']); |
| 759 |
$subscriber_fields = MailerLiteSettings::getInstance()->getSubscriberFieldsFromCustomerData($customer_data); |
| 760 |
|
| 761 |
//rename zip key for API |
| 762 |
$zip = $subscriber_fields['zip'] ?? ''; |
| 763 |
unset($subscriber_fields['zip']); |
| 764 |
$subscriber_fields['z_i_p'] = $zip; |
| 765 |
|
| 766 |
$order_customer = [ |
| 767 |
'email' => $order_data['order']['billing']['email'], |
| 768 |
'create_subscriber' => $subscribe, |
| 769 |
'accepts_marketing' => $subscribe, |
| 770 |
'subscriber_fields' => $subscriber_fields |
| 771 |
]; |
| 772 |
|
| 773 |
$customer_fields = $this->getOrderTrackingData($order_data['order']['id']); |
| 774 |
|
| 775 |
if ($customer_fields['customer_id'] !== 0) { |
| 776 |
$order_customer['resource_id'] = (string)$customer_fields['customer_id']; |
| 777 |
} |
| 778 |
|
| 779 |
$order_customer['orders_count'] = $customer_fields['orders_count']; |
| 780 |
$order_customer['total_spent'] = $customer_fields['total_spent']; |
| 781 |
|
| 782 |
$order_cart = [ |
| 783 |
'items' => $cart_details['items'] |
| 784 |
]; |
| 785 |
|
| 786 |
if (is_numeric($order_data['order']['id'])) { |
| 787 |
$order_data['order']['status'] = 'completed'; |
| 788 |
} |
| 789 |
|
| 790 |
$mailerliteClient->syncOrder($shop, $order_data['order']['id'], $order_customer, $order_cart, |
| 791 |
$order_data['order']['status'], $cart_details['total_price'], $cart_details['created_at']); |
| 792 |
|
| 793 |
if ($mailerliteClient->responseCode() === 201 || $mailerliteClient->responseCode() === 200) { |
| 794 |
|
| 795 |
$this->orderDataSubmitted($order_data['order']['id']); |
| 796 |
|
| 797 |
// Delete existing cart (abandoned checkout) |
| 798 |
if ($order_data['checkout_id'] !== null) { |
| 799 |
|
| 800 |
$mailerliteClient->deleteOrder($shop, $order_data['checkout_id']); |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
return true; |
| 805 |
} |
| 806 |
} catch (\Exception $e) { |
| 807 |
return false; |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
/** |
| 812 |
* Delete the order on cancellation |
| 813 |
* |
| 814 |
* @param $order_id |
| 815 |
* |
| 816 |
* @return bool |
| 817 |
*/ |
| 818 |
public function cancelOrder($order_id) |
| 819 |
{ |
| 820 |
$mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); |
| 821 |
|
| 822 |
$order = wc_get_order($order_id); |
| 823 |
|
| 824 |
$shop = get_option('woo_ml_shop_id', false); |
| 825 |
|
| 826 |
if ($shop === false) { |
| 827 |
return false; |
| 828 |
} |
| 829 |
|
| 830 |
if (!$order) { |
| 831 |
return false; |
| 832 |
} |
| 833 |
|
| 834 |
// get subscriber orders count and total spent fields |
| 835 |
$customer_data = $this->getOrderTrackingData($order_id); |
| 836 |
|
| 837 |
if ($mailerliteClient->getApiType() == ApiType::CURRENT) { |
| 838 |
$mailerliteClient->deleteOrder($shop, $order_id); |
| 839 |
|
| 840 |
// recalculate as the values contain the current order |
| 841 |
$customer_data['orders_count']--; |
| 842 |
$customer_data['total_spent'] -= $order->get_total(); |
| 843 |
|
| 844 |
$mailerliteClient->syncCustomer($shop, $customer_data['customer_id'], $customer_data['email'], |
| 845 |
$customer_data); |
| 846 |
} |
| 847 |
|
| 848 |
if ($mailerliteClient->getApiType() == ApiType::CLASSIC) { |
| 849 |
$subscriber_fields = [ |
| 850 |
'woo_orders_count' => $customer_data['orders_count'] - 1, |
| 851 |
'woo_total_spent' => $customer_data['total_spent'] - $order->get_total(), |
| 852 |
'woo_last_order' => $customer_data['last_order'], |
| 853 |
'woo_last_order_id' => $customer_data['last_order_id'] |
| 854 |
]; |
| 855 |
|
| 856 |
$store = home_url(); |
| 857 |
|
| 858 |
$mailerliteClient->syncCustomer($store, $customer_data['customer_id'], |
| 859 |
$customer_data['email'], $subscriber_fields); |
| 860 |
} |
| 861 |
|
| 862 |
return true; |
| 863 |
} |
| 864 |
} |