| 1 |
<?php |
| 2 |
|
| 3 |
namespace MailerLite\Includes\Classes\Process; |
| 4 |
|
| 5 |
use MailerLite\Includes\Classes\Settings\ApiSettings; |
| 6 |
use MailerLite\Includes\Classes\Singleton; |
| 7 |
use MailerLite\Includes\Shared\Api\ApiType; |
| 8 |
use MailerLite\Includes\Shared\Api\PlatformAPI; |
| 9 |
|
| 10 |
class CartProcess extends Singleton |
| 11 |
{ |
| 12 |
/** |
| 13 |
* Get cart details from order for the API |
| 14 |
* woo_ml_get_cart_details |
| 15 |
* |
| 16 |
* @param $order_id |
| 17 |
* |
| 18 |
* @return array |
| 19 |
*/ |
| 20 |
public function getCartDetails($order_id) |
| 21 |
{ |
| 22 |
|
| 23 |
$order = wc_get_order($order_id); |
| 24 |
|
| 25 |
$items = []; |
| 26 |
|
| 27 |
foreach ($order->get_items() as $item_key => $item) { |
| 28 |
|
| 29 |
if ($item->get_product_id() !== 0) { |
| 30 |
|
| 31 |
$items[] = [ |
| 32 |
'product_resource_id' => (string)$item->get_product_id(), |
| 33 |
'variant' => $item->get_name(), |
| 34 |
'quantity' => $item->get_quantity(), |
| 35 |
'price' => (float)$item->get_total() |
| 36 |
]; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
return [ |
| 41 |
'customer_id' => $order->get_customer_id(), |
| 42 |
'items' => $items, |
| 43 |
'status' => $order->get_status(), |
| 44 |
'total_price' => $order->get_total(), |
| 45 |
'created_at' => date('Y-m-d h:m:s', strtotime($order->get_date_created())) |
| 46 |
]; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Sending cart data on updated cart contents event (add or remove from cart) |
| 51 |
* woo_ml_send_cart |
| 52 |
* |
| 53 |
* @param $cookie_email |
| 54 |
* |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
public function sendCart($cookie_email = null, $subscribe = null, $language = null, $subscriber_fields = null) |
| 58 |
{ |
| 59 |
$checkout_data = CheckoutProcess::getInstance()->getCheckoutData($cookie_email, $subscribe, $language, |
| 60 |
$subscriber_fields); |
| 61 |
if ( ! empty($checkout_data)) { |
| 62 |
|
| 63 |
$this->wpSendCart($checkout_data); |
| 64 |
} |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Sending cart data on cart update |
| 70 |
* mailerlite_wp_send_cart |
| 71 |
* |
| 72 |
* @param bool $cart_data |
| 73 |
* |
| 74 |
* @return bool|void |
| 75 |
*/ |
| 76 |
public function wpSendCart($cart_data) |
| 77 |
{ |
| 78 |
|
| 79 |
if ( ! ApiSettings::getInstance()->wpApiKeyExists()) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
try { |
| 84 |
|
| 85 |
$mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); |
| 86 |
|
| 87 |
if ($mailerliteClient->getApiType() == ApiType::CLASSIC) { |
| 88 |
|
| 89 |
$shop = home_url(); |
| 90 |
|
| 91 |
$result = $mailerliteClient->sendCart($shop, $cart_data); |
| 92 |
|
| 93 |
if (isset($result->deactivate) && $result->deactivate) { |
| 94 |
woo_ml_deactivate_woo_ml_plugin(true); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
if ($mailerliteClient->getApiType() == ApiType::CURRENT) { |
| 99 |
|
| 100 |
$shop = get_option('woo_ml_shop_id', false); |
| 101 |
|
| 102 |
if ($shop === false) { |
| 103 |
|
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
$order_customer = [ |
| 108 |
'email' => $cart_data['email'], |
| 109 |
'accepts_marketing' => $cart_data['subscribe'] ?? false, |
| 110 |
'create_subscriber' => $cart_data['subscribe'] ?? false, |
| 111 |
]; |
| 112 |
|
| 113 |
if (isset($cart_data['language'])) { |
| 114 |
$order_customer['subscriber_fields'] = [ |
| 115 |
'subscriber_language' => $cart_data['language'], |
| 116 |
]; |
| 117 |
} |
| 118 |
|
| 119 |
if (isset($cart_data['subscriber_fields'])) { |
| 120 |
$order_customer['subscriber_fields'] = array_merge($order_customer['subscriber_fields'] ?? [], |
| 121 |
$cart_data['subscriber_fields']); |
| 122 |
} |
| 123 |
|
| 124 |
$order_cart = [ |
| 125 |
'resource_id' => (string)$cart_data['id'], |
| 126 |
'checkout_url' => $cart_data['abandoned_checkout_url'], |
| 127 |
'items' => [] |
| 128 |
]; |
| 129 |
|
| 130 |
foreach ($cart_data['line_items'] as $item) { |
| 131 |
|
| 132 |
$product = wc_get_product($item['product_id']); |
| 133 |
|
| 134 |
$order_cart['items'][] = [ |
| 135 |
'product_resource_id' => (string)$item['product_id'], |
| 136 |
'variant' => $product->get_name(), |
| 137 |
'quantity' => (int)$item['quantity'], |
| 138 |
'price' => floatval($product->get_price('edit')), |
| 139 |
]; |
| 140 |
} |
| 141 |
|
| 142 |
$mailerliteClient->syncOrder($shop, $cart_data['id'], $order_customer, $order_cart, 'pending', |
| 143 |
$cart_data['total_price'], $cart_data['created_at']); |
| 144 |
} |
| 145 |
} catch (\Exception $e) { |
| 146 |
return false; |
| 147 |
} |
| 148 |
} |
| 149 |
} |