1/* global wcsatt_cart_params */ 2jQuery( function( $ ) { 3 4 // Ensure wcsatt_cart_params exists to continue. 5 if ( typeof wcsatt_cart_params === 'undefined' ) { 6 return false; 7 } 8 9 var $document = $( document ), 10 $body = $( document.body ); 11 12 // Load matching subscription schemes when checking the "Add to subscription" box. 13 $document.on( 'change', '.wcsatt-add-cart-to-subscription-action-input', function() { 14 15 var $cart_totals = $( 'div.cart_totals' ), 16 $add_to_subscription = $( this ), 17 $add_to_subscription_wrapper = $add_to_subscription.closest( '.wcsatt-add-cart-to-subscription-wrapper' ), 18 $add_to_subscription_options = $add_to_subscription_wrapper.find( '.wcsatt-add-cart-to-subscription-options' ), 19 is_checked = $add_to_subscription.is( ':checked' ); 20 21 if ( is_checked ) { 22 23 $add_to_subscription_wrapper.block( { 24 message: null, 25 overlayCSS: { 26 background: '#fff', 27 opacity: 0.6 28 } 29 } ); 30 31 var data = { 32 add_to_subscription_checked: $add_to_subscription.is( ':checked' ) ? 'yes' : 'no', 33 action: 'wcsatt_load_subscriptions_matching_cart' 34 }; 35 36 $.post( wcsatt_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', data.action ), data, function( response ) { 37 38 if ( 'success' === response.result ) { 39 40 $add_to_subscription_options.html( response.html ); 41 $add_to_subscription_wrapper.removeClass( 'closed' ); 42 $add_to_subscription_wrapper.addClass( 'open' ); 43 $add_to_subscription_options.slideDown( 200 ); 44 45 } else { 46 47 window.alert( wcsatt_cart_params.i18n_subs_load_error ); 48 } 49 50 $add_to_subscription_wrapper.unblock(); 51 52 } ); 53 54 } else { 55 56 $add_to_subscription_wrapper.removeClass( 'open' ); 57 $add_to_subscription_wrapper.addClass( 'closed' ); 58 $add_to_subscription_options.slideUp( 200 ); 59 } 60 61 } ); 62 63 // AJAX handler for the add-cart-to-subscription form in the checkout page. 64 $document.on( 'click', '.woocommerce-checkout .wcsatt-add-to-subscription-button', function( event ) { 65 66 // Prevent submitting the form, as this leads to a new order and subscription. 67 event.preventDefault(); 68 69 var $add_to_subscription_button = $( this ), 70 $add_to_subscription_wrapper = $add_to_subscription_button.closest( '.wcsatt-add-cart-to-subscription-options' ), 71 $checkout_form = $( '.woocommerce-checkout form' ); 72 73 $checkout_form.block( { 74 message: null, 75 overlayCSS: { 76 background: '#fff', 77 opacity: 0.6 78 } 79 } ); 80 81 var data = { 82 'add-cart-to-subscription': parseInt( $add_to_subscription_button.val(), 10 ), 83 'add-to-subscription-checked': 'yes', 84 action: 'wcsatt_add_cart_to_subscription_from_checkout', 85 wcsatt_nonce: $add_to_subscription_wrapper.find( '#wcsatt_nonce' ).val() 86 }; 87 88 $.post( wcsatt_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', data.action ), data, function( response ) { 89 90 if ( 'success' === response.result ) { 91 92 if ( isValidUrl( response.url ) ) { 93 // For a successful request, redirect customer to the subscription page under "My Account". 94 location.href = response.url; 95 } 96 97 } else { 98 // For failed requests, refresh the checkout page for error notices to be displayed. 99 $body.trigger("update_checkout"); 100 } 101 102 $checkout_form.unblock(); 103 104 } ); 105 106 var isValidUrl = function( string ) { 107 try { 108 new URL( string ); 109 return true; 110 } catch ( err ) { 111 return false; 112 } 113 }; 114 } ); 115 116} );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.