abandoned-carts.js
56 lines
| 1 | const EMAIL_FIELD_SELECTORS = [ |
| 2 | 'form.checkout input[name="billing_email"]', |
| 3 | 'form.woocommerce-checkout input[name="billing_email"]', |
| 4 | 'form[name="checkout"] input[type="email"]', |
| 5 | 'form.checkout input[type="email"]', |
| 6 | 'form[id*="checkout"] input[type="email"]', |
| 7 | 'form[class*="checkout"] input[type="email"]', |
| 8 | 'input#billing_email', |
| 9 | 'input[type="email"]' |
| 10 | ]; |
| 11 | |
| 12 | (function () { |
| 13 | |
| 14 | const findEmailInput = () => { |
| 15 | for (const field of EMAIL_FIELD_SELECTORS) { |
| 16 | let emailField = document.querySelector(field); |
| 17 | if (emailField) return emailField; |
| 18 | } |
| 19 | |
| 20 | return null; |
| 21 | } |
| 22 | |
| 23 | const handleAction = () => { |
| 24 | |
| 25 | const data = new FormData(); |
| 26 | data.append('action', hostinger_reach_email_capture_data.action); |
| 27 | data.append('nonce', hostinger_reach_email_capture_data.nonce); |
| 28 | |
| 29 | let email = findEmailInput(); |
| 30 | if (!email) { |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | data.append('email', email.value); |
| 35 | |
| 36 | fetch(hostinger_reach_email_capture_data.ajaxurl, { |
| 37 | method: 'POST', |
| 38 | body: data |
| 39 | }); |
| 40 | } |
| 41 | |
| 42 | const init = () => { |
| 43 | |
| 44 | const emailInput = findEmailInput(); |
| 45 | |
| 46 | if (!hostinger_reach_email_capture_data || !emailInput) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | emailInput.addEventListener('change', handleAction, {passive: true}); |
| 51 | emailInput.addEventListener('blur', handleAction, {passive: true}); |
| 52 | } |
| 53 | |
| 54 | document.addEventListener('DOMContentLoaded', init); |
| 55 | })(); |
| 56 |