multistep.js
254 lines
| 1 | jQuery( window ).ready( function ( $ ) { |
| 2 | 'use strict'; |
| 3 | |
| 4 | var shoppress_checkout = { |
| 5 | $tabs: $( '.sp-tab-item' ), |
| 6 | $sections: $( '.sp-step-item' ), |
| 7 | $buttons: $( '.sp-nav-button' ), |
| 8 | $checkout_form: $( 'form.woocommerce-checkout' ), |
| 9 | $coupon_form: $( '#checkout_coupon' ), |
| 10 | $before_form: $( '#woocommerce_before_checkout_form' ), |
| 11 | current_step: $( 'ul.sp-tabs-list' ).data( 'current-title' ), |
| 12 | |
| 13 | init: function () { |
| 14 | var self = this; |
| 15 | |
| 16 | $( '.woocommerce-checkout' ).on( |
| 17 | 'shoppress_switch_tab', |
| 18 | function ( event, index ) { |
| 19 | self.switch_tab( index ); |
| 20 | } |
| 21 | ); |
| 22 | |
| 23 | $( '.sp-step-item:first' ).addClass( 'current' ); |
| 24 | |
| 25 | $( '#sp-next' ).on( 'click', function () { |
| 26 | var $currentStepItem = $( '.sp-step-item.current' ); |
| 27 | var check_status = true; |
| 28 | if ( |
| 29 | $currentStepItem.hasClass( 'sp-step-shipping' ) || |
| 30 | $currentStepItem.hasClass( 'sp-step-billing-and-shipping' ) |
| 31 | ) { |
| 32 | if ( |
| 33 | ! $( |
| 34 | '#ship-to-different-address-checkbox', |
| 35 | $currentStepItem |
| 36 | ).is( ':checked' ) |
| 37 | ) { |
| 38 | $currentStepItem = $currentStepItem.find( |
| 39 | '.woocommerce-billing-fields' |
| 40 | ); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | if ( check_status ) { |
| 45 | $( |
| 46 | ' .validate-required input, .validate-required select, .validate-required textarea', |
| 47 | $currentStepItem |
| 48 | ).each( function ( i, input ) { |
| 49 | $( input ).trigger( 'change' ); |
| 50 | } ); |
| 51 | |
| 52 | if ( |
| 53 | $( |
| 54 | '.validate-required.woocommerce-invalid', |
| 55 | $currentStepItem |
| 56 | ).length |
| 57 | ) { |
| 58 | jQuery( [ |
| 59 | document.documentElement, |
| 60 | document.body, |
| 61 | ] ).animate( |
| 62 | { |
| 63 | scrollTop: $( |
| 64 | '.validate-required.woocommerce-invalid' |
| 65 | ) |
| 66 | .first() |
| 67 | .offset().top, |
| 68 | }, |
| 69 | 1000 |
| 70 | ); |
| 71 | |
| 72 | return; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | self.switch_tab( self.current_index() + 1 ); |
| 77 | } ); |
| 78 | |
| 79 | $( '#sp-prev' ).on( 'click', function () { |
| 80 | self.switch_tab( self.current_index() - 1 ); |
| 81 | } ); |
| 82 | |
| 83 | $( document ).on( 'checkout_error', function () { |
| 84 | if ( ! $( '#createaccount' ).is( ':checked' ) ) { |
| 85 | $( |
| 86 | '#account_password_field, #account_username_field' |
| 87 | ).removeClass( 'woocommerce-invalid-required-field' ); |
| 88 | } |
| 89 | |
| 90 | if ( |
| 91 | ! $( '#ship-to-different-address-checkbox' ).is( |
| 92 | ':checked' |
| 93 | ) |
| 94 | ) { |
| 95 | $( |
| 96 | '.woocommerce-shipping-fields__field-wrapper p' |
| 97 | ).removeClass( 'woocommerce-invalid-required-field' ); |
| 98 | } |
| 99 | |
| 100 | var section_class = $( '.woocommerce-invalid-required-field' ) |
| 101 | .closest( '.sp-step-item' ) |
| 102 | .attr( 'class' ); |
| 103 | |
| 104 | $( '.sp-step-item' ).each( function ( i ) { |
| 105 | if ( $( this ).attr( 'class' ) === section_class ) { |
| 106 | self.switch_tab( i ); |
| 107 | } |
| 108 | } ); |
| 109 | } ); |
| 110 | |
| 111 | $( '.woocommerce-checkout' ).on( 'keydown', function ( e ) { |
| 112 | if ( e.which === 13 ) { |
| 113 | e.preventDefault(); |
| 114 | return false; |
| 115 | } |
| 116 | } ); |
| 117 | |
| 118 | if ( |
| 119 | typeof window.location.hash != 'undefined' && |
| 120 | window.location.hash |
| 121 | ) { |
| 122 | changeTabOnHash( window.location.hash ); |
| 123 | } |
| 124 | |
| 125 | $( window ).on( 'hashchange', function () { |
| 126 | changeTabOnHash( window.location.hash ); |
| 127 | } ); |
| 128 | |
| 129 | function changeTabOnHash( hash ) { |
| 130 | if ( /step-[0-9]/.test( hash ) ) { |
| 131 | var step = hash.match( /step-([0-9])/ )[ 1 ]; |
| 132 | self.switch_tab( step ); |
| 133 | } |
| 134 | } |
| 135 | }, |
| 136 | current_index: function () { |
| 137 | return this.$sections.index( this.$sections.filter( '.current' ) ); |
| 138 | }, |
| 139 | scroll_top: function () { |
| 140 | if ( $( '.sp-tabs-wrapper' ).length === 0 ) { |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | var diff = |
| 145 | $( '.sp-tabs-wrapper' ).offset().top - $( window ).scrollTop(); |
| 146 | var scroll_offset = 70; |
| 147 | |
| 148 | if ( diff < -40 ) { |
| 149 | $( 'html, body' ).animate( |
| 150 | { |
| 151 | scrollTop: |
| 152 | $( '.sp-tabs-wrapper' ).offset().top - |
| 153 | scroll_offset, |
| 154 | }, |
| 155 | 800 |
| 156 | ); |
| 157 | } |
| 158 | }, |
| 159 | switch_tab: function ( index ) { |
| 160 | var self = this; |
| 161 | |
| 162 | if ( index < 0 || index > this.$sections.length - 1 ) { |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | this.scroll_top(); |
| 167 | |
| 168 | $( 'html, body' ) |
| 169 | .promise() |
| 170 | .done( function () { |
| 171 | self.$tabs |
| 172 | .removeClass( 'previous' ) |
| 173 | .filter( '.current' ) |
| 174 | .addClass( 'previous' ); |
| 175 | self.$sections |
| 176 | .removeClass( 'previous' ) |
| 177 | .filter( '.current' ) |
| 178 | .addClass( 'previous' ); |
| 179 | $( |
| 180 | '.woocommerce-NoticeGroup-checkout:not(sp-error)' |
| 181 | ).show(); |
| 182 | |
| 183 | self.$tabs.removeClass( 'current' ); |
| 184 | self.$tabs.eq( index ).addClass( 'current' ); |
| 185 | self.current_step = self.$tabs |
| 186 | .eq( index ) |
| 187 | .data( 'step-title' ); |
| 188 | $( '.sp-tabs-list' ).data( |
| 189 | 'current-title', |
| 190 | self.current_step |
| 191 | ); |
| 192 | |
| 193 | self.$sections.removeClass( 'current' ); |
| 194 | self.$sections.eq( index ).addClass( 'current' ); |
| 195 | |
| 196 | self.$buttons.removeClass( 'current' ); |
| 197 | self.$coupon_form.hide(); |
| 198 | self.$before_form.hide(); |
| 199 | |
| 200 | if ( index < self.$sections.length - 1 ) { |
| 201 | $( '#sp-next' ).addClass( 'current' ); |
| 202 | } |
| 203 | |
| 204 | if ( |
| 205 | typeof $( '.woocommerce-NoticeGroup-checkout' ).data( |
| 206 | 'for-step' |
| 207 | ) !== 'undefined' && |
| 208 | $( '.woocommerce-NoticeGroup-checkout' ).data( |
| 209 | 'for-step' |
| 210 | ) !== self.current_step |
| 211 | ) { |
| 212 | $( '.woocommerce-NoticeGroup-checkout' ).remove(); |
| 213 | } |
| 214 | |
| 215 | if ( index === 0 && $( '.sp-step-login' ).length > 0 ) { |
| 216 | $( '#sp-next' ).removeClass( 'current' ); |
| 217 | $( |
| 218 | '.woocommerce-NoticeGroup-checkout:not(sp-error)' |
| 219 | ).hide(); |
| 220 | } |
| 221 | |
| 222 | if ( index === self.$sections.length - 1 ) { |
| 223 | $( '#sp-prev' ).addClass( 'current' ); |
| 224 | $( '#sp-submit' ).addClass( 'current' ); |
| 225 | self.$checkout_form |
| 226 | .removeClass( 'processing' ) |
| 227 | .unblock(); |
| 228 | } |
| 229 | |
| 230 | if ( index != 0 ) { |
| 231 | $( '#sp-prev' ).addClass( 'current' ); |
| 232 | } else { |
| 233 | $( '#sp-return-to-shop' ).addClass( 'current' ); |
| 234 | } |
| 235 | |
| 236 | if ( $( '.sp-step-review.current' ).length > 0 ) { |
| 237 | self.$coupon_form.show(); |
| 238 | } |
| 239 | |
| 240 | if ( |
| 241 | $( |
| 242 | '.sp-' + |
| 243 | self.$before_form.data( 'step' ) + |
| 244 | '.current' |
| 245 | ).length > 0 |
| 246 | ) { |
| 247 | self.$before_form.show(); |
| 248 | } |
| 249 | } ); |
| 250 | }, |
| 251 | }; |
| 252 | shoppress_checkout.init(); |
| 253 | } ); |
| 254 |