| 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 |
$subscribe = $orderProcess->orderCustomerSubscribe($order_id); |
| 35 |
|
| 36 |
$cart_details = CartProcess::getInstance()->getCartDetails($order_id); |
| 37 |
|
| 38 |
$customer_data = $orderProcess->getCustomerDataFromOrder($order_id); |
| 39 |
$subscriber_fields = $this->getSubscriberFieldsFromCustomerData($customer_data); |
| 40 |
|
| 41 |
// rename zip key for API |
| 42 |
$zip = $subscriber_fields['zip'] ?? ''; |
| 43 |
unset($subscriber_fields['zip']); |
| 44 |
$subscriber_fields['z_i_p'] = $zip; |
| 45 |
|
| 46 |
$order_customer = [ |
| 47 |
'email' => $order->get_billing_email(), |
| 48 |
'create_subscriber' => $subscribe, |
| 49 |
'accepts_marketing' => $subscribe, |
| 50 |
'subscriber_fields' => $subscriber_fields |
| 51 |
]; |
| 52 |
|
| 53 |
if (strval($cart_details['customer_id']) !== "0") { |
| 54 |
|
| 55 |
$order_customer['resource_id'] = (string)$cart_details['customer_id']; |
| 56 |
|
| 57 |
// check customer fields |
| 58 |
$wc_customer = new \WC_Customer($cart_details['customer_id']); |
| 59 |
|
| 60 |
if ( ! empty($wc_customer->get_email())) { |
| 61 |
|
| 62 |
$order_customer['orders_count'] = $wc_customer->get_order_count(); |
| 63 |
$order_customer['total_spent'] = $wc_customer->get_total_spent(); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
$order_cart = [ |
| 68 |
'items' => $cart_details['items'] |
| 69 |
]; |
| 70 |
|
| 71 |
$order_status = $order->get_status(); |
| 72 |
|
| 73 |
if (is_numeric($order_id)) { |
| 74 |
$order_status = 'completed'; |
| 75 |
} |
| 76 |
|
| 77 |
$mailerliteClient->syncOrder( |
| 78 |
$shop, |
| 79 |
$order_id, |
| 80 |
$order_customer, |
| 81 |
$order_cart, |
| 82 |
$order_status, |
| 83 |
$order->get_total(), |
| 84 |
date('Y-m-d h:m:s', strtotime($order->get_date_created())) |
| 85 |
); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Call to handle order update event |
| 92 |
* mailerlite_wp_sync_order |
| 93 |
*/ |
| 94 |
public function syncOrder($order_id, $post) |
| 95 |
{ |
| 96 |
$orderProcess = OrderProcess::getInstance(); |
| 97 |
$order = wc_get_order($order_id); |
| 98 |
|
| 99 |
$mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); |
| 100 |
|
| 101 |
if ($mailerliteClient->getApiType() == ApiType::CURRENT) { |
| 102 |
|
| 103 |
$shop = get_option('woo_ml_shop_id', false); |
| 104 |
|
| 105 |
if ($shop === false) { |
| 106 |
|
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
$subscribe = $orderProcess->orderCustomerSubscribe($order_id); |
| 111 |
|
| 112 |
$cart_details = CartProcess::getInstance()->getCartDetails($order_id); |
| 113 |
|
| 114 |
$customer_data = $orderProcess->getCustomerDataFromOrder($order_id); |
| 115 |
$subscriber_fields = $this->getSubscriberFieldsFromCustomerData($customer_data); |
| 116 |
|
| 117 |
//rename zip key for API |
| 118 |
$zip = $subscriber_fields['zip'] ?? ''; |
| 119 |
unset($subscriber_fields['zip']); |
| 120 |
$subscriber_fields['z_i_p'] = $zip; |
| 121 |
|
| 122 |
$order_customer = [ |
| 123 |
'email' => $order->get_billing_email(), |
| 124 |
'create_subscriber' => $subscribe, |
| 125 |
'accepts_marketing' => $subscribe, |
| 126 |
'subscriber_fields' => $subscriber_fields |
| 127 |
]; |
| 128 |
|
| 129 |
if (strval($cart_details['customer_id']) !== "0") { |
| 130 |
|
| 131 |
$order_customer['resource_id'] = (string)$cart_details['customer_id']; |
| 132 |
|
| 133 |
// check customer fields |
| 134 |
$wc_customer = new \WC_Customer($cart_details['customer_id']); |
| 135 |
|
| 136 |
if ( ! empty($wc_customer->get_email())) { |
| 137 |
|
| 138 |
$order_customer['orders_count'] = $wc_customer->get_order_count(); |
| 139 |
$order_customer['total_spent'] = $wc_customer->get_total_spent(); |
| 140 |
} |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
$order_cart = [ |
| 145 |
'items' => $cart_details['items'] |
| 146 |
]; |
| 147 |
|
| 148 |
$order_status = $order->get_status(); |
| 149 |
|
| 150 |
if (is_numeric($order_id)) { |
| 151 |
$order_status = 'completed'; |
| 152 |
} |
| 153 |
|
| 154 |
$mailerliteClient->syncOrder($shop, $order_id, $order_customer, $order_cart, $order_status, |
| 155 |
$order->get_total(), date('Y-m-d h:m:s', strtotime($order->get_date_created()))); |
| 156 |
|
| 157 |
if ($mailerliteClient->responseCode() === 201) { |
| 158 |
|
| 159 |
$orderProcess->completeOrderDataSubmitted($order_id); |
| 160 |
|
| 161 |
return true; |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
private function getSubscriberFieldsFromCustomerData($customer_data) |
| 167 |
{ |
| 168 |
return MailerLiteSettings::getInstance()->getSubscriberFieldsFromCustomerData($customer_data); |
| 169 |
} |
| 170 |
|
| 171 |
} |