Changeset 2118918
- Timestamp:
- 07/07/2019 02:40:54 PM (7 years ago)
- Location:
- pp-express-wc4jp/trunk/assets
- Files:
-
- 2 added
- 2 edited
-
css/wc-gateway-ppec-frontend-cart.css (modified) (2 diffs)
-
js/wc-gateway-ppec-generate-cart.js (modified) (4 diffs)
-
js/wc-gateway-ppec-order-review.js (added)
-
js/wc-gateway-ppec-smart-payment-buttons.js (added)
Legend:
- Unmodified
- Added
- Removed
-
pp-express-wc4jp/trunk/assets/css/wc-gateway-ppec-frontend-cart.css
r1838283 r2118918 3 3 margin: 1em 0; 4 4 overflow: hidden; 5 } 6 .wcppec-checkout-buttons .woocommerce-error { 7 text-align: left; 5 8 } 6 9 .wcppec-checkout-buttons__separator { … … 29 32 border: 0 !important; 30 33 } 34 .site-header .widget_shopping_cart p.buttons.wcppec-cart-widget-spb { 35 padding: 0 1em 1em; 36 } 37 .site-header .widget_shopping_cart .woocommerce-mini-cart__empty-message + p.buttons.wcppec-cart-widget-spb { 38 display: none; 39 } -
pp-express-wc4jp/trunk/assets/js/wc-gateway-ppec-generate-cart.js
r1838283 r2118918 1 /* global wc_ppec_ context */1 /* global wc_ppec_generate_cart_context */ 2 2 ;(function( $, window, document ) { 3 3 'use strict'; 4 4 5 // This button state is only applicable to non-SPB click handler below. 5 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 } ); 6 14 7 var toggle_button_availability = function( available ) { 8 if ( available ) { 9 button_enabled = true; 15 $( '#woo_pp_ec_button_product' ) 16 .on( 'enable', function() { 10 17 $( '#woo_pp_ec_button_product' ).css( { 11 18 'cursor': '', … … 13 20 'filter': '', 14 21 } ); 15 } else { 16 button_enabled = false; 22 $( '#woo_pp_ec_button_product > *' ).css( 'pointer-events', '' ); 23 } ) 24 .on( 'disable', function() { 17 25 $( '#woo_pp_ec_button_product' ).css( { 18 26 'cursor': 'not-allowed', … … 20 28 'filter': 'grayscale( 100% )', 21 29 } ); 22 } 23 } 24 25 var get_attributes = function() { 26 var select = $( '.variations_form' ).find( '.variations select' ), 27 data = {}, 28 count = 0, 29 chosen = 0; 30 31 select.each( function() { 32 var attribute_name = $( this ).data( 'attribute_name' ) || $( this ).attr( 'name' ); 33 var value = $( this ).val() || ''; 34 35 if ( value.length > 0 ) { 36 chosen++; 37 } 38 39 count++; 40 data[ attribute_name ] = value; 30 $( '#woo_pp_ec_button_product > *' ).css( 'pointer-events', 'none' ); 41 31 } ); 42 32 43 return { 44 'count' : count, 45 'chosenCount': chosen, 46 'data' : data 47 }; 33 // True if the product is simple or the user selected a valid variation. False on variable product without a valid variation selected 34 var variation_valid = true; 35 36 // True if all the fields of the product form are valid (such as required fields configured by Product Add-Ons). False otherwise 37 var fields_valid = true; 38 39 var form = $( 'form.cart' ); 40 41 var update_button = function() { 42 $( '#woo_pp_ec_button_product' ).trigger( ( variation_valid && fields_valid ) ? 'enable' : 'disable' ); 43 }; 44 45 var validate_form = function() { 46 fields_valid = form.get( 0 ).checkValidity(); 47 update_button(); 48 48 }; 49 49 50 50 // It's a variations form, button availability should depend on its events 51 51 if ( $( '.variations_form' ).length ) { 52 toggle_button_availability( false );52 variation_valid = false; 53 53 54 54 $( '.variations_form' ) 55 55 .on( 'show_variation', function( event, form, purchasable ) { 56 toggle_button_availability( purchasable ); 56 variation_valid = purchasable; 57 update_button(); 57 58 } ) 58 59 .on( 'hide_variation', function() { 59 toggle_button_availability( false ); 60 variation_valid = false; 61 update_button(); 60 62 } ); 61 63 } 62 64 63 $( '#woo_pp_ec_button_product' ).click( function( event ) { 65 // Disable the button if there are invalid fields in the product page (like required fields from Product Addons) 66 form.on( 'change', 'select, input, textarea', function() { 67 // Hack: IE11 uses the previous field value for the checkValidity() check if it's called in the onChange handler 68 setTimeout( validate_form, 0 ); 69 } ); 70 validate_form(); 71 72 var generate_cart = function( callback ) { 73 var data = { 74 'nonce': wc_ppec_generate_cart_context.generate_cart_nonce, 75 'attributes': {}, 76 }; 77 78 var field_pairs = form.serializeArray(); 79 80 for ( var i = 0; i < field_pairs.length; i++ ) { 81 // Prevent the default WooCommerce PHP form handler from recognizing this as an "add to cart" call 82 if ( 'add-to-cart' === field_pairs[ i ].name ) { 83 field_pairs[ i ].name = 'ppec-add-to-cart'; 84 } 85 86 // Save attributes as a separate prop in `data` object, 87 // so that `attributes` can be used later on when adding a variable product to cart 88 if ( -1 !== field_pairs[ i ].name.indexOf( 'attribute_' ) ) { 89 data.attributes[ field_pairs[ i ].name ] = field_pairs[ i ].value; 90 continue; 91 } 92 93 data[ field_pairs[ i ].name ] = field_pairs[ i ].value; 94 } 95 96 // If this is a simple product, the "Submit" button has the product ID as "value", we need to include it explicitly 97 data[ 'ppec-add-to-cart' ] = $( '[name=add-to-cart]' ).val(); 98 99 $.ajax( { 100 type: 'POST', 101 data: data, 102 url: wc_ppec_generate_cart_context.ajaxurl, 103 success: callback, 104 } ); 105 }; 106 107 window.wc_ppec_generate_cart = generate_cart; 108 109 // Non-SPB mode click handler, namespaced as 'legacy' as it's replaced by `payment` callback of Button API. 110 $( '#woo_pp_ec_button_product' ).on( 'click.legacy', function( event ) { 64 111 event.preventDefault(); 65 112 … … 68 115 } 69 116 70 toggle_button_availability( false ); 71 72 var data = { 73 'nonce': wc_ppec_context.generate_cart_nonce, 74 'qty': $( '.quantity .qty' ).val(), 75 'attributes': $( '.variations_form' ).length ? get_attributes().data : [], 76 'add-to-cart': $( '[name=add-to-cart]' ).val(), 77 }; 117 $( '#woo_pp_ec_button_product' ).trigger( 'disable' ); 78 118 79 119 var href = $(this).attr( 'href' ); 80 120 81 $.ajax( { 82 type: 'POST', 83 data: data, 84 url: wc_ppec_context.ajaxurl, 85 success: function( response ) { 86 window.location.href = href; 87 } 121 generate_cart( function() { 122 window.location.href = href; 88 123 } ); 89 124 } );
Note: See TracChangeset
for help on using the changeset viewer.