1(function( $ ) { 2 'use strict'; 3 4 jQuery(document).ready(function(){ 5 6 var timer; 7 var save_custom_fields = cartbounty_co.save_custom_fields; 8 var custom_checkout_fields = cartbounty_co.checkout_fields; 9 var custom_email_selectors = cartbounty_co.custom_email_selectors; 10 var custom_phone_selectors = cartbounty_co.custom_phone_selectors; 11 var custom_button_selectors = cartbounty_co.custom_button_selectors; 12 var contact_saved = localStorage.getItem('cartbounty_pro_contact_saved'); 13 var language = cartbounty_co.language; 14 var gtranslate_language = get_gtranslate_language(); 15 16 if( gtranslate_language ){ 17 language = gtranslate_language; 18 } 19 20 //Collect WooCommerce checkout fields 21 function getCheckoutData(){ 22 let email = jQuery('#email').val() || jQuery('#billing_email').val() || ''; 23 let phone = jQuery('#billing-phone').val() || jQuery('#shipping-phone').val() || jQuery('#billing_phone').val() || ''; 24 25 if(email.length > 0 || phone.length > 0){ 26 let checkoutFields = {}; 27 jQuery(custom_checkout_fields).each(function () { 28 let field = jQuery(this); 29 let fieldId = field.attr('id'); 30 let fieldValue = field.val(); 31 32 if(field.is('textarea') && field.closest('#order-notes').length > 0){ //If handling order notes field that does not have an ID 33 fieldId = 'order_notes'; 34 35 }else if(field.is(':checkbox')){ //If handling checkbox fields 36 // Handle checkbox fields 37 fieldValue = field.is(':checked') ? 1 : 0; 38 } 39 40 checkoutFields[fieldId] = fieldValue; 41 }); 42 43 clearTimeout(timer); 44 var emailValidation = cartbounty_co.email_validation; //Regex validation 45 var phoneValidation = cartbounty_co.phone_validation; //Regex validation 46 47 if ( email.match(emailValidation) || phone.match(phoneValidation) || localStorage.getItem('cartbounty_pro_pn_user_subscribed') ){ 48 timer = setTimeout(function(){ 49 let data = { 50 action: "cartbounty_pro_save", 51 nonce: cartbounty_co.nonce, 52 customer: checkoutFields, 53 language: language 54 } 55 56 if(!cartbounty_co.is_user_logged_in && cartbounty_co.recaptcha_enabled){ //If the user is not logged in and reCAPTCHA has been enabled 57 grecaptcha.ready(function() { //If reCAPTCHA is loaded and ready 58 grecaptcha.execute(cartbounty_co.recaptcha_site_key, {action: 'cartbounty_pro_abandoned_cart_checkout'}).then(function(token) { 59 data['cartbounty_pro_recaptcha_token'] = token; //Adding additional token element to the object 60 jQuery.post(cartbounty_co.ajaxurl, data, 61 function(response){ 62 63 if(response.success){ //If successfuly saved data 64 localStorage.setItem('cartbounty_pro_contact_saved', true); 65 removeExitIntentForm(); //If we have successfully captured abandoned cart, we do not have to display Exit intent form anymore 66 } 67 }); 68 }); 69 }); 70 71 }else{ 72 jQuery.post(cartbounty_co.ajaxurl, data, 73 function(response){ 74 75 if(response.success){ //If successfuly saved data 76 localStorage.setItem('cartbounty_pro_contact_saved', true); 77 removeExitIntentForm(); //If we have successfully captured abandoned cart, we do not have to display Exit intent form anymore 78 } 79 }); 80 } 81 }, 600); 82 } 83 } 84 } 85 86 function saveCustomField(){ //Function for saving custom email or phone field 87 var custom_field_selector = jQuery(this); 88 var cartbounty_pro_contact_saved = localStorage.getItem('cartbounty_pro_contact_saved'); 89 90 if(cartbounty_pro_contact_saved){ //Exit in case any of CartBounty tools have already saved data 91 return; 92 } 93 94 if(jQuery(custom_field_selector).length > 0 && !contact_saved){ //If email or phone field is present and contact information is not saved 95 var cartbounty_pro_custom_field = jQuery(custom_field_selector).val() || ''; 96 var emailValidation = cartbounty_co.email_validation; //Regex validation 97 var phoneValidation = cartbounty_co.phone_validation; //Regex validation 98 99 if(cartbounty_pro_custom_field != ''){ //If email or phone is not empty 100 101 if(cartbounty_pro_custom_field.match(emailValidation)){ 102 localStorage.setItem('cartbounty_pro_custom_email', cartbounty_pro_custom_field); //Saving user's input in browser memory 103 104 }else if(cartbounty_pro_custom_field.match(phoneValidation)){ //In case if phone number entered 105 localStorage.setItem('cartbounty_pro_custom_phone', cartbounty_pro_custom_field); //Saving user's input in browser memory 106 } 107 } 108 } 109 } 110 111 function passCustomFieldToCartBounty(){ //Function passes custom email or phone field to backend 112 var cartbounty_pro_custom_email_stored = localStorage.getItem('cartbounty_pro_custom_email'); 113 var cartbounty_pro_custom_phone_stored = localStorage.getItem('cartbounty_pro_custom_phone'); 114 var cartbounty_pro_contact_saved = localStorage.getItem('cartbounty_pro_contact_saved'); 115 116 if( ( cartbounty_pro_custom_email_stored == null && cartbounty_pro_custom_phone_stored == null ) || cartbounty_pro_contact_saved ){ //If data is missing or any of the CartBounty tools have already saved data - exit 117 return; 118 } 119 120 var fields = { 121 email: cartbounty_pro_custom_email_stored, 122 phone: cartbounty_pro_custom_phone_stored, 123 }; 124 125 var data = { 126 action: "cartbounty_pro_save", 127 source: "cartbounty_pro_custom_field", 128 nonce: cartbounty_co.nonce, 129 customer: fields, 130 language: language 131 } 132 133 if(!cartbounty_co.is_user_logged_in && cartbounty_co.recaptcha_enabled){ //If the user is not logged in and reCAPTCHA has been enabled 134 grecaptcha.ready(function() { //If reCAPTCHA is loaded and ready 135 grecaptcha.execute(cartbounty_co.recaptcha_site_key, {action: 'cartbounty_pro_abandoned_cart_tool'}).then(function(token) { 136 data['cartbounty_pro_recaptcha_token'] = token; //Adding additional token element to the object 137 jQuery.post(cartbounty_co.ajaxurl, data, //Send data over to backend for saving 138 function(response) { 139 if(response.success){ //If successfuly saved data 140 localStorage.setItem('cartbounty_pro_contact_saved', true); 141 removeCustomFields();
142 removeExitIntentForm(); 143 jQuery(document).off( 'added_to_cart', passCustomFieldToCartBounty ); 144 } 145 }); 146 }); 147 }); 148 149 }else{ //If the user is logged in or if the reCAPTCHA is disabled, not sending data to reCAPTCHA 150 jQuery.post(cartbounty_co.ajaxurl, data, //Send data over to backend for saving 151 function(response) { 152 if(response.success){ //If data successfuly saved 153 localStorage.setItem('cartbounty_pro_contact_saved', true); 154 removeCustomFields(); 155 removeExitIntentForm(); 156 jQuery(document).off('added_to_cart', passCustomFieldToCartBounty); 157 } 158 }); 159 } 160 } 161 162 function removeCustomFields(){ //Removing from local storage custom email and phone fields 163 localStorage.removeItem('cartbounty_pro_custom_email'); 164 localStorage.removeItem('cartbounty_pro_custom_phone'); 165 } 166 167 function removeExitIntentForm(){ //Removing Exit Intent popup 168 if(jQuery('#cartbounty-pro-exit-intent-form').length > 0){ //If Exit intent HTML exists on page 169 jQuery('#cartbounty-pro-exit-intent-form').remove(); 170 jQuery('#cartbounty-pro-exit-intent-form-backdrop').remove(); 171 } 172 } 173 174 //Adding a hidden input field to all "Add to cart" forms on click to protect against bots leaving anonymous carts since they may not use Javascript 175 function appendHiddenInputField(){ 176 var $target = jQuery(this); 177 var customInputField = jQuery('<input>',{ 178 type: 'hidden', 179 name: 'cartbounty_pro_bot_test', 180 value: '1' 181 }); 182 183 var $form = $target.closest('form.cart'); 184 185 if($form.length){ 186 $form.append(customInputField); 187 }else{ 188 $target.data('cartbounty_pro_bot_test', '1'); 189 } 190 } 191 192 //Adding additional CartBounty data whenever Ajax "Add to cart" button is clicked in order to detect anonymous carts left by bots 193 function addCartBountyInputDataToAjax(e, xhr, settings){ 194 if(settings.url.indexOf('wc-ajax=add_to_cart') > -1){ 195 settings.data += '&cartbounty_pro_bot_test=1'; 196 } 197 }; 198 199 //If Ajax Add to cart has been disabled under WooCommerce settings - store bot test result in local storage 200 function saveBotTestResult(){ 201 localStorage.setItem('cartbounty_pro_bot_test_passed', true); 202 } 203 204 //Save anonymous cart in case Ajax Add to cart has been disabled under WooCommerce settings and the cart has not been already saved. Necessary mostly for home shop page 205 function saveAnonymousCart(){ 206 let bot_test_passed = localStorage.getItem('cartbounty_pro_bot_test_passed'); 207 208 if(bot_test_passed){ 209 var data = { 210 action: "cartbounty_pro_save", 211 source: "cartbounty_pro_anonymous_bot_test", 212 nonce: cartbounty_co.nonce, 213 language: language 214 } 215 setTimeout(function(){ 216 jQuery.post(cartbounty_co.ajaxurl, data, //Send data over to backend for saving 217 function(response){ 218 if(response.success){ //If data successfuly saved 219 localStorage.removeItem('cartbounty_pro_bot_test_passed'); 220 } 221 }); 222 }, 1000); 223 } 224 } 225 226 //Retrieve GTranslate selected language 227 function get_gtranslate_language(){ 228 //Retrieve GTranslate selected language in case using the free GTranslate version 229 var cookie = document.cookie.match(/(?:^|;\s*)googtrans=([^;]*)/); 230 231 if (cookie && cookie[1]){ 232 var parts = cookie[1].split('/'); 233 234 if (parts.length === 3 && parts[2]){ 235 return parts[2]; // Example: '/en/es' â 'es' 236 } 237 } 238 239 //Retrieve GTranslate selected language in case using the paid GTranslate version 240 return document.documentElement.getAttribute('lang') || ''; 241 } 242 243 jQuery('.wc-block-checkout, .woocommerce-checkout').on( 'keyup keypress change', 'input, textarea, select', getCheckoutData ); 244 jQuery(window).on( 'load', getCheckoutData ); //Automatically collect and save input field data if input fields already filled on page load 245 jQuery(document).ajaxSend(addCartBountyInputDataToAjax); //Attaching custom data to the WooCommerce Ajax requests 246 jQuery(document).on('click', custom_button_selectors, appendHiddenInputField); 247 jQuery(document).on('click', custom_button_selectors, saveBotTestResult); 248 249 if( ( save_custom_fields && !contact_saved ) ){ //If custom field saving enabled and contact is not saved - try to
249save email or phone 250 passCustomFieldToCartBounty(); 251 jQuery(document).on( 'keyup keypress change', custom_email_selectors + ', ' + custom_phone_selectors, saveCustomField ); 252 jQuery(document).on( 'added_to_cart', passCustomFieldToCartBounty ); //Sending data over for saving in case WooCommerce "added_to_cart" event fires after product added to cart 253 } 254 255 saveAnonymousCart(); 256 }); 257 258})( jQuery );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.