add-payment-method.js
5 years ago
add-payment-method.min.js
5 years ago
add-to-cart-variation.js
5 years ago
add-to-cart-variation.min.js
5 years ago
add-to-cart.js
5 years ago
add-to-cart.min.js
5 years ago
address-i18n.js
5 years ago
address-i18n.min.js
5 years ago
cart-fragments.js
5 years ago
cart-fragments.min.js
5 years ago
cart.js
5 years ago
cart.min.js
5 years ago
checkout.js
5 years ago
checkout.min.js
5 years ago
country-select.js
5 years ago
country-select.min.js
5 years ago
credit-card-form.js
8 years ago
credit-card-form.min.js
8 years ago
geolocation.js
5 years ago
geolocation.min.js
5 years ago
lost-password.js
8 years ago
lost-password.min.js
8 years ago
password-strength-meter.js
5 years ago
password-strength-meter.min.js
5 years ago
price-slider.js
5 years ago
price-slider.min.js
5 years ago
single-product.js
5 years ago
single-product.min.js
5 years ago
tokenization-form.js
6 years ago
tokenization-form.min.js
5 years ago
woocommerce.js
5 years ago
woocommerce.min.js
5 years ago
checkout.js
728 lines
| 1 | /* global wc_checkout_params */ |
| 2 | jQuery( function( $ ) { |
| 3 | |
| 4 | // wc_checkout_params is required to continue, ensure the object exists |
| 5 | if ( typeof wc_checkout_params === 'undefined' ) { |
| 6 | return false; |
| 7 | } |
| 8 | |
| 9 | $.blockUI.defaults.overlayCSS.cursor = 'default'; |
| 10 | |
| 11 | var wc_checkout_form = { |
| 12 | updateTimer: false, |
| 13 | dirtyInput: false, |
| 14 | selectedPaymentMethod: false, |
| 15 | xhr: false, |
| 16 | $order_review: $( '#order_review' ), |
| 17 | $checkout_form: $( 'form.checkout' ), |
| 18 | init: function() { |
| 19 | $( document.body ).on( 'update_checkout', this.update_checkout ); |
| 20 | $( document.body ).on( 'init_checkout', this.init_checkout ); |
| 21 | |
| 22 | // Payment methods |
| 23 | this.$checkout_form.on( 'click', 'input[name="payment_method"]', this.payment_method_selected ); |
| 24 | |
| 25 | if ( $( document.body ).hasClass( 'woocommerce-order-pay' ) ) { |
| 26 | this.$order_review.on( 'click', 'input[name="payment_method"]', this.payment_method_selected ); |
| 27 | this.$order_review.on( 'submit', this.submitOrder ); |
| 28 | this.$order_review.attr( 'novalidate', 'novalidate' ); |
| 29 | } |
| 30 | |
| 31 | // Prevent HTML5 validation which can conflict. |
| 32 | this.$checkout_form.attr( 'novalidate', 'novalidate' ); |
| 33 | |
| 34 | // Form submission |
| 35 | this.$checkout_form.on( 'submit', this.submit ); |
| 36 | |
| 37 | // Inline validation |
| 38 | this.$checkout_form.on( 'input validate change', '.input-text, select, input:checkbox', this.validate_field ); |
| 39 | |
| 40 | // Manual trigger |
| 41 | this.$checkout_form.on( 'update', this.trigger_update_checkout ); |
| 42 | |
| 43 | // Inputs/selects which update totals |
| 44 | this.$checkout_form.on( 'change', 'select.shipping_method, input[name^="shipping_method"], #ship-to-different-address input, .update_totals_on_change select, .update_totals_on_change input[type="radio"], .update_totals_on_change input[type="checkbox"]', this.trigger_update_checkout ); // eslint-disable-line max-len |
| 45 | this.$checkout_form.on( 'change', '.address-field select', this.input_changed ); |
| 46 | this.$checkout_form.on( 'change', '.address-field input.input-text, .update_totals_on_change input.input-text', this.maybe_input_changed ); // eslint-disable-line max-len |
| 47 | this.$checkout_form.on( 'keydown', '.address-field input.input-text, .update_totals_on_change input.input-text', this.queue_update_checkout ); // eslint-disable-line max-len |
| 48 | |
| 49 | // Address fields |
| 50 | this.$checkout_form.on( 'change', '#ship-to-different-address input', this.ship_to_different_address ); |
| 51 | |
| 52 | // Trigger events |
| 53 | this.$checkout_form.find( '#ship-to-different-address input' ).trigger( 'change' ); |
| 54 | this.init_payment_methods(); |
| 55 | |
| 56 | // Update on page load |
| 57 | if ( wc_checkout_params.is_checkout === '1' ) { |
| 58 | $( document.body ).trigger( 'init_checkout' ); |
| 59 | } |
| 60 | if ( wc_checkout_params.option_guest_checkout === 'yes' ) { |
| 61 | $( 'input#createaccount' ).change( this.toggle_create_account ).trigger( 'change' ); |
| 62 | } |
| 63 | }, |
| 64 | init_payment_methods: function() { |
| 65 | var $payment_methods = $( '.woocommerce-checkout' ).find( 'input[name="payment_method"]' ); |
| 66 | |
| 67 | // If there is one method, we can hide the radio input |
| 68 | if ( 1 === $payment_methods.length ) { |
| 69 | $payment_methods.eq(0).hide(); |
| 70 | } |
| 71 | |
| 72 | // If there was a previously selected method, check that one. |
| 73 | if ( wc_checkout_form.selectedPaymentMethod ) { |
| 74 | $( '#' + wc_checkout_form.selectedPaymentMethod ).prop( 'checked', true ); |
| 75 | } |
| 76 | |
| 77 | // If there are none selected, select the first. |
| 78 | if ( 0 === $payment_methods.filter( ':checked' ).length ) { |
| 79 | $payment_methods.eq(0).prop( 'checked', true ); |
| 80 | } |
| 81 | |
| 82 | // Get name of new selected method. |
| 83 | var checkedPaymentMethod = $payment_methods.filter( ':checked' ).eq(0).prop( 'id' ); |
| 84 | |
| 85 | if ( $payment_methods.length > 1 ) { |
| 86 | // Hide open descriptions. |
| 87 | $( 'div.payment_box:not(".' + checkedPaymentMethod + '")' ).filter( ':visible' ).slideUp( 0 ); |
| 88 | } |
| 89 | |
| 90 | // Trigger click event for selected method |
| 91 | $payment_methods.filter( ':checked' ).eq(0).trigger( 'click' ); |
| 92 | }, |
| 93 | get_payment_method: function() { |
| 94 | return wc_checkout_form.$checkout_form.find( 'input[name="payment_method"]:checked' ).val(); |
| 95 | }, |
| 96 | payment_method_selected: function( e ) { |
| 97 | e.stopPropagation(); |
| 98 | |
| 99 | if ( $( '.payment_methods input.input-radio' ).length > 1 ) { |
| 100 | var target_payment_box = $( 'div.payment_box.' + $( this ).attr( 'ID' ) ), |
| 101 | is_checked = $( this ).is( ':checked' ); |
| 102 | |
| 103 | if ( is_checked && ! target_payment_box.is( ':visible' ) ) { |
| 104 | $( 'div.payment_box' ).filter( ':visible' ).slideUp( 230 ); |
| 105 | |
| 106 | if ( is_checked ) { |
| 107 | target_payment_box.slideDown( 230 ); |
| 108 | } |
| 109 | } |
| 110 | } else { |
| 111 | $( 'div.payment_box' ).show(); |
| 112 | } |
| 113 | |
| 114 | if ( $( this ).data( 'order_button_text' ) ) { |
| 115 | $( '#place_order' ).text( $( this ).data( 'order_button_text' ) ); |
| 116 | } else { |
| 117 | $( '#place_order' ).text( $( '#place_order' ).data( 'value' ) ); |
| 118 | } |
| 119 | |
| 120 | var selectedPaymentMethod = $( '.woocommerce-checkout input[name="payment_method"]:checked' ).attr( 'id' ); |
| 121 | |
| 122 | if ( selectedPaymentMethod !== wc_checkout_form.selectedPaymentMethod ) { |
| 123 | $( document.body ).trigger( 'payment_method_selected' ); |
| 124 | } |
| 125 | |
| 126 | wc_checkout_form.selectedPaymentMethod = selectedPaymentMethod; |
| 127 | }, |
| 128 | toggle_create_account: function() { |
| 129 | $( 'div.create-account' ).hide(); |
| 130 | |
| 131 | if ( $( this ).is( ':checked' ) ) { |
| 132 | // Ensure password is not pre-populated. |
| 133 | $( '#account_password' ).val( '' ).trigger( 'change' ); |
| 134 | $( 'div.create-account' ).slideDown(); |
| 135 | } |
| 136 | }, |
| 137 | init_checkout: function() { |
| 138 | $( document.body ).trigger( 'update_checkout' ); |
| 139 | }, |
| 140 | maybe_input_changed: function( e ) { |
| 141 | if ( wc_checkout_form.dirtyInput ) { |
| 142 | wc_checkout_form.input_changed( e ); |
| 143 | } |
| 144 | }, |
| 145 | input_changed: function( e ) { |
| 146 | wc_checkout_form.dirtyInput = e.target; |
| 147 | wc_checkout_form.maybe_update_checkout(); |
| 148 | }, |
| 149 | queue_update_checkout: function( e ) { |
| 150 | var code = e.keyCode || e.which || 0; |
| 151 | |
| 152 | if ( code === 9 ) { |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | wc_checkout_form.dirtyInput = this; |
| 157 | wc_checkout_form.reset_update_checkout_timer(); |
| 158 | wc_checkout_form.updateTimer = setTimeout( wc_checkout_form.maybe_update_checkout, '1000' ); |
| 159 | }, |
| 160 | trigger_update_checkout: function() { |
| 161 | wc_checkout_form.reset_update_checkout_timer(); |
| 162 | wc_checkout_form.dirtyInput = false; |
| 163 | $( document.body ).trigger( 'update_checkout' ); |
| 164 | }, |
| 165 | maybe_update_checkout: function() { |
| 166 | var update_totals = true; |
| 167 | |
| 168 | if ( $( wc_checkout_form.dirtyInput ).length ) { |
| 169 | var $required_inputs = $( wc_checkout_form.dirtyInput ).closest( 'div' ).find( '.address-field.validate-required' ); |
| 170 | |
| 171 | if ( $required_inputs.length ) { |
| 172 | $required_inputs.each( function() { |
| 173 | if ( $( this ).find( 'input.input-text' ).val() === '' ) { |
| 174 | update_totals = false; |
| 175 | } |
| 176 | }); |
| 177 | } |
| 178 | } |
| 179 | if ( update_totals ) { |
| 180 | wc_checkout_form.trigger_update_checkout(); |
| 181 | } |
| 182 | }, |
| 183 | ship_to_different_address: function() { |
| 184 | $( 'div.shipping_address' ).hide(); |
| 185 | if ( $( this ).is( ':checked' ) ) { |
| 186 | $( 'div.shipping_address' ).slideDown(); |
| 187 | } |
| 188 | }, |
| 189 | reset_update_checkout_timer: function() { |
| 190 | clearTimeout( wc_checkout_form.updateTimer ); |
| 191 | }, |
| 192 | is_valid_json: function( raw_json ) { |
| 193 | try { |
| 194 | var json = JSON.parse( raw_json ); |
| 195 | |
| 196 | return ( json && 'object' === typeof json ); |
| 197 | } catch ( e ) { |
| 198 | return false; |
| 199 | } |
| 200 | }, |
| 201 | validate_field: function( e ) { |
| 202 | var $this = $( this ), |
| 203 | $parent = $this.closest( '.form-row' ), |
| 204 | validated = true, |
| 205 | validate_required = $parent.is( '.validate-required' ), |
| 206 | validate_email = $parent.is( '.validate-email' ), |
| 207 | validate_phone = $parent.is( '.validate-phone' ), |
| 208 | pattern = '', |
| 209 | event_type = e.type; |
| 210 | |
| 211 | if ( 'input' === event_type ) { |
| 212 | $parent.removeClass( 'woocommerce-invalid woocommerce-invalid-required-field woocommerce-invalid-email woocommerce-invalid-phone woocommerce-validated' ); // eslint-disable-line max-len |
| 213 | } |
| 214 | |
| 215 | if ( 'validate' === event_type || 'change' === event_type ) { |
| 216 | |
| 217 | if ( validate_required ) { |
| 218 | if ( 'checkbox' === $this.attr( 'type' ) && ! $this.is( ':checked' ) ) { |
| 219 | $parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-required-field' ); |
| 220 | validated = false; |
| 221 | } else if ( $this.val() === '' ) { |
| 222 | $parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-required-field' ); |
| 223 | validated = false; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | if ( validate_email ) { |
| 228 | if ( $this.val() ) { |
| 229 | /* https://stackoverflow.com/questions/2855865/jquery-validate-e-mail-address-regex */ |
| 230 | pattern = new RegExp( /^([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*(([ \t]*\r\n)?[ \t]+)?")@(([a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.)+([a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[0-9a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.?$/i ); // eslint-disable-line max-len |
| 231 | |
| 232 | if ( ! pattern.test( $this.val() ) ) { |
| 233 | $parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-email woocommerce-invalid-phone' ); // eslint-disable-line max-len |
| 234 | validated = false; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | if ( validate_phone ) { |
| 240 | pattern = new RegExp( /[\s\#0-9_\-\+\/\(\)\.]/g ); |
| 241 | |
| 242 | if ( 0 < $this.val().replace( pattern, '' ).length ) { |
| 243 | $parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-phone' ); |
| 244 | validated = false; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if ( validated ) { |
| 249 | $parent.removeClass( 'woocommerce-invalid woocommerce-invalid-required-field woocommerce-invalid-email woocommerce-invalid-phone' ).addClass( 'woocommerce-validated' ); // eslint-disable-line max-len |
| 250 | } |
| 251 | } |
| 252 | }, |
| 253 | update_checkout: function( event, args ) { |
| 254 | // Small timeout to prevent multiple requests when several fields update at the same time |
| 255 | wc_checkout_form.reset_update_checkout_timer(); |
| 256 | wc_checkout_form.updateTimer = setTimeout( wc_checkout_form.update_checkout_action, '5', args ); |
| 257 | }, |
| 258 | update_checkout_action: function( args ) { |
| 259 | if ( wc_checkout_form.xhr ) { |
| 260 | wc_checkout_form.xhr.abort(); |
| 261 | } |
| 262 | |
| 263 | if ( $( 'form.checkout' ).length === 0 ) { |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | args = typeof args !== 'undefined' ? args : { |
| 268 | update_shipping_method: true |
| 269 | }; |
| 270 | |
| 271 | var country = $( '#billing_country' ).val(), |
| 272 | state = $( '#billing_state' ).val(), |
| 273 | postcode = $( ':input#billing_postcode' ).val(), |
| 274 | city = $( '#billing_city' ).val(), |
| 275 | address = $( ':input#billing_address_1' ).val(), |
| 276 | address_2 = $( ':input#billing_address_2' ).val(), |
| 277 | s_country = country, |
| 278 | s_state = state, |
| 279 | s_postcode = postcode, |
| 280 | s_city = city, |
| 281 | s_address = address, |
| 282 | s_address_2 = address_2, |
| 283 | $required_inputs = $( wc_checkout_form.$checkout_form ).find( '.address-field.validate-required:visible' ), |
| 284 | has_full_address = true; |
| 285 | |
| 286 | if ( $required_inputs.length ) { |
| 287 | $required_inputs.each( function() { |
| 288 | if ( $( this ).find( ':input' ).val() === '' ) { |
| 289 | has_full_address = false; |
| 290 | } |
| 291 | }); |
| 292 | } |
| 293 | |
| 294 | if ( $( '#ship-to-different-address' ).find( 'input' ).is( ':checked' ) ) { |
| 295 | s_country = $( '#shipping_country' ).val(); |
| 296 | s_state = $( '#shipping_state' ).val(); |
| 297 | s_postcode = $( ':input#shipping_postcode' ).val(); |
| 298 | s_city = $( '#shipping_city' ).val(); |
| 299 | s_address = $( ':input#shipping_address_1' ).val(); |
| 300 | s_address_2 = $( ':input#shipping_address_2' ).val(); |
| 301 | } |
| 302 | |
| 303 | var data = { |
| 304 | security : wc_checkout_params.update_order_review_nonce, |
| 305 | payment_method : wc_checkout_form.get_payment_method(), |
| 306 | country : country, |
| 307 | state : state, |
| 308 | postcode : postcode, |
| 309 | city : city, |
| 310 | address : address, |
| 311 | address_2 : address_2, |
| 312 | s_country : s_country, |
| 313 | s_state : s_state, |
| 314 | s_postcode : s_postcode, |
| 315 | s_city : s_city, |
| 316 | s_address : s_address, |
| 317 | s_address_2 : s_address_2, |
| 318 | has_full_address: has_full_address, |
| 319 | post_data : $( 'form.checkout' ).serialize() |
| 320 | }; |
| 321 | |
| 322 | if ( false !== args.update_shipping_method ) { |
| 323 | var shipping_methods = {}; |
| 324 | |
| 325 | // eslint-disable-next-line max-len |
| 326 | $( 'select.shipping_method, input[name^="shipping_method"][type="radio"]:checked, input[name^="shipping_method"][type="hidden"]' ).each( function() { |
| 327 | shipping_methods[ $( this ).data( 'index' ) ] = $( this ).val(); |
| 328 | } ); |
| 329 | |
| 330 | data.shipping_method = shipping_methods; |
| 331 | } |
| 332 | |
| 333 | $( '.woocommerce-checkout-payment, .woocommerce-checkout-review-order-table' ).block({ |
| 334 | message: null, |
| 335 | overlayCSS: { |
| 336 | background: '#fff', |
| 337 | opacity: 0.6 |
| 338 | } |
| 339 | }); |
| 340 | |
| 341 | wc_checkout_form.xhr = $.ajax({ |
| 342 | type: 'POST', |
| 343 | url: wc_checkout_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'update_order_review' ), |
| 344 | data: data, |
| 345 | success: function( data ) { |
| 346 | |
| 347 | // Reload the page if requested |
| 348 | if ( data && true === data.reload ) { |
| 349 | window.location.reload(); |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | // Remove any notices added previously |
| 354 | $( '.woocommerce-NoticeGroup-updateOrderReview' ).remove(); |
| 355 | |
| 356 | var termsCheckBoxChecked = $( '#terms' ).prop( 'checked' ); |
| 357 | |
| 358 | // Save payment details to a temporary object |
| 359 | var paymentDetails = {}; |
| 360 | $( '.payment_box :input' ).each( function() { |
| 361 | var ID = $( this ).attr( 'id' ); |
| 362 | |
| 363 | if ( ID ) { |
| 364 | if ( $.inArray( $( this ).attr( 'type' ), [ 'checkbox', 'radio' ] ) !== -1 ) { |
| 365 | paymentDetails[ ID ] = $( this ).prop( 'checked' ); |
| 366 | } else { |
| 367 | paymentDetails[ ID ] = $( this ).val(); |
| 368 | } |
| 369 | } |
| 370 | }); |
| 371 | |
| 372 | // Always update the fragments |
| 373 | if ( data && data.fragments ) { |
| 374 | $.each( data.fragments, function ( key, value ) { |
| 375 | if ( ! wc_checkout_form.fragments || wc_checkout_form.fragments[ key ] !== value ) { |
| 376 | $( key ).replaceWith( value ); |
| 377 | } |
| 378 | $( key ).unblock(); |
| 379 | } ); |
| 380 | wc_checkout_form.fragments = data.fragments; |
| 381 | } |
| 382 | |
| 383 | // Recheck the terms and conditions box, if needed |
| 384 | if ( termsCheckBoxChecked ) { |
| 385 | $( '#terms' ).prop( 'checked', true ); |
| 386 | } |
| 387 | |
| 388 | // Fill in the payment details if possible without overwriting data if set. |
| 389 | if ( ! $.isEmptyObject( paymentDetails ) ) { |
| 390 | $( '.payment_box :input' ).each( function() { |
| 391 | var ID = $( this ).attr( 'id' ); |
| 392 | if ( ID ) { |
| 393 | if ( $.inArray( $( this ).attr( 'type' ), [ 'checkbox', 'radio' ] ) !== -1 ) { |
| 394 | $( this ).prop( 'checked', paymentDetails[ ID ] ).trigger( 'change' ); |
| 395 | } else if ( $.inArray( $( this ).attr( 'type' ), [ 'select' ] ) !== -1 ) { |
| 396 | $( this ).val( paymentDetails[ ID ] ).trigger( 'change' ); |
| 397 | } else if ( null !== $( this ).val() && 0 === $( this ).val().length ) { |
| 398 | $( this ).val( paymentDetails[ ID ] ).trigger( 'change' ); |
| 399 | } |
| 400 | } |
| 401 | }); |
| 402 | } |
| 403 | |
| 404 | // Check for error |
| 405 | if ( data && 'failure' === data.result ) { |
| 406 | |
| 407 | var $form = $( 'form.checkout' ); |
| 408 | |
| 409 | // Remove notices from all sources |
| 410 | $( '.woocommerce-error, .woocommerce-message' ).remove(); |
| 411 | |
| 412 | // Add new errors returned by this event |
| 413 | if ( data.messages ) { |
| 414 | $form.prepend( '<div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-updateOrderReview">' + data.messages + '</div>' ); // eslint-disable-line max-len |
| 415 | } else { |
| 416 | $form.prepend( data ); |
| 417 | } |
| 418 | |
| 419 | // Lose focus for all fields |
| 420 | $form.find( '.input-text, select, input:checkbox' ).trigger( 'validate' ).trigger( 'blur' ); |
| 421 | |
| 422 | wc_checkout_form.scroll_to_notices(); |
| 423 | } |
| 424 | |
| 425 | // Re-init methods |
| 426 | wc_checkout_form.init_payment_methods(); |
| 427 | |
| 428 | // Fire updated_checkout event. |
| 429 | $( document.body ).trigger( 'updated_checkout', [ data ] ); |
| 430 | } |
| 431 | |
| 432 | }); |
| 433 | }, |
| 434 | handleUnloadEvent: function( e ) { |
| 435 | // Modern browsers have their own standard generic messages that they will display. |
| 436 | // Confirm, alert, prompt or custom message are not allowed during the unload event |
| 437 | // Browsers will display their own standard messages |
| 438 | |
| 439 | // Check if the browser is Internet Explorer |
| 440 | if((navigator.userAgent.indexOf('MSIE') !== -1 ) || (!!document.documentMode)) { |
| 441 | // IE handles unload events differently than modern browsers |
| 442 | e.preventDefault(); |
| 443 | return undefined; |
| 444 | } |
| 445 | |
| 446 | return true; |
| 447 | }, |
| 448 | attachUnloadEventsOnSubmit: function() { |
| 449 | $( window ).on('beforeunload', this.handleUnloadEvent); |
| 450 | }, |
| 451 | detachUnloadEventsOnSubmit: function() { |
| 452 | $( window ).unbind('beforeunload', this.handleUnloadEvent); |
| 453 | }, |
| 454 | blockOnSubmit: function( $form ) { |
| 455 | var form_data = $form.data(); |
| 456 | |
| 457 | if ( 1 !== form_data['blockUI.isBlocked'] ) { |
| 458 | $form.block({ |
| 459 | message: null, |
| 460 | overlayCSS: { |
| 461 | background: '#fff', |
| 462 | opacity: 0.6 |
| 463 | } |
| 464 | }); |
| 465 | } |
| 466 | }, |
| 467 | submitOrder: function() { |
| 468 | wc_checkout_form.blockOnSubmit( $( this ) ); |
| 469 | }, |
| 470 | submit: function() { |
| 471 | wc_checkout_form.reset_update_checkout_timer(); |
| 472 | var $form = $( this ); |
| 473 | |
| 474 | if ( $form.is( '.processing' ) ) { |
| 475 | return false; |
| 476 | } |
| 477 | |
| 478 | // Trigger a handler to let gateways manipulate the checkout if needed |
| 479 | // eslint-disable-next-line max-len |
| 480 | if ( $form.triggerHandler( 'checkout_place_order' ) !== false && $form.triggerHandler( 'checkout_place_order_' + wc_checkout_form.get_payment_method() ) !== false ) { |
| 481 | |
| 482 | $form.addClass( 'processing' ); |
| 483 | |
| 484 | wc_checkout_form.blockOnSubmit( $form ); |
| 485 | |
| 486 | // Attach event to block reloading the page when the form has been submitted |
| 487 | wc_checkout_form.attachUnloadEventsOnSubmit(); |
| 488 | |
| 489 | // ajaxSetup is global, but we use it to ensure JSON is valid once returned. |
| 490 | $.ajaxSetup( { |
| 491 | dataFilter: function( raw_response, dataType ) { |
| 492 | // We only want to work with JSON |
| 493 | if ( 'json' !== dataType ) { |
| 494 | return raw_response; |
| 495 | } |
| 496 | |
| 497 | if ( wc_checkout_form.is_valid_json( raw_response ) ) { |
| 498 | return raw_response; |
| 499 | } else { |
| 500 | // Attempt to fix the malformed JSON |
| 501 | var maybe_valid_json = raw_response.match( /{"result.*}/ ); |
| 502 | |
| 503 | if ( null === maybe_valid_json ) { |
| 504 | console.log( 'Unable to fix malformed JSON' ); |
| 505 | } else if ( wc_checkout_form.is_valid_json( maybe_valid_json[0] ) ) { |
| 506 | console.log( 'Fixed malformed JSON. Original:' ); |
| 507 | console.log( raw_response ); |
| 508 | raw_response = maybe_valid_json[0]; |
| 509 | } else { |
| 510 | console.log( 'Unable to fix malformed JSON' ); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | return raw_response; |
| 515 | } |
| 516 | } ); |
| 517 | |
| 518 | $.ajax({ |
| 519 | type: 'POST', |
| 520 | url: wc_checkout_params.checkout_url, |
| 521 | data: $form.serialize(), |
| 522 | dataType: 'json', |
| 523 | success: function( result ) { |
| 524 | // Detach the unload handler that prevents a reload / redirect |
| 525 | wc_checkout_form.detachUnloadEventsOnSubmit(); |
| 526 | |
| 527 | try { |
| 528 | if ( 'success' === result.result && $form.triggerHandler( 'checkout_place_order_success', result ) !== false ) { |
| 529 | if ( -1 === result.redirect.indexOf( 'https://' ) || -1 === result.redirect.indexOf( 'http://' ) ) { |
| 530 | window.location = result.redirect; |
| 531 | } else { |
| 532 | window.location = decodeURI( result.redirect ); |
| 533 | } |
| 534 | } else if ( 'failure' === result.result ) { |
| 535 | throw 'Result failure'; |
| 536 | } else { |
| 537 | throw 'Invalid response'; |
| 538 | } |
| 539 | } catch( err ) { |
| 540 | // Reload page |
| 541 | if ( true === result.reload ) { |
| 542 | window.location.reload(); |
| 543 | return; |
| 544 | } |
| 545 | |
| 546 | // Trigger update in case we need a fresh nonce |
| 547 | if ( true === result.refresh ) { |
| 548 | $( document.body ).trigger( 'update_checkout' ); |
| 549 | } |
| 550 | |
| 551 | // Add new errors |
| 552 | if ( result.messages ) { |
| 553 | wc_checkout_form.submit_error( result.messages ); |
| 554 | } else { |
| 555 | wc_checkout_form.submit_error( '<div class="woocommerce-error">' + wc_checkout_params.i18n_checkout_error + '</div>' ); // eslint-disable-line max-len |
| 556 | } |
| 557 | } |
| 558 | }, |
| 559 | error: function( jqXHR, textStatus, errorThrown ) { |
| 560 | // Detach the unload handler that prevents a reload / redirect |
| 561 | wc_checkout_form.detachUnloadEventsOnSubmit(); |
| 562 | |
| 563 | wc_checkout_form.submit_error( '<div class="woocommerce-error">' + errorThrown + '</div>' ); |
| 564 | } |
| 565 | }); |
| 566 | } |
| 567 | |
| 568 | return false; |
| 569 | }, |
| 570 | submit_error: function( error_message ) { |
| 571 | $( '.woocommerce-NoticeGroup-checkout, .woocommerce-error, .woocommerce-message' ).remove(); |
| 572 | wc_checkout_form.$checkout_form.prepend( '<div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-checkout">' + error_message + '</div>' ); // eslint-disable-line max-len |
| 573 | wc_checkout_form.$checkout_form.removeClass( 'processing' ).unblock(); |
| 574 | wc_checkout_form.$checkout_form.find( '.input-text, select, input:checkbox' ).trigger( 'validate' ).trigger( 'blur' ); |
| 575 | wc_checkout_form.scroll_to_notices(); |
| 576 | $( document.body ).trigger( 'checkout_error' , [ error_message ] ); |
| 577 | }, |
| 578 | scroll_to_notices: function() { |
| 579 | var scrollElement = $( '.woocommerce-NoticeGroup-updateOrderReview, .woocommerce-NoticeGroup-checkout' ); |
| 580 | |
| 581 | if ( ! scrollElement.length ) { |
| 582 | scrollElement = $( '.form.checkout' ); |
| 583 | } |
| 584 | $.scroll_to_notices( scrollElement ); |
| 585 | } |
| 586 | }; |
| 587 | |
| 588 | var wc_checkout_coupons = { |
| 589 | init: function() { |
| 590 | $( document.body ).on( 'click', 'a.showcoupon', this.show_coupon_form ); |
| 591 | $( document.body ).on( 'click', '.woocommerce-remove-coupon', this.remove_coupon ); |
| 592 | $( 'form.checkout_coupon' ).hide().submit( this.submit ); |
| 593 | }, |
| 594 | show_coupon_form: function() { |
| 595 | $( '.checkout_coupon' ).slideToggle( 400, function() { |
| 596 | $( '.checkout_coupon' ).find( ':input:eq(0)' ).focus(); |
| 597 | }); |
| 598 | return false; |
| 599 | }, |
| 600 | submit: function() { |
| 601 | var $form = $( this ); |
| 602 | |
| 603 | if ( $form.is( '.processing' ) ) { |
| 604 | return false; |
| 605 | } |
| 606 | |
| 607 | $form.addClass( 'processing' ).block({ |
| 608 | message: null, |
| 609 | overlayCSS: { |
| 610 | background: '#fff', |
| 611 | opacity: 0.6 |
| 612 | } |
| 613 | }); |
| 614 | |
| 615 | var data = { |
| 616 | security: wc_checkout_params.apply_coupon_nonce, |
| 617 | coupon_code: $form.find( 'input[name="coupon_code"]' ).val() |
| 618 | }; |
| 619 | |
| 620 | $.ajax({ |
| 621 | type: 'POST', |
| 622 | url: wc_checkout_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'apply_coupon' ), |
| 623 | data: data, |
| 624 | success: function( code ) { |
| 625 | $( '.woocommerce-error, .woocommerce-message' ).remove(); |
| 626 | $form.removeClass( 'processing' ).unblock(); |
| 627 | |
| 628 | if ( code ) { |
| 629 | $form.before( code ); |
| 630 | $form.slideUp(); |
| 631 | |
| 632 | $( document.body ).trigger( 'applied_coupon_in_checkout', [ data.coupon_code ] ); |
| 633 | $( document.body ).trigger( 'update_checkout', { update_shipping_method: false } ); |
| 634 | } |
| 635 | }, |
| 636 | dataType: 'html' |
| 637 | }); |
| 638 | |
| 639 | return false; |
| 640 | }, |
| 641 | remove_coupon: function( e ) { |
| 642 | e.preventDefault(); |
| 643 | |
| 644 | var container = $( this ).parents( '.woocommerce-checkout-review-order' ), |
| 645 | coupon = $( this ).data( 'coupon' ); |
| 646 | |
| 647 | container.addClass( 'processing' ).block({ |
| 648 | message: null, |
| 649 | overlayCSS: { |
| 650 | background: '#fff', |
| 651 | opacity: 0.6 |
| 652 | } |
| 653 | }); |
| 654 | |
| 655 | var data = { |
| 656 | security: wc_checkout_params.remove_coupon_nonce, |
| 657 | coupon: coupon |
| 658 | }; |
| 659 | |
| 660 | $.ajax({ |
| 661 | type: 'POST', |
| 662 | url: wc_checkout_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'remove_coupon' ), |
| 663 | data: data, |
| 664 | success: function( code ) { |
| 665 | $( '.woocommerce-error, .woocommerce-message' ).remove(); |
| 666 | container.removeClass( 'processing' ).unblock(); |
| 667 | |
| 668 | if ( code ) { |
| 669 | $( 'form.woocommerce-checkout' ).before( code ); |
| 670 | |
| 671 | $( document.body ).trigger( 'removed_coupon_in_checkout', [ data.coupon_code ] ); |
| 672 | $( document.body ).trigger( 'update_checkout', { update_shipping_method: false } ); |
| 673 | |
| 674 | // Remove coupon code from coupon field |
| 675 | $( 'form.checkout_coupon' ).find( 'input[name="coupon_code"]' ).val( '' ); |
| 676 | } |
| 677 | }, |
| 678 | error: function ( jqXHR ) { |
| 679 | if ( wc_checkout_params.debug_mode ) { |
| 680 | /* jshint devel: true */ |
| 681 | console.log( jqXHR.responseText ); |
| 682 | } |
| 683 | }, |
| 684 | dataType: 'html' |
| 685 | }); |
| 686 | } |
| 687 | }; |
| 688 | |
| 689 | var wc_checkout_login_form = { |
| 690 | init: function() { |
| 691 | $( document.body ).on( 'click', 'a.showlogin', this.show_login_form ); |
| 692 | }, |
| 693 | show_login_form: function() { |
| 694 | $( 'form.login, form.woocommerce-form--login' ).slideToggle(); |
| 695 | return false; |
| 696 | } |
| 697 | }; |
| 698 | |
| 699 | var wc_terms_toggle = { |
| 700 | init: function() { |
| 701 | $( document.body ).on( 'click', 'a.woocommerce-terms-and-conditions-link', this.toggle_terms ); |
| 702 | }, |
| 703 | |
| 704 | toggle_terms: function() { |
| 705 | if ( $( '.woocommerce-terms-and-conditions' ).length ) { |
| 706 | $( '.woocommerce-terms-and-conditions' ).slideToggle( function() { |
| 707 | var link_toggle = $( '.woocommerce-terms-and-conditions-link' ); |
| 708 | |
| 709 | if ( $( '.woocommerce-terms-and-conditions' ).is( ':visible' ) ) { |
| 710 | link_toggle.addClass( 'woocommerce-terms-and-conditions-link--open' ); |
| 711 | link_toggle.removeClass( 'woocommerce-terms-and-conditions-link--closed' ); |
| 712 | } else { |
| 713 | link_toggle.removeClass( 'woocommerce-terms-and-conditions-link--open' ); |
| 714 | link_toggle.addClass( 'woocommerce-terms-and-conditions-link--closed' ); |
| 715 | } |
| 716 | } ); |
| 717 | |
| 718 | return false; |
| 719 | } |
| 720 | } |
| 721 | }; |
| 722 | |
| 723 | wc_checkout_form.init(); |
| 724 | wc_checkout_coupons.init(); |
| 725 | wc_checkout_login_form.init(); |
| 726 | wc_terms_toggle.init(); |
| 727 | }); |
| 728 |