woocommerce/assets/js/frontend/cart.js

243 lines
5.9 KiB
JavaScript
Raw Normal View History

/* global wc_cart_params */
jQuery( function( $ ) {
// wc_cart_params is required to continue, ensure the object exists
if ( typeof wc_cart_params === 'undefined' ) {
return false;
}
// Gets a url for a given AJAX endpoint.
var get_url = function( endpoint ) {
return wc_cart_params.wc_ajax_url.toString().replace(
'%%endpoint%%',
endpoint
);
};
// Check if a node is blocked for processing.
var is_blocked = function( $node ) {
return $node.is( '.processing' );
};
// Block a node for processing.
var block = function( $node ) {
$node.addClass( 'processing' ).block( {
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
} );
};
// Unblock a node after processing is complete.
var unblock = function( $node ) {
$node.removeClass( 'processing' ).unblock();
};
// Updates the .woocommerce div with a string of html.
var update_wc_div = function( html_str ) {
var $html = $.parseHTML( html_str );
var $new_div = $( 'div.woocommerce', $html );
$( 'div.woocommerce' ).replaceWith( $new_div );
};
// Shipping calculator
$( document ).on( 'click', '.shipping-calculator-button', function() {
$( '.shipping-calculator-form' ).slideToggle( 'slow' );
return false;
} ).on( 'change', 'select.shipping_method, input[name^=shipping_method]', function() {
var shipping_methods = [];
$( 'select.shipping_method, input[name^=shipping_method][type=radio]:checked, input[name^=shipping_method][type=hidden]' ).each( function() {
shipping_methods[ $( this ).data( 'index' ) ] = $( this ).val();
} );
2012-12-28 13:02:12 +00:00
block( $( 'div.cart_totals' ) );
2012-12-28 13:02:12 +00:00
var data = {
security: wc_cart_params.update_shipping_method_nonce,
shipping_method: shipping_methods
};
2012-12-28 13:02:12 +00:00
$.post( get_url( 'update_shipping_method' ), data, function( response ) {
$( 'div.cart_totals' ).replaceWith( response );
2015-04-13 15:37:22 +00:00
$( document.body ).trigger( 'updated_shipping_method' );
} );
} );
2012-12-28 13:02:12 +00:00
$( document ).on( 'submit', 'form.woocommerce-shipping-calculator', function( evt ) {
evt.preventDefault();
var $form = $( evt.target );
block( $form );
// Provide the submit button value because wc-form-handler expects it.
$( '<input />' ).attr( 'type', 'hidden' )
.attr( 'name', 'calc_shipping' )
.attr( 'value', 'x' )
.appendTo( $form );
// Make call to actual form post URL.
$.ajax( {
type: $form.attr( 'method' ),
url: $form.attr( 'action' ),
data: $form.serialize(),
dataType: 'html',
success: function( response ) {
update_wc_div(response );
},
complete: function() {
unblock( $form );
}
} );
} );
$( '.shipping-calculator-form' ).hide();
// Update the cart after something has changed.
var update_cart_totals = function() {
block( $( 'div.cart_totals' ) );
$.ajax( {
url: get_url( 'get_cart_totals' ),
dataType: 'html',
success: function( response ) {
$( 'div.cart_totals' ).replaceWith( response );
}
} );
};
// clears previous notices and shows new one above form.
var show_notice = function( html_element ) {
var $form = $( 'div.woocommerce > form' );
$( '.woocommerce-error, .woocommerce-message' ).remove();
$form.before( html_element );
};
// Handle form submit and route to correct logic.
$( document ).on( 'submit', 'div.woocommerce > form', function( evt ) {
evt.preventDefault();
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' );
var coupon_code = $text_field.val();
var data = {
security: wc_cart_params.apply_coupon_nonce,
coupon_code: coupon_code
};
$.ajax( {
type: 'POST',
url: get_url( 'apply_coupon' ),
data: data,
dataType: 'html',
success: function( response ) {
show_notice( response );
},
complete: function() {
unblock( $form );
$text_field.val( '' );
update_cart_totals();
}
} );
};
$( document ).on( 'click', 'a.woocommerce-remove-coupon', function( evt ) {
evt.preventDefault();
var $tr = $( this ).parents( 'tr' );
var coupon = $( this ).attr( 'data-coupon' );
block( $tr.parents( 'table' ) );
var data = {
security: wc_cart_params.remove_coupon_nonce,
coupon: coupon
};
$.ajax( {
type: 'POST',
url: get_url( 'remove_coupon' ),
data: data,
dataType: 'html',
success: function( response ) {
show_notice( response );
unblock( $tr.parents( 'table' ) );
},
complete: function() {
update_cart_totals();
}
} );
} );
// Quantity Update
var quantity_update = function( $form ) {
// Provide the submit button value because wc-form-handler expects it.
$( '<input />' ).attr( 'type', 'hidden' )
.attr( 'name', 'update_cart' )
.attr( 'value', 'Update Cart' )
.appendTo( $form );
block( $form );
// Make call to actual form post URL.
$.ajax( {
type: $form.attr( 'method' ),
url: $form.attr( 'action' ),
data: $form.serialize(),
dataType: 'html',
success: update_wc_div,
complete: function() {
unblock( $form );
}
} );
};
// Item Remove
$( document ).on( 'click', 'td.product-remove > a', function( evt ) {
evt.preventDefault();
var $a = $( evt.target );
var $form = $a.parents( 'form' );
block( $form );
$.ajax( {
type: 'GET',
url: $a.attr( 'href' ),
dataType: 'html',
success: update_wc_div,
complete: function() {
unblock( $form );
}
} );
} );
} );