| 1 |
<?php |
| 2 |
|
| 3 |
class WooMailerLiteCheckoutDataService |
| 4 |
{ |
| 5 |
public static function getCheckoutData($email = null) |
| 6 |
{ |
| 7 |
if (empty(WC()->cart)) { |
| 8 |
WC()->frontend_includes(); |
| 9 |
|
| 10 |
if ( ! did_action('woocommerce_load_cart_from_session') && function_exists('wc_load_cart')) { |
| 11 |
wc_load_cart(); |
| 12 |
} |
| 13 |
} |
| 14 |
$cart = WC()->cart; |
| 15 |
$cartItems = $cart->get_cart(); |
| 16 |
$customer = $cart->get_customer(); |
| 17 |
if ($email) { |
| 18 |
$customerEmail = $email; |
| 19 |
} else { |
| 20 |
$customerEmail = $customer->get_email(); |
| 21 |
} |
| 22 |
$cartFromDb = WooMailerLiteCart::where('hash', WooMailerLiteSession::getMLCartHash()) |
| 23 |
->withoutPrefix(function($query) use ($customerEmail) { |
| 24 |
$query->orWhere('email', $customerEmail); |
| 25 |
})->first(); |
| 26 |
if (!$cartFromDb) { |
| 27 |
return true; |
| 28 |
} |
| 29 |
$cartData = $cartFromDb->data; |
| 30 |
|
| 31 |
if (!$customerEmail) { |
| 32 |
$customerEmail = $cartFromDb->email; |
| 33 |
} |
| 34 |
|
| 35 |
// check if email was updated recently in checkout |
| 36 |
if (filter_var($cartFromDb->email, FILTER_VALIDATE_EMAIL) && $customerEmail !== $cartFromDb->email) { |
| 37 |
$customerEmail = $cartFromDb->email; |
| 38 |
} |
| 39 |
$checkoutData = []; |
| 40 |
if (!empty($customerEmail)) { |
| 41 |
$lineItems = []; |
| 42 |
$total = 0; |
| 43 |
|
| 44 |
foreach ($cartItems as $key => $item) { |
| 45 |
$subtotal = intval($item['quantity']) * floatval($item['data']->get_price('edit')); |
| 46 |
|
| 47 |
$lineItems[] = [ |
| 48 |
'key' => $key, |
| 49 |
'line_subtotal' => $subtotal, |
| 50 |
'line_total' => $subtotal, |
| 51 |
'product_id' => $item['product_id'], |
| 52 |
'quantity' => $item['quantity'], |
| 53 |
'variation' => $item['variation'], |
| 54 |
'variation_id' => $item['variation_id'], |
| 55 |
]; |
| 56 |
|
| 57 |
$total += $subtotal; |
| 58 |
} |
| 59 |
|
| 60 |
$shopCheckoutUrl = wc_get_checkout_url(); |
| 61 |
$checkoutUrl = $shopCheckoutUrl . '?ml_checkout=' . $cartData['checkout_id']; |
| 62 |
|
| 63 |
$checkoutData = [ |
| 64 |
'id' => $cartData['checkout_id'], |
| 65 |
'email' => $customerEmail, |
| 66 |
'line_items' => $lineItems, |
| 67 |
'abandoned_checkout_url' => $checkoutUrl, |
| 68 |
'total_price' => $total, |
| 69 |
'created_at' => date('Y-m-d H:i:s'), |
| 70 |
'subscribe' => false |
| 71 |
]; |
| 72 |
|
| 73 |
if (isset($cartFromDb->subscribe)) { |
| 74 |
$checkoutData['subscribe'] = $cartFromDb->subscribe; |
| 75 |
} |
| 76 |
|
| 77 |
if (WooMailerLiteOptions::get("settings.checkoutHidden")) { |
| 78 |
$checkoutData['subscribe'] = true; |
| 79 |
} |
| 80 |
|
| 81 |
if (isset($_POST['language'])) { |
| 82 |
WC()->session->set('_woo_ml_language', textInput('language')); |
| 83 |
$checkoutData['language'] = textInput('language'); |
| 84 |
} |
| 85 |
|
| 86 |
if (!empty($subscriberFields)) { |
| 87 |
$checkoutData['subscriber_fields'] = $subscriberFields; |
| 88 |
} |
| 89 |
} |
| 90 |
return $checkoutData; |
| 91 |
} |
| 92 |
} |
| 93 |
|