| 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 the URL carries checkout pre-fill params (billing_* / shipping_*), |
| 269 |
// let the PHP-rendered values stand — do not overwrite with stale |
| 270 |
// localStorage data from a previous visit. |
| 271 |
const urlSearch = new URLSearchParams( window.location.search ); |
| 272 |
const hasPrefillParams = Array.from( urlSearch.keys() ).some( |
| 273 |
( k ) => |
| 274 |
k.startsWith( 'billing_' ) || |
| 275 |
k.startsWith( 'shipping_' ) |
| 276 |
); |
| 277 |
if ( hasPrefillParams ) { |
| 278 |
return; |
| 279 |
} |
| 280 |
|
| 281 |
if ( |
| 282 |
localStorage.getItem( 'cartflows_checkout_form' ) !== null |
| 283 |
) { |
| 284 |
checkout_data = JSON.parse( |
| 285 |
localStorage.getItem( 'cartflows_checkout_form' ) |
| 286 |
); |
| 287 |
|
| 288 |
for ( let i = 0; i < checkout_data.length; i++ ) { |
| 289 |
if ( |
| 290 |
$( |
| 291 |
'form.woocommerce-checkout [name=' + |
| 292 |
checkout_data[ i ].name + |
| 293 |
']' |
| 294 |
).hasClass( 'select2-hidden-accessible' ) |
| 295 |
) { |
| 296 |
$( |
| 297 |
'form.woocommerce-checkout [name=' + |
| 298 |
checkout_data[ i ].name + |
| 299 |
']' |
| 300 |
).selectWoo( 'val', [ checkout_data[ i ].value ] ); |
| 301 |
} else { |
| 302 |
$( |
| 303 |
'form.woocommerce-checkout [name=' + |
| 304 |
checkout_data[ i ].name + |
| 305 |
']' |
| 306 |
).val( checkout_data[ i ].value ); |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
}, |
| 311 |
}; |
| 312 |
|
| 313 |
const fixDutchAddressOrder = function () { |
| 314 |
const billing_country = $( '#billing_country' ).val(); |
| 315 |
const billing_address = $( '#billing_address_1' ).val(); |
| 316 |
|
| 317 |
if ( billing_country === 'NL' && billing_address ) { |
| 318 |
const match_billing = |
| 319 |
billing_address.match( /^(\d+\S*)\s+(.+)$/ ); |
| 320 |
if ( match_billing ) { |
| 321 |
$( '#billing_address_1' ).val( |
| 322 |
match_billing[ 2 ] + ' ' + match_billing[ 1 ] |
| 323 |
); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
const shipping_country = $( '#shipping_country' ).val(); |
| 328 |
const shipping_address = $( '#shipping_address_1' ).val(); |
| 329 |
|
| 330 |
if ( shipping_country === 'NL' && shipping_address ) { |
| 331 |
const match_shipping = |
| 332 |
shipping_address.match( /^(\d+\S*)\s+(.+)$/ ); |
| 333 |
if ( match_shipping ) { |
| 334 |
$( '#shipping_address_1' ).val( |
| 335 |
match_shipping[ 2 ] + ' ' + match_shipping[ 1 ] |
| 336 |
); |
| 337 |
} |
| 338 |
} |
| 339 |
}; |
| 340 |
|
| 341 |
wcf_form_data.get(); |
| 342 |
|
| 343 |
$( |
| 344 |
checkout_cust_form + ' input, ' + checkout_cust_form + ' select' |
| 345 |
).on( 'change', function () { |
| 346 |
wcf_form_data.set(); |
| 347 |
} ); |
| 348 |
}; |
| 349 |
|
| 350 |
const wcf_checkout_coupons = { |
| 351 |
init() { |
| 352 |
$( document.body ).on( |
| 353 |
'click', |
| 354 |
'.wcf-submit-coupon', |
| 355 |
this.submit_coupon |
| 356 |
); |
| 357 |
$( document.body ).on( |
| 358 |
'click', |
| 359 |
'.wcf-remove-coupon', |
| 360 |
this.remove_coupon |
| 361 |
); |
| 362 |
}, |
| 363 |
|
| 364 |
submit_coupon( e ) { |
| 365 |
e.preventDefault(); |
| 366 |
const coupon_wrapper_class = $( '.wcf-custom-coupon-field' ), |
| 367 |
coupon_wrapper = $( this ).closest( coupon_wrapper_class ), |
| 368 |
coupon_field = coupon_wrapper.find( '.wcf-coupon-code-input' ), |
| 369 |
coupon_value = coupon_field.val(); |
| 370 |
|
| 371 |
if ( '' === coupon_value ) { |
| 372 |
coupon_field.addClass( 'field-required' ); |
| 373 |
return false; |
| 374 |
} |
| 375 |
coupon_field.removeClass( 'field-required' ); |
| 376 |
|
| 377 |
const billing_email = $( |
| 378 |
'.wcf-embed-checkout-form #billing_email' |
| 379 |
).val(); |
| 380 |
|
| 381 |
const data = { |
| 382 |
coupon_code: coupon_value, |
| 383 |
action: 'wcf_woo_apply_coupon', |
| 384 |
security: cartflows.wcf_validate_coupon_nonce, |
| 385 |
billing_email, |
| 386 |
}; |
| 387 |
|
| 388 |
$.ajax( { |
| 389 |
type: 'POST', |
| 390 |
url: cartflows.ajax_url, |
| 391 |
data, |
| 392 |
|
| 393 |
success( code_data ) { |
| 394 |
const coupon_message = $( '.wcf-custom-coupon-field' ); |
| 395 |
coupon_message |
| 396 |
.find( '.woocommerce-error, .woocommerce-message' ) |
| 397 |
.remove(); |
| 398 |
|
| 399 |
if ( code_data && code_data.status === true ) { |
| 400 |
$( document.body ).trigger( 'update_checkout', { |
| 401 |
update_shipping_method: false, |
| 402 |
} ); |
| 403 |
coupon_message.prepend( code_data.msg ); |
| 404 |
coupon_wrapper_class.addClass( 'wcf-coupon-applied' ); |
| 405 |
} else if ( code_data && code_data.msg ) { |
| 406 |
coupon_message.prepend( code_data.msg ); |
| 407 |
coupon_wrapper_class.removeClass( |
| 408 |
'wcf-coupon-applied' |
| 409 |
); |
| 410 |
} else { |
| 411 |
console.log( |
| 412 |
'Error: Error while applying the coupon. Response: ' + |
| 413 |
code_data.data && code_data.data.error |
| 414 |
? code_data.data.error |
| 415 |
: code_data |
| 416 |
); |
| 417 |
} |
| 418 |
}, |
| 419 |
} ); |
| 420 |
}, |
| 421 |
|
| 422 |
remove_coupon( e ) { |
| 423 |
e.preventDefault(); |
| 424 |
const data = { |
| 425 |
coupon_code: $( this ).attr( 'data-coupon' ), |
| 426 |
action: 'wcf_woo_remove_coupon', |
| 427 |
security: cartflows.wcf_validate_remove_coupon_nonce, |
| 428 |
}; |
| 429 |
|
| 430 |
$.ajax( { |
| 431 |
type: 'POST', |
| 432 |
url: cartflows.ajax_url, |
| 433 |
data, |
| 434 |
|
| 435 |
success( code ) { |
| 436 |
const coupon_message = $( '.wcf-custom-coupon-field' ); |
| 437 |
coupon_message |
| 438 |
.find( '.woocommerce-error, .woocommerce-message' ) |
| 439 |
.hide(); |
| 440 |
|
| 441 |
$( '.wcf-custom-coupon-field' ).removeClass( |
| 442 |
'wcf-coupon-applied' |
| 443 |
); |
| 444 |
|
| 445 |
if ( code ) { |
| 446 |
$( document.body ).trigger( 'update_checkout', { |
| 447 |
update_shipping_method: false, |
| 448 |
} ); |
| 449 |
coupon_message.prepend( code ); |
| 450 |
} |
| 451 |
}, |
| 452 |
} ); |
| 453 |
}, |
| 454 |
}; |
| 455 |
|
| 456 |
const wcf_remove_cart_products = function () { |
| 457 |
$( document.body ).on( |
| 458 |
'click', |
| 459 |
'#wcf-embed-checkout-form .wcf-remove-product', |
| 460 |
function ( e ) { |
| 461 |
e.preventDefault(); |
| 462 |
const p_id = $( this ).attr( 'data-id' ); |
| 463 |
const data = { |
| 464 |
p_key: $( this ).attr( 'data-item-key' ), |
| 465 |
p_id, |
| 466 |
action: 'wcf_woo_remove_cart_product', |
| 467 |
security: cartflows.wcf_validate_remove_cart_product_nonce, |
| 468 |
}; |
| 469 |
|
| 470 |
$.ajax( { |
| 471 |
type: 'POST', |
| 472 |
url: cartflows.ajax_url, |
| 473 |
data, |
| 474 |
|
| 475 |
success( response ) { |
| 476 |
const res_data = JSON.parse( response ); |
| 477 |
|
| 478 |
if ( res_data.need_shipping === false ) { |
| 479 |
// $('#wcf-embed-checkout-form').find('#ship-to-different-address-checkbox').hide(); |
| 480 |
$( '#wcf-embed-checkout-form' ) |
| 481 |
.find( '#ship-to-different-address-checkbox' ) |
| 482 |
.attr( 'checked', false ); |
| 483 |
} |
| 484 |
|
| 485 |
// Handle order bump checkbox state when product is removed. |
| 486 |
if ( |
| 487 |
typeof res_data.is_order_bump !== 'undefined' && |
| 488 |
res_data.is_order_bump && |
| 489 |
res_data.order_bump_id |
| 490 |
) { |
| 491 |
$( |
| 492 |
'input[name="wcf-bump-order-cb-' + |
| 493 |
res_data.order_bump_id + |
| 494 |
'"]' |
| 495 |
).prop( 'checked', false ); |
| 496 |
} |
| 497 |
|
| 498 |
$( '#wcf-embed-checkout-form' ) |
| 499 |
.find( '.woocommerce-notices-wrapper' ) |
| 500 |
.first() |
| 501 |
.html( res_data.msg ); |
| 502 |
$( document ).trigger( 'cartflows_remove_product', [ |
| 503 |
p_id, |
| 504 |
] ); |
| 505 |
$( '#wcf-embed-checkout-form' ).trigger( |
| 506 |
'update_checkout' |
| 507 |
); |
| 508 |
}, |
| 509 |
} ); |
| 510 |
} |
| 511 |
); |
| 512 |
}; |
| 513 |
|
| 514 |
const wcf_toggle_optimized_fields = function () { |
| 515 |
jQuery.each( |
| 516 |
cartflows_checkout_optimized_fields, |
| 517 |
function ( field, cartflows_optimized_field ) { |
| 518 |
if ( cartflows_optimized_field.is_optimized ) { |
| 519 |
jQuery( '#' + field ).prepend( |
| 520 |
'<a href="#" id="wcf_optimized_' + |
| 521 |
field + |
| 522 |
'">' + |
| 523 |
cartflows_optimized_field.field_label + |
| 524 |
'</a>' |
| 525 |
); |
| 526 |
jQuery( '#wcf_optimized_' + field ).on( |
| 527 |
'click', |
| 528 |
function ( e ) { |
| 529 |
e.preventDefault(); |
| 530 |
jQuery( '#' + field ).removeClass( |
| 531 |
'wcf-hide-field' |
| 532 |
); |
| 533 |
// jQuery("#" + field).removeClass('mt20') |
| 534 |
const field_id = field.replace( /_field/g, '' ); |
| 535 |
$( '#' + field_id ).trigger( 'focus' ); |
| 536 |
jQuery( this ).remove(); |
| 537 |
} |
| 538 |
); |
| 539 |
} |
| 540 |
} |
| 541 |
); |
| 542 |
}; |
| 543 |
|
| 544 |
const wcf_instant_checkout_coupon_field = function () { |
| 545 |
$( document.body ).on( |
| 546 |
'click', |
| 547 |
'.coupon-field .wcf-custom-coupon-field .wcf-optimized-coupon-field', |
| 548 |
function ( e ) { |
| 549 |
e.preventDefault(); |
| 550 |
|
| 551 |
$( '.wcf-custom-coupon-field' ).removeClass( 'wcf-hide-field' ); |
| 552 |
$( this ).remove(); |
| 553 |
} |
| 554 |
); |
| 555 |
}; |
| 556 |
|
| 557 |
const wcf_anim_field_style_two = function () { |
| 558 |
const $inputs = $( |
| 559 |
'.wcf-field-modern-label .woocommerce input, .wcf-field-modern-label .woocommerce select, .wcf-field-modern-label .woocommerce textarea' |
| 560 |
); |
| 561 |
|
| 562 |
/** |
| 563 |
* Add wcf-anim-label class when floating label option is enabled. |
| 564 |
* The class will be added only when the info is started to type in the checkout fields. |
| 565 |
* |
| 566 |
* @param {Object} $this the object of current checkout field |
| 567 |
*/ |
| 568 |
const _add_anim_class = function ( $this ) { |
| 569 |
const field_row = $this.closest( '.form-row' ), |
| 570 |
is_select = |
| 571 |
$this.is( 'select' ) || |
| 572 |
$this.hasClass( 'select2-hidden-accessible' ), |
| 573 |
field_value = is_select |
| 574 |
? $this.find( ':selected' ).text() |
| 575 |
: $this.val(), |
| 576 |
field_type = $this.attr( 'type' ); |
| 577 |
|
| 578 |
if ( '' === field_value ) { |
| 579 |
field_row.removeClass( 'wcf-anim-label' ); |
| 580 |
} else if ( 'hidden' === field_type ) { |
| 581 |
field_row.addClass( 'wcf-anim-hidden-label' ); |
| 582 |
} else { |
| 583 |
field_row.addClass( 'wcf-anim-label' ); |
| 584 |
} |
| 585 |
}; |
| 586 |
|
| 587 |
// Trigger the addition of anim class when focused or info is inputed on the field. |
| 588 |
$inputs.on( 'focusout input', function () { |
| 589 |
const $this = $( this ); |
| 590 |
_add_anim_class( $this ); |
| 591 |
} ); |
| 592 |
|
| 593 |
// Load the anim class on the page ready. |
| 594 |
$( $inputs ).each( function () { |
| 595 |
_add_anim_class( $( this ) ); |
| 596 |
} ); |
| 597 |
}; |
| 598 |
|
| 599 |
const validateEmail = function ( email ) { |
| 600 |
const email_reg = |
| 601 |
/^(([^<>()[\]\\.,;:\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,}))$/; |
| 602 |
return email_reg.test( email ); |
| 603 |
}; |
| 604 |
|
| 605 |
let xhrCountEmail = 0; |
| 606 |
let delayTimerEmail; |
| 607 |
|
| 608 |
const inline_email_address_validation = function () { |
| 609 |
const email = $( '.wcf-customer-info #billing_email' ).val(); |
| 610 |
|
| 611 |
if ( 'undefined' === typeof email || cartflows.is_logged_in ) { |
| 612 |
return; |
| 613 |
} |
| 614 |
|
| 615 |
if ( '' !== email ) { |
| 616 |
const email_field = $( '#billing_email' ); |
| 617 |
const validation_msg_wrap = $( '.wcf-email-validation-block' ), |
| 618 |
customer_login_wrap = $( '.wcf-customer-login-section' ); |
| 619 |
|
| 620 |
validation_msg_wrap.remove(); |
| 621 |
|
| 622 |
if ( ! validateEmail( email ) ) { |
| 623 |
email_field.after( |
| 624 |
'<span class="wcf-email-validation-block error">' + |
| 625 |
cartflows.email_validation_msgs.error_msg + |
| 626 |
'</span>' |
| 627 |
); |
| 628 |
customer_login_wrap.removeClass( 'wcf-show' ); |
| 629 |
return false; |
| 630 |
} |
| 631 |
|
| 632 |
clearTimeout( delayTimerEmail ); |
| 633 |
|
| 634 |
const seqNumber = ++xhrCountEmail; |
| 635 |
|
| 636 |
delayTimerEmail = setTimeout( function () { |
| 637 |
$.ajax( { |
| 638 |
url: cartflows.ajax_url, |
| 639 |
type: 'POST', |
| 640 |
data: { |
| 641 |
action: 'wcf_check_email_exists', |
| 642 |
email_address: email, |
| 643 |
security: cartflows.check_email_exist_nonce, |
| 644 |
}, |
| 645 |
success( resp ) { |
| 646 |
if ( seqNumber !== xhrCountEmail ) { |
| 647 |
return; |
| 648 |
} |
| 649 |
|
| 650 |
validation_msg_wrap.remove(); |
| 651 |
|
| 652 |
if ( |
| 653 |
resp.data && |
| 654 |
resp.data.success && |
| 655 |
customer_login_wrap.hasClass( 'wcf-show' ) |
| 656 |
) { |
| 657 |
email_field.after( |
| 658 |
'<span class="wcf-email-validation-block success">' + |
| 659 |
cartflows.email_validation_msgs |
| 660 |
.success_msg + |
| 661 |
'</span>' |
| 662 |
); |
| 663 |
return; |
| 664 |
} |
| 665 |
|
| 666 |
if ( resp.data && resp.data.success ) { |
| 667 |
if ( resp.data.is_login_allowed ) { |
| 668 |
email_field.after( |
| 669 |
'<span class="wcf-email-validation-block success">' + |
| 670 |
cartflows.email_validation_msgs |
| 671 |
.success_msg + |
| 672 |
'</span>' |
| 673 |
); |
| 674 |
|
| 675 |
customer_login_wrap |
| 676 |
.slideDown( 400 ) |
| 677 |
.addClass( 'wcf-show' ); |
| 678 |
} |
| 679 |
|
| 680 |
$( '.wcf-create-account-section' ).hide(); |
| 681 |
$( |
| 682 |
'.woocommerce-billing-fields__customer-login-label' |
| 683 |
).show(); |
| 684 |
} else { |
| 685 |
customer_login_wrap |
| 686 |
.slideUp( 400 ) |
| 687 |
.removeClass( 'wcf-show' ); |
| 688 |
|
| 689 |
//Learndash Woo integration plugin hides the create aacount checkbox.So need to show it again. |
| 690 |
$( |
| 691 |
'.wcf-create-account-section .create-account label.checkbox' |
| 692 |
).show(); |
| 693 |
|
| 694 |
$( '.wcf-create-account-section' ).show(); |
| 695 |
|
| 696 |
$( |
| 697 |
'.woocommerce-billing-fields__customer-login-label' |
| 698 |
).hide(); |
| 699 |
} |
| 700 |
}, |
| 701 |
} ); |
| 702 |
}, 300 ); |
| 703 |
} else { |
| 704 |
$( '.wcf-create-account-section' ).hide(); |
| 705 |
$( '.wcf-customer-login-section' ).hide(); |
| 706 |
$( '.wcf-email-validation-block' ).hide(); |
| 707 |
$( '.woocommerce-billing-fields__customer-login-label' ).show(); |
| 708 |
} |
| 709 |
|
| 710 |
return false; |
| 711 |
}; |
| 712 |
|
| 713 |
const woocommerce_user_login = function () { |
| 714 |
$( '.wcf-customer-login-url' ).on( 'click', function login_form( e ) { |
| 715 |
e.preventDefault(); |
| 716 |
const customer_login_wrap = $( '.wcf-customer-login-section' ); |
| 717 |
|
| 718 |
if ( customer_login_wrap.hasClass( 'wcf-show' ) ) { |
| 719 |
customer_login_wrap.slideUp( 400 ); |
| 720 |
customer_login_wrap.removeClass( 'wcf-show' ); |
| 721 |
} else { |
| 722 |
customer_login_wrap.slideDown( 400 ); |
| 723 |
customer_login_wrap.addClass( 'wcf-show' ); |
| 724 |
} |
| 725 |
} ); |
| 726 |
|
| 727 |
$( '.wcf-customer-login-section__login-button' ).on( |
| 728 |
'click', |
| 729 |
function name() { |
| 730 |
const email = $( '#billing_email' ).val(); |
| 731 |
const password = $( '#billing_password' ).val(); |
| 732 |
|
| 733 |
$.ajax( { |
| 734 |
url: cartflows.ajax_url, |
| 735 |
type: 'POST', |
| 736 |
data: { |
| 737 |
action: 'wcf_woocommerce_login', |
| 738 |
email, |
| 739 |
password, |
| 740 |
security: cartflows.woocommerce_login_nonce, |
| 741 |
}, |
| 742 |
success( resp ) { |
| 743 |
if ( resp.data && resp.data.success ) { |
| 744 |
location.reload(); |
| 745 |
} else { |
| 746 |
$( '.wcf-customer-info__notice' ) |
| 747 |
.addClass( 'wcf-notice' ) |
| 748 |
.html( resp.data.error ); |
| 749 |
} |
| 750 |
}, |
| 751 |
} ); |
| 752 |
} |
| 753 |
); |
| 754 |
}; |
| 755 |
|
| 756 |
const wcf_instant_checkout_relocate_notices = function () { |
| 757 |
if ( $( 'body' ).hasClass( 'cartflows-instant-checkout' ) ) { |
| 758 |
$( '.woocommerce-NoticeGroup' ).prependTo( |
| 759 |
'.woocommerce-notices-wrapper' |
| 760 |
); |
| 761 |
} |
| 762 |
}; |
| 763 |
|
| 764 |
const wcf_file_upload = { |
| 765 |
init() { |
| 766 |
$( document.body ).on( |
| 767 |
'change', |
| 768 |
'.wcf-checkout-file-input', |
| 769 |
this.handle_file_change |
| 770 |
); |
| 771 |
}, |
| 772 |
|
| 773 |
handle_file_change() { |
| 774 |
const $input = $( this ), |
| 775 |
fieldKey = $input.data( 'field-key' ), |
| 776 |
$hiddenField = $( '#' + fieldKey ), |
| 777 |
$error = $( '#' + fieldKey + '_error' ), |
| 778 |
maxSize = parseInt( $input.data( 'max-size' ) ) || 5; |
| 779 |
|
| 780 |
// Clear previous errors. |
| 781 |
$error.empty(); |
| 782 |
|
| 783 |
if ( ! this.files.length ) { |
| 784 |
$hiddenField.val( '' ); |
| 785 |
return; |
| 786 |
} |
| 787 |
|
| 788 |
const file = this.files[ 0 ]; |
| 789 |
|
| 790 |
// Client-side file size validation. |
| 791 |
if ( file.size > maxSize * 1024 * 1024 ) { |
| 792 |
$error.html( 'File size exceeds ' + maxSize + 'MB limit.' ); |
| 793 |
$input.val( '' ); |
| 794 |
return; |
| 795 |
} |
| 796 |
|
| 797 |
// Client-side file type validation. |
| 798 |
const ext = file.name.split( '.' ).pop().toLowerCase(); |
| 799 |
|
| 800 |
let allowed = []; |
| 801 |
|
| 802 |
const allowedTypes = ( |
| 803 |
$input.data( 'allowed-types' ) || '' |
| 804 |
).toString(); |
| 805 |
if ( allowedTypes ) { |
| 806 |
allowed = allowedTypes.split( ',' ).map( function ( t ) { |
| 807 |
return t.trim().toLowerCase(); |
| 808 |
} ); |
| 809 |
} else { |
| 810 |
// Fallback to master allowlist. |
| 811 |
allowed = [ |
| 812 |
'jpg', |
| 813 |
'jpeg', |
| 814 |
'png', |
| 815 |
'webp', |
| 816 |
'gif', |
| 817 |
'pdf', |
| 818 |
'mp3', |
| 819 |
'm4a', |
| 820 |
'wav', |
| 821 |
'mp4', |
| 822 |
'mov', |
| 823 |
]; |
| 824 |
} |
| 825 |
|
| 826 |
if ( allowed.length && ! allowed.includes( ext ) ) { |
| 827 |
$error.html( 'File type not allowed.' ); |
| 828 |
$input.val( '' ); |
| 829 |
return; |
| 830 |
} |
| 831 |
|
| 832 |
const $wrapper = $input.closest( '.wcf-file-input-wrapper' ); |
| 833 |
// Show uploading state. |
| 834 |
$wrapper.addClass( 'wcf-file-uploading' ); |
| 835 |
|
| 836 |
const formData = new FormData(); |
| 837 |
formData.append( 'wcf_checkout_file', file ); |
| 838 |
formData.append( 'action', 'wcf_upload_checkout_file' ); |
| 839 |
formData.append( 'security', cartflows.wcf_file_upload_nonce ); |
| 840 |
formData.append( 'field_key', fieldKey ); |
| 841 |
formData.append( |
| 842 |
'checkout_id', |
| 843 |
$( '._wcf_checkout_id' ).val() || '' |
| 844 |
); |
| 845 |
|
| 846 |
$.ajax( { |
| 847 |
url: cartflows.ajax_url, |
| 848 |
type: 'POST', |
| 849 |
data: formData, |
| 850 |
contentType: false, |
| 851 |
processData: false, |
| 852 |
success( response ) { |
| 853 |
$wrapper.removeClass( 'wcf-file-uploading' ); |
| 854 |
|
| 855 |
if ( response.success && response.data ) { |
| 856 |
$hiddenField.val( response.data.url ); |
| 857 |
} else { |
| 858 |
const errorMsg = |
| 859 |
response.data && response.data.error |
| 860 |
? response.data.error |
| 861 |
: 'Upload failed'; |
| 862 |
$error.html( errorMsg ); |
| 863 |
$hiddenField.val( '' ); |
| 864 |
$input.val( '' ); |
| 865 |
} |
| 866 |
}, |
| 867 |
error() { |
| 868 |
$wrapper.removeClass( 'wcf-file-uploading' ); |
| 869 |
$error.html( 'Upload failed. Please try again.' ); |
| 870 |
$hiddenField.val( '' ); |
| 871 |
$input.val( '' ); |
| 872 |
}, |
| 873 |
} ); |
| 874 |
}, |
| 875 |
}; |
| 876 |
|
| 877 |
$( function () { |
| 878 |
wcf_persistent_data(); |
| 879 |
|
| 880 |
wcf_update_checkout_on_return(); |
| 881 |
|
| 882 |
wcf_custom_field_validation(); |
| 883 |
|
| 884 |
wcf_remove_cart_products(); |
| 885 |
|
| 886 |
wcf_checkout_coupons.init(); |
| 887 |
|
| 888 |
wcf_toggle_optimized_fields(); |
| 889 |
wcf_instant_checkout_coupon_field(); |
| 890 |
|
| 891 |
$( document.body ).on( |
| 892 |
'checkout_error updated_checkout', |
| 893 |
wcf_instant_checkout_relocate_notices |
| 894 |
); |
| 895 |
|
| 896 |
wcf_anim_field_style_two(); |
| 897 |
|
| 898 |
if ( cartflows.has_file_field ) { |
| 899 |
wcf_file_upload.init(); |
| 900 |
} |
| 901 |
|
| 902 |
// On email input field change. |
| 903 |
$( '.wcf-customer-info #billing_email' ).on( 'input', function () { |
| 904 |
inline_email_address_validation(); |
| 905 |
} ); |
| 906 |
|
| 907 |
// On page load as we saves the checkout fields values. |
| 908 |
if ( $( '.wcf-customer-info #billing_email' ).length > 0 ) { |
| 909 |
inline_email_address_validation(); |
| 910 |
} |
| 911 |
|
| 912 |
woocommerce_user_login(); |
| 913 |
} ); |
| 914 |
} )( jQuery ); |
| 915 |
|