2015-06-10 17:26:21 +00:00
|
|
|
/* global wc_cart_params */
|
2014-03-18 03:08:41 +00:00
|
|
|
jQuery( function( $ ) {
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2016-02-11 20:13:01 +00:00
|
|
|
// Gets a url for a given AJAX endpoint.
|
|
|
|
var get_url = function( endpoint ) {
|
|
|
|
return wc_cart_params.wc_ajax_url.toString().replace(
|
|
|
|
'%%endpoint%%',
|
|
|
|
endpoint
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-02-11 19:01:20 +00:00
|
|
|
// 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();
|
|
|
|
};
|
|
|
|
|
2013-12-04 19:15:24 +00:00
|
|
|
// wc_cart_params is required to continue, ensure the object exists
|
2014-03-18 03:08:41 +00:00
|
|
|
if ( typeof wc_cart_params === 'undefined' ) {
|
2013-12-04 19:15:24 +00:00
|
|
|
return false;
|
2014-03-18 03:08:41 +00:00
|
|
|
}
|
2013-12-04 19:15:24 +00:00
|
|
|
|
2012-07-17 14:09:18 +00:00
|
|
|
// Shipping calculator
|
2014-03-18 03:08:41 +00:00
|
|
|
$( document ).on( 'click', '.shipping-calculator-button', function() {
|
|
|
|
$( '.shipping-calculator-form' ).slideToggle( 'slow' );
|
2012-07-17 14:09:18 +00:00
|
|
|
return false;
|
2014-01-16 07:06:23 +00:00
|
|
|
}).on( 'change', 'select.shipping_method, input[name^=shipping_method]', function() {
|
2013-08-14 20:00:34 +00:00
|
|
|
var shipping_methods = [];
|
|
|
|
|
2015-06-10 17:26:21 +00:00
|
|
|
$( 'select.shipping_method, input[name^=shipping_method][type=radio]:checked, input[name^=shipping_method][type=hidden]' ).each( function() {
|
2014-03-18 03:08:41 +00:00
|
|
|
shipping_methods[ $( this ).data( 'index' ) ] = $( this ).val();
|
2015-06-10 17:26:21 +00:00
|
|
|
});
|
2012-12-28 13:02:12 +00:00
|
|
|
|
2016-02-11 19:01:20 +00:00
|
|
|
block( $( 'div.cart_totals' ) );
|
2012-12-28 13:02:12 +00:00
|
|
|
|
2012-07-17 14:09:18 +00:00
|
|
|
var data = {
|
2014-03-18 03:08:41 +00:00
|
|
|
security: wc_cart_params.update_shipping_method_nonce,
|
|
|
|
shipping_method: shipping_methods
|
2012-07-17 14:09:18 +00:00
|
|
|
};
|
2012-12-28 13:02:12 +00:00
|
|
|
|
2016-02-11 20:13:01 +00:00
|
|
|
$.post( get_url( 'update_shipping_method' ), data, function( response ) {
|
2014-03-18 03:08:41 +00:00
|
|
|
$( 'div.cart_totals' ).replaceWith( response );
|
2015-04-13 15:37:22 +00:00
|
|
|
$( document.body ).trigger( 'updated_shipping_method' );
|
2012-07-17 14:09:18 +00:00
|
|
|
});
|
2014-03-18 03:08:41 +00:00
|
|
|
});
|
2012-12-28 13:02:12 +00:00
|
|
|
|
2014-03-18 03:08:41 +00:00
|
|
|
$( '.shipping-calculator-form' ).hide();
|
2016-02-11 06:26:41 +00:00
|
|
|
|
|
|
|
// Update the cart after something has changed.
|
|
|
|
var update_cart_totals = function() {
|
2016-02-11 19:01:20 +00:00
|
|
|
block( $( 'div.cart_totals' ) );
|
2016-02-11 06:26:41 +00:00
|
|
|
|
|
|
|
$.ajax( {
|
2016-02-11 20:13:01 +00:00
|
|
|
url: get_url( 'get_cart_totals' ),
|
|
|
|
dataType: 'html',
|
2016-02-11 06:26:41 +00:00
|
|
|
success: function( response ) {
|
|
|
|
$( 'div.cart_totals' ).replaceWith( response );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-02-11 20:13:01 +00:00
|
|
|
// clears previous notices and shows new one above form.
|
2016-02-11 18:51:29 +00:00
|
|
|
var show_notice = function( html_element ) {
|
|
|
|
var $form = $( 'div.woocommerce > form' );
|
|
|
|
|
|
|
|
$( '.woocommerce-error, .woocommerce-message' ).remove();
|
|
|
|
$form.before( html_element );
|
|
|
|
};
|
|
|
|
|
2016-02-11 06:26:41 +00:00
|
|
|
// Coupon code
|
|
|
|
$( '[name="apply_coupon"]' ).on( 'click', function( evt ) {
|
|
|
|
evt.preventDefault();
|
|
|
|
|
2016-02-11 19:01:20 +00:00
|
|
|
var $form = $( 'div.woocommerce > form' );
|
|
|
|
|
|
|
|
if ( is_blocked( $form ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
block( $form );
|
|
|
|
|
2016-02-11 18:51:29 +00:00
|
|
|
var $text_field = $( '#coupon_code' );
|
|
|
|
var coupon_code = $text_field.val();
|
2016-02-11 06:26:41 +00:00
|
|
|
|
|
|
|
var data = {
|
|
|
|
security: wc_cart_params.apply_coupon_nonce,
|
|
|
|
coupon_code: coupon_code
|
|
|
|
};
|
|
|
|
|
|
|
|
$.ajax( {
|
2016-02-11 18:51:29 +00:00
|
|
|
type: 'POST',
|
2016-02-11 20:13:01 +00:00
|
|
|
url: get_url( 'apply_coupon' ),
|
2016-02-11 18:51:29 +00:00
|
|
|
data: data,
|
|
|
|
dataType: 'html',
|
2016-02-11 06:26:41 +00:00
|
|
|
success: function( response ) {
|
2016-02-11 18:51:29 +00:00
|
|
|
show_notice( response );
|
2016-02-11 06:26:41 +00:00
|
|
|
},
|
|
|
|
complete: function() {
|
2016-02-11 19:01:20 +00:00
|
|
|
unblock( $form );
|
|
|
|
$text_field.val( '' );
|
2016-02-11 06:26:41 +00:00
|
|
|
update_cart_totals();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2016-02-11 19:55:34 +00:00
|
|
|
|
|
|
|
$( 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',
|
2016-02-11 20:13:01 +00:00
|
|
|
url: get_url( 'remove_coupon' ),
|
2016-02-11 19:55:34 +00:00
|
|
|
data: data,
|
|
|
|
dataType: 'html',
|
|
|
|
success: function( response ) {
|
|
|
|
show_notice( response );
|
|
|
|
unblock( $tr.parents( 'table' ) );
|
|
|
|
},
|
|
|
|
complete: function() {
|
|
|
|
update_cart_totals();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2013-12-04 19:15:24 +00:00
|
|
|
});
|