| 1 |
(function($) { |
| 2 |
/** |
| 3 |
* Fetch checkout data and send it via AJAX |
| 4 |
*/ |
| 5 |
function fetchCheckoutData() { |
| 6 |
if ($('body').hasClass('woocommerce-order-received')) { |
| 7 |
return; // Do not trigger on the "Thank You" page |
| 8 |
} |
| 9 |
|
| 10 |
const fieldsData = { action: 'notifier_save_cart_abandonment_data' }; |
| 11 |
|
| 12 |
$('input[id^="billing_"], select[id^="billing_"], input[id^="billing-"], select[id^="billing-"], input[id^="shipping_"], select[id^="shipping_"], input[id^="shipping-"], select[id^="shipping-"]').each(function() { |
| 13 |
const fieldName = $(this).attr('id').replace(/[-]/g, '_'); // Normalize dashes to underscores |
| 14 |
if (fieldName && $(this).val()) { |
| 15 |
fieldsData[fieldName] = $(this).val(); |
| 16 |
} |
| 17 |
}); |
| 18 |
|
| 19 |
if ($('#email').length && $('#email').val()) { |
| 20 |
fieldsData['billing_email'] = $('#email').val(); |
| 21 |
} else if ($('#billing_email').length && $('#billing_email').val()) { |
| 22 |
fieldsData['billing_email'] = $('#billing_email').val(); |
| 23 |
} else { |
| 24 |
fieldsData['billing_email'] = ''; // Default to empty string if no email field is present |
| 25 |
} |
| 26 |
|
| 27 |
fieldsData.security = notifierFrontObj.security; |
| 28 |
|
| 29 |
$.ajax({ |
| 30 |
url: notifierFrontObj.ajaxurl, |
| 31 |
type: 'POST', |
| 32 |
data: fieldsData, |
| 33 |
success: function (response) { |
| 34 |
// Handle the response on success |
| 35 |
} |
| 36 |
}); |
| 37 |
} |
| 38 |
|
| 39 |
$(document).ready(function() { |
| 40 |
if ($('.woocommerce-checkout').length > 0) { |
| 41 |
setTimeout(fetchCheckoutData, 800); |
| 42 |
$(document.body).on('updated_checkout', fetchCheckoutData); |
| 43 |
$(document).on( |
| 44 |
'change', |
| 45 |
'input[id^="billing_"], select[id^="billing_"], input[id^="billing-"], select[id^="billing-"], input[id^="shipping_"], select[id^="shipping_"], input[id^="shipping-"], select[id^="shipping-"], #email', |
| 46 |
fetchCheckoutData |
| 47 |
); |
| 48 |
} |
| 49 |
}); |
| 50 |
|
| 51 |
})(jQuery); |
| 52 |
|