Plugin Directory

Changeset 2118918


Ignore:
Timestamp:
07/07/2019 02:40:54 PM (7 years ago)
Author:
Artisan-Workshop
Message:

update 1.6.15 asset

Location:
pp-express-wc4jp/trunk/assets
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • pp-express-wc4jp/trunk/assets/css/wc-gateway-ppec-frontend-cart.css

    r1838283 r2118918  
    33    margin: 1em 0;
    44    overflow: hidden;
     5}
     6.wcppec-checkout-buttons .woocommerce-error {
     7    text-align: left;
    58}
    69.wcppec-checkout-buttons__separator {
     
    2932    border: 0 !important;
    3033}
     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 */
    22;(function( $, window, document ) {
    33    'use strict';
    44
     5    // This button state is only applicable to non-SPB click handler below.
    56    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        } );
    614
    7     var toggle_button_availability = function( available ) {
    8         if ( available ) {
    9             button_enabled = true;
     15    $( '#woo_pp_ec_button_product' )
     16        .on( 'enable', function() {
    1017            $( '#woo_pp_ec_button_product' ).css( {
    1118                'cursor': '',
     
    1320                'filter': '',
    1421            } );
    15         } else {
    16             button_enabled = false;
     22            $( '#woo_pp_ec_button_product > *' ).css( 'pointer-events', '' );
     23        } )
     24        .on( 'disable', function() {
    1725            $( '#woo_pp_ec_button_product' ).css( {
    1826                'cursor': 'not-allowed',
     
    2028                'filter': 'grayscale( 100% )',
    2129            } );
    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' );
    4131        } );
    4232
    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();
    4848    };
    4949
    5050    // It's a variations form, button availability should depend on its events
    5151    if ( $( '.variations_form' ).length ) {
    52         toggle_button_availability( false );
     52        variation_valid = false;
    5353
    5454        $( '.variations_form' )
    5555        .on( 'show_variation', function( event, form, purchasable ) {
    56             toggle_button_availability( purchasable );
     56            variation_valid = purchasable;
     57            update_button();
    5758        } )
    5859        .on( 'hide_variation', function() {
    59             toggle_button_availability( false );
     60            variation_valid = false;
     61            update_button();
    6062        } );
    6163    }
    6264
    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 ) {
    64111        event.preventDefault();
    65112
     
    68115        }
    69116
    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' );
    78118
    79119        var href = $(this).attr( 'href' );
    80120
    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;
    88123        } );
    89124    } );
Note: See TracChangeset for help on using the changeset viewer.