Skip to content

Commit

Permalink
Cart AJAX: Clean up form handling for cart form.
Browse files Browse the repository at this point in the history
The coupon and update cart logic was tripping on each other
between the click handling and form submit handling.
This commit combines the event handler, then splits off from there.
  • Loading branch information
coderkevin committed Feb 15, 2016
1 parent 4b8cf5c commit b330813
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
30 changes: 21 additions & 9 deletions assets/js/frontend/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,31 @@ jQuery( function( $ ) {
$form.before( html_element );
};

// Coupon code
$( '[name="apply_coupon"]' ).on( 'click', function( evt ) {
// Handle form submit and route to correct logic.
$( document ).on( 'submit', 'div.woocommerce > form', function( evt ) {
evt.preventDefault();

var $form = $( 'div.woocommerce > form' );
var $form = $( evt.target );
var $submit = $( document.activeElement );

window.console.log( $submit );

if ( is_blocked( $form ) ) {
return false;
}

if ( $submit.is( '[name="update_cart"]' ) || $submit.is( 'input.qty' ) ) {
window.console.log( 'update cart' );
quantity_update( $form );

} else if ( $submit.is( '[name="apply_coupon"]' ) || $submit.is( '#coupon_code' ) ) {
window.console.log( 'apply coupon' );
apply_coupon( $form );
}
} );

// Coupon code
var apply_coupon = function( $form ) {
block( $form );

var $text_field = $( '#coupon_code' );
Expand All @@ -123,7 +138,7 @@ jQuery( function( $ ) {
update_cart_totals();
}
} );
} );
};

$( document ).on( 'click', 'a.woocommerce-remove-coupon', function( evt ) {
evt.preventDefault();
Expand Down Expand Up @@ -154,10 +169,7 @@ jQuery( function( $ ) {
} );

// Quantity Update
$( document ).on( 'submit', 'div.woocommerce > form', function( evt ) {
evt.preventDefault();

var $form = $( evt.target );
var quantity_update = function( $form ) {

// Provide the submit button value because wc-form-handler expects it.
$( '<input />' ).attr( 'type', 'hidden' )
Expand All @@ -178,7 +190,7 @@ jQuery( function( $ ) {
unblock( $form );
}
} );
} );
};

// Item Remove
$( document ).on( 'click', 'td.product-remove > a', function( evt ) {
Expand Down
2 changes: 1 addition & 1 deletion assets/js/frontend/cart.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b330813

Please sign in to comment.