| 1 |
jQuery(document).ready(function(a) { |
| 2 |
const email = document.querySelector('#billing_email'); |
| 3 |
const first_name_field = document.querySelector('#billing_first_name'); |
| 4 |
const last_name_field = document.querySelector('#billing_last_name'); |
| 5 |
const signup = document.querySelector('#woo_ml_subscribe'); |
| 6 |
|
| 7 |
if (jQuery('#woo_ml_preselect_enabled')?.val() == 'yes') { |
| 8 |
jQuery('#woo_ml_subscribe').prop('checked', true); |
| 9 |
} |
| 10 |
|
| 11 |
if (email !== null) { |
| 12 |
email.addEventListener('blur', (event) => { |
| 13 |
validateMLSub(); |
| 14 |
}); |
| 15 |
} |
| 16 |
|
| 17 |
if (first_name_field !== null) { |
| 18 |
first_name_field.addEventListener('blur', (event) => { |
| 19 |
if(first_name_field.value.length > 0) { |
| 20 |
validateMLSub(); |
| 21 |
} |
| 22 |
}); |
| 23 |
} |
| 24 |
|
| 25 |
if (last_name_field !== null) { |
| 26 |
last_name_field.addEventListener('blur', (event) => { |
| 27 |
if(last_name_field.value.length > 0) { |
| 28 |
validateMLSub(); |
| 29 |
} |
| 30 |
}); |
| 31 |
} |
| 32 |
|
| 33 |
if (signup !== null) { |
| 34 |
a(document).on('change', signup, function(event) { |
| 35 |
validateMLSub(); |
| 36 |
}); |
| 37 |
} |
| 38 |
|
| 39 |
function validateMLSub() { |
| 40 |
if(email !== null && email.value.length > 0) { |
| 41 |
checkoutMLSub(); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
function checkoutMLSub() { |
| 46 |
/** set cookie before sending request to server |
| 47 |
* since multiple checkout update requests can be sent |
| 48 |
* and server cookies won't get updated, so send the saved |
| 49 |
* cookie as a request parameter |
| 50 |
**/ |
| 51 |
|
| 52 |
if (!getCookie('mailerlite_checkout_token')) { |
| 53 |
var now = new Date(); |
| 54 |
now.setTime(now.getTime() + 48 * 3600 * 1000); |
| 55 |
document.cookie = `mailerlite_checkout_token=${(+new Date).toString()}; expires=${now.toUTCString()}; path=/`; |
| 56 |
} |
| 57 |
|
| 58 |
const accept_marketing = document.querySelector('#woo_ml_subscribe').checked; |
| 59 |
|
| 60 |
let first_name = ''; |
| 61 |
let last_name = ''; |
| 62 |
|
| 63 |
if (first_name_field !== null) { |
| 64 |
first_name = first_name_field.value; |
| 65 |
} |
| 66 |
|
| 67 |
if (last_name_field !== null) { |
| 68 |
last_name = last_name_field.value; |
| 69 |
} |
| 70 |
|
| 71 |
jQuery.ajax({ |
| 72 |
url: woo_ml_public_post.ajax_url, |
| 73 |
type: "post", |
| 74 |
data: { |
| 75 |
action: "post_woo_ml_email_cookie", |
| 76 |
email: email.value, |
| 77 |
signup: accept_marketing, |
| 78 |
language: woo_ml_public_post.language, |
| 79 |
first_name: first_name, |
| 80 |
last_name: last_name, |
| 81 |
cookie_mailerlite_checkout_token:getCookie('mailerlite_checkout_token') |
| 82 |
} |
| 83 |
}) |
| 84 |
} |
| 85 |
}); |
| 86 |
|
| 87 |
function getCookie(name) { |
| 88 |
const value = `; ${document.cookie}`; |
| 89 |
const parts = value.split(`; ${name}=`); |
| 90 |
if (parts.length === 2) { |
| 91 |
return parts.pop().split(';').shift() |
| 92 |
} |
| 93 |
return null; |
| 94 |
} |