| 1 |
<?php |
| 2 |
|
| 3 |
namespace MailerLite\Includes\Classes\Process; |
| 4 |
|
| 5 |
use MailerLite\Includes\Classes\Singleton; |
| 6 |
|
| 7 |
class CheckoutProcess extends Singleton |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Sets checkout for user session when clicking on Return to checkout email |
| 11 |
* woo_ml_reload_checkout |
| 12 |
* @return void |
| 13 |
*/ |
| 14 |
public function reloadCheckout() |
| 15 |
{ |
| 16 |
if ( ! function_exists('WC')) { |
| 17 |
|
| 18 |
return false; |
| 19 |
} |
| 20 |
|
| 21 |
if ( ! is_object(WC()->session)) { |
| 22 |
|
| 23 |
return false; |
| 24 |
} |
| 25 |
|
| 26 |
if (isset($_GET['ml_checkout'])) { |
| 27 |
//$checkout_id = substr($_GET['ml_checkout'], 0, strpos($_GET['ml_checkout'], "?")); |
| 28 |
$checkout_id = $_GET['ml_checkout']; |
| 29 |
$checkout = $this->getSavedCheckout($checkout_id); |
| 30 |
|
| 31 |
if ($checkout && ! empty($checkout->cart_content)) { |
| 32 |
WC()->session->set('cart', unserialize($checkout->cart_content)); |
| 33 |
@setcookie('mailerlite_checkout_token', $checkout->checkout_id, time() + 172800, '/'); |
| 34 |
@setcookie('mailerlite_checkout_email', $checkout->email, time() + 172800, '/'); |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Remove Checkout |
| 41 |
* woo_ml_remove_checkout |
| 42 |
* |
| 43 |
* @param $email |
| 44 |
* |
| 45 |
* @return void |
| 46 |
*/ |
| 47 |
public function removeCheckout($email) |
| 48 |
{ |
| 49 |
global $wpdb; |
| 50 |
$table = $wpdb->prefix . 'mailerlite_checkouts'; |
| 51 |
|
| 52 |
$wpdb->delete($table, array('email' => $email)); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get saved checkout id by email |
| 57 |
* woo_ml_get_saved_checkout_id_by_email |
| 58 |
* |
| 59 |
* @param $email |
| 60 |
* |
| 61 |
* @return null |
| 62 |
*/ |
| 63 |
public function getSavedCheckoutIdByEmail($email) |
| 64 |
{ |
| 65 |
$checkout = $this->getSavedCheckoutByEmail($email); |
| 66 |
if ( ! empty($checkout)) { |
| 67 |
return $checkout->checkout_id; |
| 68 |
} |
| 69 |
|
| 70 |
return null; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get saved checkout by email |
| 75 |
* woo_ml_get_saved_checkout_by_email |
| 76 |
* |
| 77 |
* @param $email |
| 78 |
* |
| 79 |
* @return array|object|\stdClass|null |
| 80 |
*/ |
| 81 |
public function getSavedCheckoutByEmail($email) |
| 82 |
{ |
| 83 |
global $wpdb; |
| 84 |
$table = $wpdb->prefix . 'mailerlite_checkouts'; |
| 85 |
|
| 86 |
return $wpdb->get_row($wpdb->prepare("SELECT * FROM $table WHERE email = %s ORDER BY time DESC LIMIT 1", |
| 87 |
$email)); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Get saved checkout |
| 92 |
* woo_ml_get_saved_checkout |
| 93 |
* |
| 94 |
* @param string $checkout_id |
| 95 |
* |
| 96 |
* @return array |
| 97 |
*/ |
| 98 |
public function getSavedCheckout($checkout_id) |
| 99 |
{ |
| 100 |
global $wpdb; |
| 101 |
$table = $wpdb->prefix . 'mailerlite_checkouts'; |
| 102 |
|
| 103 |
return $wpdb->get_row($wpdb->prepare("SELECT * FROM $table WHERE checkout_id = %s", $checkout_id)); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Insert/update/delete checkout entry from the table |
| 108 |
* woo_ml_save_or_update_checkout |
| 109 |
* |
| 110 |
* @param string $checkout_id |
| 111 |
* @param string $customer_email |
| 112 |
* @param array $cart |
| 113 |
* |
| 114 |
* @return void |
| 115 |
*/ |
| 116 |
public function saveOrUpdateCheckout($checkout_id, $customer_email, $cart) |
| 117 |
{ |
| 118 |
global $wpdb; |
| 119 |
$table = $wpdb->prefix . 'mailerlite_checkouts'; |
| 120 |
|
| 121 |
if ($wpdb->get_var("SHOW TABLES LIKE '$table'") != $table) { |
| 122 |
$this->createMailerliteCheckoutsTable(); |
| 123 |
} |
| 124 |
|
| 125 |
$checkout = $this->getSavedCheckout($checkout_id); |
| 126 |
if ( ! empty($checkout) && ! empty($cart)) { |
| 127 |
$wpdb->query($wpdb->prepare("UPDATE $table |
| 128 |
SET email = %s, cart_content = %s |
| 129 |
WHERE checkout_id = %s", $customer_email, serialize($cart), $checkout_id) |
| 130 |
); |
| 131 |
} elseif ( ! empty($checkout) && empty($cart)) { |
| 132 |
$wpdb->delete($table, array('checkout_id' => $checkout_id)); |
| 133 |
} else { |
| 134 |
$wpdb->insert( |
| 135 |
$table, |
| 136 |
array( |
| 137 |
'time' => current_time('mysql'), |
| 138 |
'checkout_id' => $checkout_id, |
| 139 |
'email' => $customer_email, |
| 140 |
'cart_content' => serialize($cart) |
| 141 |
) |
| 142 |
); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Drop mailerlite checkouts table |
| 148 |
* woo_ml_drop_mailerlite_checkouts_table |
| 149 |
* @return void |
| 150 |
*/ |
| 151 |
public function dropMailerliteCheckoutsTable() |
| 152 |
{ |
| 153 |
global $wpdb; |
| 154 |
$table = $wpdb->prefix . 'mailerlite_checkouts'; |
| 155 |
$wpdb->query("DROP TABLE IF EXISTS $table"); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Intial creation of mailerlite_checkouts table |
| 160 |
* woo_ml_create_mailerlite_checkouts_table |
| 161 |
* @return void |
| 162 |
*/ |
| 163 |
public function createMailerliteCheckoutsTable() |
| 164 |
{ |
| 165 |
global $wpdb; |
| 166 |
$table = $wpdb->prefix . 'mailerlite_checkouts'; |
| 167 |
|
| 168 |
$charset_collate = $wpdb->get_charset_collate(); |
| 169 |
|
| 170 |
$sql = "CREATE TABLE $table( |
| 171 |
id mediumint(9) NOT NULL AUTO_INCREMENT, |
| 172 |
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, |
| 173 |
checkout_id varchar(55) NOT NULL, |
| 174 |
email text NOT NULL, |
| 175 |
cart_content text DEFAULT '' NOT NULL, |
| 176 |
PRIMARY KEY (id) |
| 177 |
) $charset_collate;"; |
| 178 |
|
| 179 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 180 |
dbDelta($sql); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Preparing checkout data for api |
| 185 |
* woo_ml_get_checkout_data |
| 186 |
* |
| 187 |
* @param $cookie_email |
| 188 |
* |
| 189 |
* @return array |
| 190 |
*/ |
| 191 |
public function getCheckoutData( |
| 192 |
$cookie_email = null, |
| 193 |
$subscribe = null, |
| 194 |
$language = null, |
| 195 |
$subscriber_fields = null |
| 196 |
) { |
| 197 |
if ( ! function_exists('WC')) { |
| 198 |
return false; |
| 199 |
} |
| 200 |
|
| 201 |
if (empty(WC()->cart)) { |
| 202 |
WC()->frontend_includes(); |
| 203 |
|
| 204 |
if ( ! did_action('woocommerce_load_cart_from_session') && function_exists('wc_load_cart')) { |
| 205 |
wc_load_cart(); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
if ( ! $cookie_email) { |
| 210 |
$cookie_email = $_COOKIE['mailerlite_checkout_email'] ?? $cookie_email; |
| 211 |
} |
| 212 |
|
| 213 |
if ( ! $subscribe) { |
| 214 |
$subscribe = (bool) ($_COOKIE['mailerlite_accepts_marketing'] ?? false); |
| 215 |
} |
| 216 |
|
| 217 |
$cart = WC()->cart; |
| 218 |
$cart_items = $cart->get_cart(); |
| 219 |
$customer = $cart->get_customer(); |
| 220 |
$customer_email = $customer->get_email(); |
| 221 |
if ( ! $customer_email) { |
| 222 |
$customer_email = $cookie_email; |
| 223 |
} |
| 224 |
|
| 225 |
// check if email was updated recently in checkout |
| 226 |
if (filter_var($cookie_email, FILTER_VALIDATE_EMAIL) && $customer_email !== $cookie_email) { |
| 227 |
$customer_email = $cookie_email; |
| 228 |
} |
| 229 |
|
| 230 |
$checkout_data = []; |
| 231 |
if ( ! empty($customer_email)) { |
| 232 |
$line_items = []; |
| 233 |
$total = 0; |
| 234 |
|
| 235 |
foreach ($cart_items as $key => $value) { |
| 236 |
$subtotal = intval($value['quantity']) * floatval($value['data']->get_price('edit')); |
| 237 |
|
| 238 |
$line_item = [ |
| 239 |
'key' => $key, |
| 240 |
'line_subtotal' => $subtotal, |
| 241 |
'line_total' => $subtotal, |
| 242 |
'product_id' => $value['product_id'], |
| 243 |
'quantity' => $value['quantity'], |
| 244 |
'variation' => $value['variation'], |
| 245 |
'variation_id' => $value['variation_id'] |
| 246 |
]; |
| 247 |
$line_items[] = $line_item; |
| 248 |
|
| 249 |
$total += $subtotal; |
| 250 |
} |
| 251 |
|
| 252 |
if ( ! isset($_COOKIE['mailerlite_checkout_token'])) { |
| 253 |
$checkout_id = md5(uniqid(rand(), true));; |
| 254 |
@setcookie('mailerlite_checkout_token', $checkout_id, time() + 172800, '/'); |
| 255 |
} else { |
| 256 |
$checkout_id = $_COOKIE['mailerlite_checkout_token']; |
| 257 |
} |
| 258 |
|
| 259 |
$shop_checkout_url = wc_get_checkout_url(); |
| 260 |
$checkout_url = $shop_checkout_url . '?ml_checkout=' . $checkout_id; |
| 261 |
|
| 262 |
$checkout_data = [ |
| 263 |
'id' => $checkout_id, |
| 264 |
'email' => $customer_email, |
| 265 |
'line_items' => $line_items, |
| 266 |
'abandoned_checkout_url' => $checkout_url, |
| 267 |
'total_price' => $total, |
| 268 |
'created_at' => date('Y-m-d h:m:s') |
| 269 |
]; |
| 270 |
|
| 271 |
if ($subscribe === true) { |
| 272 |
$checkout_data['subscribe'] = true; |
| 273 |
} |
| 274 |
|
| 275 |
if ( ! empty($language)) { |
| 276 |
$checkout_data['language'] = $language; |
| 277 |
} |
| 278 |
|
| 279 |
if ( ! empty($subscriber_fields)) { |
| 280 |
$checkout_data['subscriber_fields'] = $subscriber_fields; |
| 281 |
} |
| 282 |
|
| 283 |
$this->saveOrUpdateCheckout($checkout_id, $customer_email, $cart_items); |
| 284 |
} |
| 285 |
|
| 286 |
return $checkout_data; |
| 287 |
} |
| 288 |
} |