| 1 |
( function ( $ ) { |
| 2 |
const wcf_update_checkout_on_return = function () { |
| 3 |
const vis = ( function () { |
| 4 |
let stateKey, eventKey; |
| 5 |
const keys = { |
| 6 |
hidden: 'visibilitychange', |
| 7 |
webkitHidden: 'webkitvisibilitychange', |
| 8 |
mozHidden: 'mozvisibilitychange', |
| 9 |
msHidden: 'msvisibilitychange', |
| 10 |
}; |
| 11 |
for ( stateKey in keys ) { |
| 12 |
if ( stateKey in document ) { |
| 13 |
eventKey = keys[ stateKey ]; |
| 14 |
break; |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
return function ( c ) { |
| 19 |
if ( c ) { |
| 20 |
document.addEventListener( eventKey, c ); |
| 21 |
} |
| 22 |
return ! document[ stateKey ]; |
| 23 |
}; |
| 24 |
} )(); |
| 25 |
|
| 26 |
function getCookie( name ) { |
| 27 |
const cookieArr = document.cookie.split( ';' ); |
| 28 |
for ( let i = 0; i < cookieArr.length; i++ ) { |
| 29 |
const cookiePair = cookieArr[ i ].split( '=' ); |
| 30 |
|
| 31 |
if ( name === cookiePair[ 0 ].trim() ) { |
| 32 |
return decodeURIComponent( cookiePair[ 1 ] ); |
| 33 |
} |
| 34 |
} |
| 35 |
return null; |
| 36 |
} |
| 37 |
|
| 38 |
vis( function () { |
| 39 |
const active_checkout_cookie = getCookie( |
| 40 |
cartflows.active_checkout_cookie |
| 41 |
); |
| 42 |
|
| 43 |
if ( active_checkout_cookie && vis() ) { |
| 44 |
if ( |
| 45 |
parseInt( cartflows.current_step ) !== |
| 46 |
parseInt( active_checkout_cookie ) |
| 47 |
) { |
| 48 |
// Add loader. |
| 49 |
$( |
| 50 |
'.woocommerce-checkout-payment, .woocommerce-checkout-review-order-table' |
| 51 |
).block( { |
| 52 |
message: null, |
| 53 |
overlayCSS: { |
| 54 |
background: '#fff', |
| 55 |
opacity: 0.6, |
| 56 |
}, |
| 57 |
} ); |
| 58 |
|
| 59 |
console.log( 'Multiple checkouts are open.' ); |
| 60 |
|
| 61 |
$( document.body ).trigger( 'update_checkout' ); |
| 62 |
|
| 63 |
$( document ).ajaxComplete( function ( event, xhr ) { |
| 64 |
if ( ! xhr.hasOwnProperty( 'responseJSON' ) ) { |
| 65 |
return; |
| 66 |
} |
| 67 |
const fragmants = xhr.responseJSON.hasOwnProperty( |
| 68 |
'fragments' |
| 69 |
) |
| 70 |
? xhr.responseJSON.fragments |
| 71 |
: null; |
| 72 |
|
| 73 |
if ( |
| 74 |
fragmants && |
| 75 |
fragmants.hasOwnProperty( 'wcf_cart_data' ) |
| 76 |
) { |
| 77 |
$( document.body ).trigger( |
| 78 |
'wcf_cart_data_restored', |
| 79 |
[ fragmants.wcf_cart_data ] |
| 80 |
); |
| 81 |
} |
| 82 |
} ); |
| 83 |
} |
| 84 |
} |
| 85 |
} ); |
| 86 |
}; |
| 87 |
|
| 88 |
/** |
| 89 |
* Checkout Custom Field Validations |
| 90 |
* This will collect all the present fields in the woocommerce form and adds an class if the field |
| 91 |
* is blank |
| 92 |
*/ |
| 93 |
const wcf_custom_field_validation = function () { |
| 94 |
/** |
| 95 |
* Controls the display of the error message on the basis of backend setting. |
| 96 |
* |
| 97 |
* @param {boolean} field_required |
| 98 |
* @param {string} field_row |
| 99 |
* @param {string} field_wrap |
| 100 |
*/ |
| 101 |
const add_validation_msg = function ( |
| 102 |
field_required = false, |
| 103 |
field_row, |
| 104 |
field_wrap |
| 105 |
) { |
| 106 |
field_row.find( '.wcf-field-required-error' ).remove(); |
| 107 |
|
| 108 |
if ( |
| 109 |
field_required && |
| 110 |
'yes' === cartflows.field_validation.is_enabled |
| 111 |
) { |
| 112 |
const label_text = field_row.find( 'label' ).text(); |
| 113 |
field_wrap.after( |
| 114 |
'<span class="wcf-field-required-error">' + |
| 115 |
label_text.replace( /\*/g, '' ).trim() + |
| 116 |
' ' + |
| 117 |
cartflows.field_validation.error_msg + |
| 118 |
'</span>' |
| 119 |
); |
| 120 |
} else { |
| 121 |
field_row.find( '.wcf-field-required-error' ).remove(); |
| 122 |
} |
| 123 |
}; |
| 124 |
|
| 125 |
const custom_field_add_class = function ( |
| 126 |
field_value, |
| 127 |
field_row, |
| 128 |
field_wrap, |
| 129 |
field_type |
| 130 |
) { |
| 131 |
let isError = false; |
| 132 |
|
| 133 |
if ( |
| 134 |
field_value === '' || |
| 135 |
( 'select' === field_type && field_value === ' ' ) |
| 136 |
) { |
| 137 |
if ( field_row.hasClass( 'validate-required' ) ) { |
| 138 |
field_wrap.addClass( 'field-required' ); |
| 139 |
isError = true; |
| 140 |
} |
| 141 |
} else { |
| 142 |
field_wrap.removeClass( 'field-required' ); |
| 143 |
} |
| 144 |
|
| 145 |
add_validation_msg( isError, field_row, field_wrap ); |
| 146 |
}; |
| 147 |
|
| 148 |
const numberFieldValidation = function ( |
| 149 |
field_value, |
| 150 |
field_row, |
| 151 |
field_wrap, |
| 152 |
minValue, |
| 153 |
maxValue |
| 154 |
) { |
| 155 |
if ( |
| 156 |
field_value === '' || |
| 157 |
field_value < minValue || |
| 158 |
field_value > maxValue |
| 159 |
) { |
| 160 |
field_wrap.addClass( 'field-required' ); |
| 161 |
field_wrap.after( |
| 162 |
'<span class="wcf-field-required-error">' + |
| 163 |
cartflows.field_validation_msgs.number_field + |
| 164 |
minValue + |
| 165 |
' & ' + |
| 166 |
maxValue + |
| 167 |
'</span>' |
| 168 |
); |
| 169 |
} else { |
| 170 |
field_wrap.removeClass( 'field-required' ); |
| 171 |
field_row.find( '.wcf-field-required-error' ).remove(); |
| 172 |
} |
| 173 |
}; |
| 174 |
|
| 175 |
const fields_wrapper = $( 'form.woocommerce-checkout' ), |
| 176 |
$all_fields = fields_wrapper.find( 'input, textarea' ), |
| 177 |
$selects = fields_wrapper.find( 'select' ); |
| 178 |
|
| 179 |
$all_fields.on( 'blur', function () { |
| 180 |
const $this = $( this ), |
| 181 |
field_type = $this.attr( 'type' ), |
| 182 |
field_row = $this.closest( 'p.form-row' ), |
| 183 |
field_value = $this.val(); |
| 184 |
|
| 185 |
custom_field_add_class( field_value, field_row, $this, field_type ); |
| 186 |
if ( 'number' === field_type ) { |
| 187 |
const minValue = $this.attr( 'min' ); |
| 188 |
const maxValue = $this.attr( 'max' ); |
| 189 |
numberFieldValidation( |
| 190 |
Number( field_value ), |
| 191 |
field_row, |
| 192 |
$this, |
| 193 |
Number( minValue ), |
| 194 |
Number( maxValue ) |
| 195 |
); |
| 196 |
} |
| 197 |
} ); |
| 198 |
|
| 199 |
$selects.on( 'blur', function () { |
| 200 |
const $this = $( this ), |
| 201 |
field_row = $this.closest( 'p.form-row' ), |
| 202 |
field_type = 'select', |
| 203 |
field_wrap = field_row.find( '.select2-container--default' ), |
| 204 |
field_value = field_row.find( 'select' ).val(); |
| 205 |
|
| 206 |
custom_field_add_class( |
| 207 |
field_value, |
| 208 |
field_row, |
| 209 |
field_wrap, |
| 210 |
field_type |
| 211 |
); |
| 212 |
} ); |
| 213 |
}; |
| 214 |
|
| 215 |
const wcf_check_is_local_storage = function () { |
| 216 |
const test = 'test'; |
| 217 |
try { |
| 218 |
localStorage.setItem( test, test ); |
| 219 |
localStorage.removeItem( test ); |
| 220 |
return true; |
| 221 |
} catch ( e ) { |
| 222 |
return false; |
| 223 |
} |
| 224 |
}; |
| 225 |
|
| 226 |
const wcf_persistent_data = function () { |
| 227 |
if ( 'yes' !== cartflows.allow_persistence ) { |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
if ( false === wcf_check_is_local_storage() ) { |
| 232 |
return; |
| 233 |
} |
| 234 |
|
| 235 |
const checkout_cust_form = |
| 236 |
'form.woocommerce-checkout #customer_details'; |
| 237 |
|
| 238 |
const wcf_form_data = { |
| 239 |
set() { |
| 240 |
const checkout_data = []; |
| 241 |
const checkout_form = $( |
| 242 |
'form.woocommerce-checkout #customer_details' |
| 243 |
); |
| 244 |
|
| 245 |
localStorage.removeItem( 'cartflows_checkout_form' ); |
| 246 |
|
| 247 |
checkout_form |
| 248 |
.find( |
| 249 |
'input[type=text], select, input[type=email], input[type=tel]' |
| 250 |
) |
| 251 |
.each( function () { |
| 252 |
checkout_data.push( { |
| 253 |
name: this.name, |
| 254 |
value: this.value, |
| 255 |
} ); |
| 256 |
} ); |
| 257 |
|
| 258 |
// Fix the dutch address order. |
| 259 |
fixDutchAddressOrder(); |
| 260 |
|
| 261 |
cartflows_checkout_form = JSON.stringify( checkout_data ); |
| 262 |
localStorage.setItem( |
| 263 |
'cartflows_checkout_form', |
| 264 |
cartflows_checkout_form |
| 265 |
); |
| 266 |
}, |
| 267 |
get() { |
| 268 |
if ( |
| 269 |
localStorage.getItem( 'cartflows_checkout_form' ) !== null |
| 270 |
) { |
| 271 |
checkout_data = JSON.parse( |
| 272 |
localStorage.getItem( 'cartflows_checkout_form' ) |
| 273 |
); |
| 274 |
|
| 275 |
for ( let i = 0; i < checkout_data.length; i++ ) { |
| 276 |
if ( |
| 277 |
$( |
| 278 |
'form.woocommerce-checkout [name=' + |
| 279 |
checkout_data[ i ].name + |
| 280 |
']' |
| 281 |
).hasClass( 'select2-hidden-accessible' ) |
| 282 |
) { |
| 283 |
$( |
| 284 |
'form.woocommerce-checkout [name=' + |
| 285 |
checkout_data[ i ].name + |
| 286 |
']' |
| 287 |
).selectWoo( 'val', [ checkout_data[ i ].value ] ); |
| 288 |
} else { |
| 289 |
$( |
| 290 |
'form.woocommerce-checkout [name=' + |
| 291 |
checkout_data[ i ].name + |
| 292 |
']' |
| 293 |
).val( checkout_data[ i ].value ); |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
}, |
| 298 |
}; |
| 299 |
|
| 300 |
const fixDutchAddressOrder = function () { |
| 301 |
const billing_country = $( '#billing_country' ).val(); |
| 302 |
const billing_address = $( '#billing_address_1' ).val(); |
| 303 |
|
| 304 |
if ( billing_country === 'NL' && billing_address ) { |
| 305 |
const match_billing = |
| 306 |
billing_address.match( /^(\d+\S*)\s+(.+)$/ ); |
| 307 |
if ( match_billing ) { |
| 308 |
$( '#billing_address_1' ).val( |
| 309 |
match_billing[ 2 ] + ' ' + match_billing[ 1 ] |
| 310 |
); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
const shipping_country = $( '#shipping_country' ).val(); |
| 315 |
const shipping_address = $( '#shipping_address_1' ).val(); |
| 316 |
|
| 317 |
if ( shipping_country === 'NL' && shipping_address ) { |
| 318 |
const match_shipping = |
| 319 |
shipping_address.match( /^(\d+\S*)\s+(.+)$/ ); |
| 320 |
if ( match_shipping ) { |
| 321 |
$( '#shipping_address_1' ).val( |
| 322 |
match_shipping[ 2 ] + ' ' + match_shipping[ 1 ] |
| 323 |
); |
| 324 |
} |
| 325 |
} |
| 326 |
}; |
| 327 |
|
| 328 |
wcf_form_data.get(); |
| 329 |
|
| 330 |
$( |
| 331 |
checkout_cust_form + ' input, ' + checkout_cust_form + ' select' |
| 332 |
).on( 'change', function () { |
| 333 |
wcf_form_data.set(); |
| 334 |
} ); |
| 335 |
}; |
| 336 |
|
| 337 |
const wcf_checkout_coupons = { |
| 338 |
init() { |
| 339 |
$( document.body ).on( |
| 340 |
'click', |
| 341 |
'.wcf-submit-coupon', |
| 342 |
this.submit_coupon |
| 343 |
); |
| 344 |
$( document.body ).on( |
| 345 |
'click', |
| 346 |
'.wcf-remove-coupon', |
| 347 |
this.remove_coupon |
| 348 |
); |
| 349 |
}, |
| 350 |
|
| 351 |
submit_coupon( e ) { |
| 352 |
e.preventDefault(); |
| 353 |
const coupon_wrapper_class = $( '.wcf-custom-coupon-field' ), |
| 354 |
coupon_wrapper = $( this ).closest( coupon_wrapper_class ), |
| 355 |
coupon_field = coupon_wrapper.find( '.wcf-coupon-code-input' ), |
| 356 |
coupon_value = coupon_field.val(); |
| 357 |
|
| 358 |
if ( '' === coupon_value ) { |
| 359 |
coupon_field.addClass( 'field-required' ); |
| 360 |
return false; |
| 361 |
} |
| 362 |
coupon_field.removeClass( 'field-required' ); |
| 363 |
|
| 364 |
const billing_email = $( |
| 365 |
'.wcf-embed-checkout-form #billing_email' |
| 366 |
).val(); |
| 367 |
|
| 368 |
const data = { |
| 369 |
coupon_code: coupon_value, |
| 370 |
action: 'wcf_woo_apply_coupon', |
| 371 |
security: cartflows.wcf_validate_coupon_nonce, |
| 372 |
billing_email, |
| 373 |
}; |
| 374 |
|
| 375 |
$.ajax( { |
| 376 |
type: 'POST', |
| 377 |
url: cartflows.ajax_url, |
| 378 |
data, |
| 379 |
|
| 380 |
success( code_data ) { |
| 381 |
const coupon_message = $( '.wcf-custom-coupon-field' ); |
| 382 |
coupon_message |
| 383 |
.find( '.woocommerce-error, .woocommerce-message' ) |
| 384 |
.remove(); |
| 385 |
|
| 386 |
if ( code_data && code_data.status === true ) { |
| 387 |
$( document.body ).trigger( 'update_checkout', { |
| 388 |
update_shipping_method: false, |
| 389 |
} ); |
| 390 |
coupon_message.prepend( code_data.msg ); |
| 391 |
coupon_wrapper_class.addClass( 'wcf-coupon-applied' ); |
| 392 |
} else if ( code_data && code_data.msg ) { |
| 393 |
coupon_message.prepend( code_data.msg ); |
| 394 |
coupon_wrapper_class.removeClass( |
| 395 |
'wcf-coupon-applied' |
| 396 |
); |
| 397 |
} else { |
| 398 |
console.log( |
| 399 |
'Error: Error while applying the coupon. Response: ' + |
| 400 |
code_data.data && code_data.data.error |
| 401 |
? code_data.data.error |
| 402 |
: code_data |
| 403 |
); |
| 404 |
} |
| 405 |
}, |
| 406 |
} ); |
| 407 |
}, |
| 408 |
|
| 409 |
remove_coupon( e ) { |
| 410 |
e.preventDefault(); |
| 411 |
const data = { |
| 412 |
coupon_code: $( this ).attr( 'data-coupon' ), |
| 413 |
action: 'wcf_woo_remove_coupon', |
| 414 |
security: cartflows.wcf_validate_remove_coupon_nonce, |
| 415 |
}; |
| 416 |
|
| 417 |
$.ajax( { |
| 418 |
type: 'POST', |
| 419 |
url: cartflows.ajax_url, |
| 420 |
data, |
| 421 |
|
| 422 |
success( code ) { |
| 423 |
const coupon_message = $( '.wcf-custom-coupon-field' ); |
| 424 |
coupon_message |
| 425 |
.find( '.woocommerce-error, .woocommerce-message' ) |
| 426 |
.hide(); |
| 427 |
|
| 428 |
$( '.wcf-custom-coupon-field' ).removeClass( |
| 429 |
'wcf-coupon-applied' |
| 430 |
); |
| 431 |
|
| 432 |
if ( code ) { |
| 433 |
$( document.body ).trigger( 'update_checkout', { |
| 434 |
update_shipping_method: false, |
| 435 |
} ); |
| 436 |
coupon_message.prepend( code ); |
| 437 |
} |
| 438 |
}, |
| 439 |
} ); |
| 440 |
}, |
| 441 |
}; |
| 442 |
|
| 443 |
const wcf_remove_cart_products = function () { |
| 444 |
$( document.body ).on( |
| 445 |
'click', |
| 446 |
'#wcf-embed-checkout-form .wcf-remove-product', |
| 447 |
function ( e ) { |
| 448 |
e.preventDefault(); |
| 449 |
const p_id = $( this ).attr( 'data-id' ); |
| 450 |
const data = { |
| 451 |
p_key: $( this ).attr( 'data-item-key' ), |
| 452 |
p_id, |
| 453 |
action: 'wcf_woo_remove_cart_product', |
| 454 |
security: cartflows.wcf_validate_remove_cart_product_nonce, |
| 455 |
}; |
| 456 |
|
| 457 |
$.ajax( { |
| 458 |
type: 'POST', |
| 459 |
url: cartflows.ajax_url, |
| 460 |
data, |
| 461 |
|
| 462 |
success( response ) { |
| 463 |
const res_data = JSON.parse( response ); |
| 464 |
|
| 465 |
if ( res_data.need_shipping === false ) { |
| 466 |
// $('#wcf-embed-checkout-form').find('#ship-to-different-address-checkbox').hide(); |
| 467 |
$( '#wcf-embed-checkout-form' ) |
| 468 |
.find( '#ship-to-different-address-checkbox' ) |
| 469 |
.attr( 'checked', false ); |
| 470 |
} |
| 471 |
|
| 472 |
// Handle order bump checkbox state when product is removed. |
| 473 |
if ( |
| 474 |
typeof res_data.is_order_bump !== 'undefined' && |
| 475 |
res_data.is_order_bump && |
| 476 |
res_data.order_bump_id |
| 477 |
) { |
| 478 |
$( |
| 479 |
'input[name="wcf-bump-order-cb-' + |
| 480 |
res_data.order_bump_id + |
| 481 |
'"]' |
| 482 |
).prop( 'checked', false ); |
| 483 |
} |
| 484 |
|
| 485 |
$( '#wcf-embed-checkout-form' ) |
| 486 |
.find( '.woocommerce-notices-wrapper' ) |
| 487 |
.first() |
| 488 |
.html( res_data.msg ); |
| 489 |
$( document ).trigger( 'cartflows_remove_product', [ |
| 490 |
p_id, |
| 491 |
] ); |
| 492 |
$( '#wcf-embed-checkout-form' ).trigger( |
| 493 |
'update_checkout' |
| 494 |
); |
| 495 |
}, |
| 496 |
} ); |
| 497 |
} |
| 498 |
); |
| 499 |
}; |
| 500 |
|
| 501 |
const wcf_toggle_optimized_fields = function () { |
| 502 |
jQuery.each( |
| 503 |
cartflows_checkout_optimized_fields, |
| 504 |
function ( field, cartflows_optimized_field ) { |
| 505 |
if ( cartflows_optimized_field.is_optimized ) { |
| 506 |
jQuery( '#' + field ).prepend( |
| 507 |
'<a href="#" id="wcf_optimized_' + |
| 508 |
field + |
| 509 |
'">' + |
| 510 |
cartflows_optimized_field.field_label + |
| 511 |
'</a>' |
| 512 |
); |
| 513 |
jQuery( '#wcf_optimized_' + field ).on( |
| 514 |
'click', |
| 515 |
function ( e ) { |
| 516 |
e.preventDefault(); |
| 517 |
jQuery( '#' + field ).removeClass( |
| 518 |
'wcf-hide-field' |
| 519 |
); |
| 520 |
// jQuery("#" + field).removeClass('mt20') |
| 521 |
const field_id = field.replace( /_field/g, '' ); |
| 522 |
$( '#' + field_id ).trigger( 'focus' ); |
| 523 |
jQuery( this ).remove(); |
| 524 |
} |
| 525 |
); |
| 526 |
} |
| 527 |
} |
| 528 |
); |
| 529 |
}; |
| 530 |
|
| 531 |
const wcf_instant_checkout_coupon_field = function () { |
| 532 |
$( document.body ).on( |
| 533 |
'click', |
| 534 |
'.coupon-field .wcf-custom-coupon-field .wcf-optimized-coupon-field', |
| 535 |
function ( e ) { |
| 536 |
e.preventDefault(); |
| 537 |
|
| 538 |
$( '.wcf-custom-coupon-field' ).removeClass( 'wcf-hide-field' ); |
| 539 |
$( this ).remove(); |
| 540 |
} |
| 541 |
); |
| 542 |
}; |
| 543 |
|
| 544 |
const wcf_anim_field_style_two = function () { |
| 545 |
const $inputs = $( |
| 546 |
'.wcf-field-modern-label .woocommerce input, .wcf-field-modern-label .woocommerce select, .wcf-field-modern-label .woocommerce textarea' |
| 547 |
); |
| 548 |
|
| 549 |
/** |
| 550 |
* Add wcf-anim-label class when floating label option is enabled. |
| 551 |
* The class will be added only when the info is started to type in the checkout fields. |
| 552 |
* |
| 553 |
* @param {Object} $this the object of current checkout field |
| 554 |
*/ |
| 555 |
const _add_anim_class = function ( $this ) { |
| 556 |
const field_row = $this.closest( '.form-row' ), |
| 557 |
is_select = |
| 558 |
$this.is( 'select' ) || |
| 559 |
$this.hasClass( 'select2-hidden-accessible' ), |
| 560 |
field_value = is_select |
| 561 |
? $this.find( ':selected' ).text() |
| 562 |
: $this.val(), |
| 563 |
field_type = $this.attr( 'type' ); |
| 564 |
|
| 565 |
if ( '' === field_value ) { |
| 566 |
field_row.removeClass( 'wcf-anim-label' ); |
| 567 |
} else if ( 'hidden' === field_type ) { |
| 568 |
field_row.addClass( 'wcf-anim-hidden-label' ); |
| 569 |
} else { |
| 570 |
field_row.addClass( 'wcf-anim-label' ); |
| 571 |
} |
| 572 |
}; |
| 573 |
|
| 574 |
// Trigger the addition of anim class when focused or info is inputed on the field. |
| 575 |
$inputs.on( 'focusout input', function () { |
| 576 |
const $this = $( this ); |
| 577 |
_add_anim_class( $this ); |
| 578 |
} ); |
| 579 |
|
| 580 |
// Load the anim class on the page ready. |
| 581 |
$( $inputs ).each( function () { |
| 582 |
_add_anim_class( $( this ) ); |
| 583 |
} ); |
| 584 |
}; |
| 585 |
|
| 586 |
const validateEmail = function ( email ) { |
| 587 |
const email_reg = |
| 588 |
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; |
| 589 |
return email_reg.test( email ); |
| 590 |
}; |
| 591 |
|
| 592 |
let xhrCountEmail = 0; |
| 593 |
let delayTimerEmail; |
| 594 |
|
| 595 |
const inline_email_address_validation = function () { |
| 596 |
const email = $( '.wcf-customer-info #billing_email' ).val(); |
| 597 |
|
| 598 |
if ( 'undefined' === typeof email || cartflows.is_logged_in ) { |
| 599 |
return; |
| 600 |
} |
| 601 |
|
| 602 |
if ( '' !== email ) { |
| 603 |
const email_field = $( '#billing_email' ); |
| 604 |
const validation_msg_wrap = $( '.wcf-email-validation-block' ), |
| 605 |
customer_login_wrap = $( '.wcf-customer-login-section' ); |
| 606 |
|
| 607 |
validation_msg_wrap.remove(); |
| 608 |
|
| 609 |
if ( ! validateEmail( email ) ) { |
| 610 |
email_field.after( |
| 611 |
'<span class="wcf-email-validation-block error">' + |
| 612 |
cartflows.email_validation_msgs.error_msg + |
| 613 |
'</span>' |
| 614 |
); |
| 615 |
customer_login_wrap.removeClass( 'wcf-show' ); |
| 616 |
return false; |
| 617 |
} |
| 618 |
|
| 619 |
clearTimeout( delayTimerEmail ); |
| 620 |
|
| 621 |
const seqNumber = ++xhrCountEmail; |
| 622 |
|
| 623 |
delayTimerEmail = setTimeout( function () { |
| 624 |
$.ajax( { |
| 625 |
url: cartflows.ajax_url, |
| 626 |
type: 'POST', |
| 627 |
data: { |
| 628 |
action: 'wcf_check_email_exists', |
| 629 |
email_address: email, |
| 630 |
security: cartflows.check_email_exist_nonce, |
| 631 |
}, |
| 632 |
success( resp ) { |
| 633 |
if ( seqNumber !== xhrCountEmail ) { |
| 634 |
return; |
| 635 |
} |
| 636 |
|
| 637 |
validation_msg_wrap.remove(); |
| 638 |
|
| 639 |
if ( |
| 640 |
resp.data && |
| 641 |
resp.data.success && |
| 642 |
customer_login_wrap.hasClass( 'wcf-show' ) |
| 643 |
) { |
| 644 |
email_field.after( |
| 645 |
'<span class="wcf-email-validation-block success">' + |
| 646 |
cartflows.email_validation_msgs |
| 647 |
.success_msg + |
| 648 |
'</span>' |
| 649 |
); |
| 650 |
return; |
| 651 |
} |
| 652 |
|
| 653 |
if ( resp.data && resp.data.success ) { |
| 654 |
if ( resp.data.is_login_allowed ) { |
| 655 |
email_field.after( |
| 656 |
'<span class="wcf-email-validation-block success">' + |
| 657 |
cartflows.email_validation_msgs |
| 658 |
.success_msg + |
| 659 |
'</span>' |
| 660 |
); |
| 661 |
|
| 662 |
customer_login_wrap |
| 663 |
.slideDown( 400 ) |
| 664 |
.addClass( 'wcf-show' ); |
| 665 |
} |
| 666 |
|
| 667 |
$( '.wcf-create-account-section' ).hide(); |
| 668 |
$( |
| 669 |
'.woocommerce-billing-fields__customer-login-label' |
| 670 |
).show(); |
| 671 |
} else { |
| 672 |
customer_login_wrap |
| 673 |
.slideUp( 400 ) |
| 674 |
.removeClass( 'wcf-show' ); |
| 675 |
|
| 676 |
//Learndash Woo integration plugin hides the create aacount checkbox.So need to show it again. |
| 677 |
$( |
| 678 |
'.wcf-create-account-section .create-account label.checkbox' |
| 679 |
).show(); |
| 680 |
|
| 681 |
$( '.wcf-create-account-section' ).show(); |
| 682 |
|
| 683 |
$( |
| 684 |
'.woocommerce-billing-fields__customer-login-label' |
| 685 |
).hide(); |
| 686 |
} |
| 687 |
}, |
| 688 |
} ); |
| 689 |
}, 300 ); |
| 690 |
} else { |
| 691 |
$( '.wcf-create-account-section' ).hide(); |
| 692 |
$( '.wcf-customer-login-section' ).hide(); |
| 693 |
$( '.wcf-email-validation-block' ).hide(); |
| 694 |
$( '.woocommerce-billing-fields__customer-login-label' ).show(); |
| 695 |
} |
| 696 |
|
| 697 |
return false; |
| 698 |
}; |
| 699 |
|
| 700 |
const woocommerce_user_login = function () { |
| 701 |
$( '.wcf-customer-login-url' ).on( 'click', function login_form( e ) { |
| 702 |
e.preventDefault(); |
| 703 |
const customer_login_wrap = $( '.wcf-customer-login-section' ); |
| 704 |
|
| 705 |
if ( customer_login_wrap.hasClass( 'wcf-show' ) ) { |
| 706 |
customer_login_wrap.slideUp( 400 ); |
| 707 |
customer_login_wrap.removeClass( 'wcf-show' ); |
| 708 |
} else { |
| 709 |
customer_login_wrap.slideDown( 400 ); |
| 710 |
customer_login_wrap.addClass( 'wcf-show' ); |
| 711 |
} |
| 712 |
} ); |
| 713 |
|
| 714 |
$( '.wcf-customer-login-section__login-button' ).on( |
| 715 |
'click', |
| 716 |
function name() { |
| 717 |
const email = $( '#billing_email' ).val(); |
| 718 |
const password = $( '#billing_password' ).val(); |
| 719 |
|
| 720 |
$.ajax( { |
| 721 |
url: cartflows.ajax_url, |
| 722 |
type: 'POST', |
| 723 |
data: { |
| 724 |
action: 'wcf_woocommerce_login', |
| 725 |
email, |
| 726 |
password, |
| 727 |
security: cartflows.woocommerce_login_nonce, |
| 728 |
}, |
| 729 |
success( resp ) { |
| 730 |
if ( resp.data && resp.data.success ) { |
| 731 |
location.reload(); |
| 732 |
} else { |
| 733 |
$( '.wcf-customer-info__notice' ) |
| 734 |
.addClass( 'wcf-notice' ) |
| 735 |
.html( resp.data.error ); |
| 736 |
} |
| 737 |
}, |
| 738 |
} ); |
| 739 |
} |
| 740 |
); |
| 741 |
}; |
| 742 |
|
| 743 |
const wcf_instant_checkout_relocate_notices = function () { |
| 744 |
if ( $( 'body' ).hasClass( 'cartflows-instant-checkout' ) ) { |
| 745 |
$( '.woocommerce-NoticeGroup' ).prependTo( |
| 746 |
'.woocommerce-notices-wrapper' |
| 747 |
); |
| 748 |
} |
| 749 |
}; |
| 750 |
|
| 751 |
const wcf_file_upload = { |
| 752 |
init() { |
| 753 |
$( document.body ).on( |
| 754 |
'change', |
| 755 |
'.wcf-checkout-file-input', |
| 756 |
this.handle_file_change |
| 757 |
); |
| 758 |
}, |
| 759 |
|
| 760 |
handle_file_change() { |
| 761 |
const $input = $( this ), |
| 762 |
fieldKey = $input.data( 'field-key' ), |
| 763 |
$hiddenField = $( '#' + fieldKey ), |
| 764 |
$error = $( '#' + fieldKey + '_error' ), |
| 765 |
maxSize = parseInt( $input.data( 'max-size' ) ) || 5; |
| 766 |
|
| 767 |
// Clear previous errors. |
| 768 |
$error.empty(); |
| 769 |
|
| 770 |
if ( ! this.files.length ) { |
| 771 |
$hiddenField.val( '' ); |
| 772 |
return; |
| 773 |
} |
| 774 |
|
| 775 |
const file = this.files[ 0 ]; |
| 776 |
|
| 777 |
// Client-side file size validation. |
| 778 |
if ( file.size > maxSize * 1024 * 1024 ) { |
| 779 |
$error.html( 'File size exceeds ' + maxSize + 'MB limit.' ); |
| 780 |
$input.val( '' ); |
| 781 |
return; |
| 782 |
} |
| 783 |
|
| 784 |
// Client-side file type validation. |
| 785 |
const ext = file.name.split( '.' ).pop().toLowerCase(); |
| 786 |
|
| 787 |
let allowed = []; |
| 788 |
|
| 789 |
const allowedTypes = ( |
| 790 |
$input.data( 'allowed-types' ) || '' |
| 791 |
).toString(); |
| 792 |
if ( allowedTypes ) { |
| 793 |
allowed = allowedTypes.split( ',' ).map( function ( t ) { |
| 794 |
return t.trim().toLowerCase(); |
| 795 |
} ); |
| 796 |
} else { |
| 797 |
// Fallback to master allowlist. |
| 798 |
allowed = [ |
| 799 |
'jpg', |
| 800 |
'jpeg', |
| 801 |
'png', |
| 802 |
'webp', |
| 803 |
'gif', |
| 804 |
'pdf', |
| 805 |
'mp3', |
| 806 |
'm4a', |
| 807 |
'wav', |
| 808 |
'mp4', |
| 809 |
'mov', |
| 810 |
]; |
| 811 |
} |
| 812 |
|
| 813 |
if ( allowed.length && ! allowed.includes( ext ) ) { |
| 814 |
$error.html( 'File type not allowed.' ); |
| 815 |
$input.val( '' ); |
| 816 |
return; |
| 817 |
} |
| 818 |
|
| 819 |
const $wrapper = $input.closest( '.wcf-file-input-wrapper' ); |
| 820 |
// Show uploading state. |
| 821 |
$wrapper.addClass( 'wcf-file-uploading' ); |
| 822 |
|
| 823 |
const formData = new FormData(); |
| 824 |
formData.append( 'wcf_checkout_file', file ); |
| 825 |
formData.append( 'action', 'wcf_upload_checkout_file' ); |
| 826 |
formData.append( 'security', cartflows.wcf_file_upload_nonce ); |
| 827 |
formData.append( 'field_key', fieldKey ); |
| 828 |
formData.append( |
| 829 |
'checkout_id', |
| 830 |
$( '._wcf_checkout_id' ).val() || '' |
| 831 |
); |
| 832 |
|
| 833 |
$.ajax( { |
| 834 |
url: cartflows.ajax_url, |
| 835 |
type: 'POST', |
| 836 |
data: formData, |
| 837 |
contentType: false, |
| 838 |
processData: false, |
| 839 |
success( response ) { |
| 840 |
$wrapper.removeClass( 'wcf-file-uploading' ); |
| 841 |
|
| 842 |
if ( response.success && response.data ) { |
| 843 |
$hiddenField.val( response.data.url ); |
| 844 |
} else { |
| 845 |
const errorMsg = |
| 846 |
response.data && response.data.error |
| 847 |
? response.data.error |
| 848 |
: 'Upload failed'; |
| 849 |
$error.html( errorMsg ); |
| 850 |
$hiddenField.val( '' ); |
| 851 |
$input.val( '' ); |
| 852 |
} |
| 853 |
}, |
| 854 |
error() { |
| 855 |
$wrapper.removeClass( 'wcf-file-uploading' ); |
| 856 |
$error.html( 'Upload failed. Please try again.' ); |
| 857 |
$hiddenField.val( '' ); |
| 858 |
$input.val( '' ); |
| 859 |
}, |
| 860 |
} ); |
| 861 |
}, |
| 862 |
}; |
| 863 |
|
| 864 |
$( function () { |
| 865 |
wcf_persistent_data(); |
| 866 |
|
| 867 |
wcf_update_checkout_on_return(); |
| 868 |
|
| 869 |
wcf_custom_field_validation(); |
| 870 |
|
| 871 |
wcf_remove_cart_products(); |
| 872 |
|
| 873 |
wcf_checkout_coupons.init(); |
| 874 |
|
| 875 |
wcf_toggle_optimized_fields(); |
| 876 |
wcf_instant_checkout_coupon_field(); |
| 877 |
|
| 878 |
$( document.body ).on( |
| 879 |
'checkout_error updated_checkout', |
| 880 |
wcf_instant_checkout_relocate_notices |
| 881 |
); |
| 882 |
|
| 883 |
wcf_anim_field_style_two(); |
| 884 |
|
| 885 |
if ( cartflows.has_file_field ) { |
| 886 |
wcf_file_upload.init(); |
| 887 |
} |
| 888 |
|
| 889 |
// On email input field change. |
| 890 |
$( '.wcf-customer-info #billing_email' ).on( 'input', function () { |
| 891 |
inline_email_address_validation(); |
| 892 |
} ); |
| 893 |
|
| 894 |
// On page load as we saves the checkout fields values. |
| 895 |
if ( $( '.wcf-customer-info #billing_email' ).length > 0 ) { |
| 896 |
inline_email_address_validation(); |
| 897 |
} |
| 898 |
|
| 899 |
woocommerce_user_login(); |
| 900 |
} ); |
| 901 |
} )( jQuery ); |
| 902 |
|