1/* global wc_ppec_generate_cart_context */ 2;(function( $, window, document ) { 3 'use strict'; 4 5 // This button state is only applicable to non-SPB click handler below. 6 var button_enabled = true; 7 $( '#woo_pp_ec_button_product' ) 8 .on( 'enable.legacy', function() { 9 button_enabled = true; 10 } ) 11 .on( 'disable.legacy', function() { 12 button_enabled = false; 13 } ); 14 15 $( '#woo_pp_ec_button_product' ) 16 .on( 'enable', function() { 17 $( '#woo_pp_ec_button_product' ) 18 .css( { 19 'cursor': '', 20 '-webkit-filter': '', // Safari 6.0 - 9.0 21 'filter': '', 22 } ) 23 .off( 'mouseup' ) 24 .find( '> *' ) 25 .css( 'pointer-events', '' ); 26 } ) 27 .on( 'disable', function() { 28 $( '#woo_pp_ec_button_product' ) 29 .css( { 30 'cursor': 'not-allowed', 31 '-webkit-filter': 'grayscale( 100% )', // Safari 6.0 - 9.0 32 'filter': 'grayscale( 100% )', 33 } ) 34 .on( 'mouseup', function( event ) { 35 event.stopImmediatePropagation(); 36 form.find( ':submit' ).trigger( 'click' ); 37 } ) 38 .find( '> *' ) 39 .css( 'pointer-events', 'none' ); 40 } ); 41 42 // True if the product is simple or the user selected a valid variation. False on variable product without a valid variation selected 43 var variation_valid = true; 44 45 // True if all the fields of the product form are valid (such as required fields configured by Product Add-Ons). False otherwise 46 var fields_valid = true; 47 48 var form = $( 'form.cart' ); 49 50 var update_button = function() { 51 $( '#woo_pp_ec_button_product' ).trigger( ( variation_valid && fields_valid ) ? 'enable' : 'disable' ); 52 }; 53 54 var validate_form = function() { 55 // Check fields are valid and allow third parties to attach their own validation checks
56 fields_valid = form.get( 0 ).checkValidity() && $( document ).triggerHandler( 'wc_ppec_validate_product_form', [ fields_valid, form ] ) !== false; 57 58 update_button(); 59 }; 60 61 // It's a variations form, button availability should depend on its events 62 if ( $( '.variations_form' ).length ) { 63 variation_valid = false; 64 65 $( '.variations_form' ) 66 .on( 'show_variation', function( event, form, purchasable ) { 67 variation_valid = purchasable; 68 update_button(); 69 } ) 70 .on( 'hide_variation', function() { 71 variation_valid = false; 72 update_button(); 73 } ); 74 } 75 76 // Disable the button if there are invalid fields in the product page (like required fields from Product Addons) 77 form.on( 'change', 'select, input, textarea', function() { 78 // Hack: IE11 uses the previous field value for the checkValidity() check if it's called in the onChange handler 79 setTimeout( validate_form, 0 ); 80 } ); 81 82 $( document ).ready(function() { 83 validate_form(); 84 } ); 85 86 var generate_cart = function( callback ) { 87 var data = { 88 'nonce': wc_ppec_generate_cart_context.generate_cart_nonce, 89 'attributes': {}, 90 }; 91 92 var field_pairs = form.serializeArray(); 93 94 for ( var i = 0; i < field_pairs.length; i++ ) { 95 // Prevent the default WooCommerce PHP form handler from recognizing this as an "add to cart" call 96 if ( 'add-to-cart' === field_pairs[ i ].name ) { 97 field_pairs[ i ].name = 'ppec-add-to-cart'; 98 } 99 100 // Save attributes as a separate prop in `data` object, 101 // so that `attributes` can be used later on when adding a variable product to cart 102 if ( -1 !== field_pairs[ i ].name.indexOf( 'attribute_' ) ) { 103 data.attributes[ field_pairs[ i ].name ] = field_pairs[ i ].value; 104 continue; 105 } 106 107 data[ field_pairs[ i ].name ] = field_pairs[ i ].value; 108 } 109 110 // If this is a simple product, the "Submit" button has the product ID as "value", we need to include it explicitly 111 data[ 'ppec-add-to-cart' ] = $( '[name=add-to-cart]' ).val(); 112 113 $.ajax( { 114 type: 'POST', 115 data: data, 116 url: wc_ppec_generate_cart_context.ajaxurl, 117 success: callback, 118 } ); 119 }; 120 121 window.wc_ppec_generate_cart = generate_cart; 122 123 // Non-SPB mode click handler, namespaced as 'legacy' as it's replaced by `payment` callback of Button API. 124 $( '#woo_pp_ec_button_product' ).on( 'click.legacy', function( event ) { 125 event.preventDefault(); 126 127 if ( ! button_enabled ) { 128 return; 129 } 130 131 $( '#woo_pp_ec_button_product' ).trigger( 'disable' ); 132 133 var href = $(this).attr( 'href' ); 134 135 generate_cart( function() { 136 window.location.href = href; 137 } ); 138 } ); 139 140})( jQuery, window, document );
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.