woocommerce/assets/js/frontend/cart.js

97 lines
2.5 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;
}
// Shipping calculator
$( document ).on( 'click', '.shipping-calculator-button', function() {
$( '.shipping-calculator-form' ).slideToggle( 'slow' );
return false;
2014-01-16 07:06:23 +00:00
}).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
$( 'div.cart_totals' ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
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( wc_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', '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
$( '.shipping-calculator-form' ).hide();
// Update the cart after something has changed.
var update_cart_totals = function() {
$( 'div.cart_totals' ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
var url = wc_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'get_cart_totals' );
$.ajax( {
url: url,
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 );
};
// Coupon code
$( '[name="apply_coupon"]' ).on( 'click', function( evt ) {
evt.preventDefault();
var $text_field = $( '#coupon_code' );
var coupon_code = $text_field.val();
$text_field.val( '' );
var url = wc_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'apply_coupon' );
var data = {
security: wc_cart_params.apply_coupon_nonce,
coupon_code: coupon_code
};
$.ajax( {
type: 'POST',
url: url,
data: data,
dataType: 'html',
success: function( response ) {
show_notice( response );
},
complete: function() {
update_cart_totals();
}
});
});
});