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-15 04:55:47 +00:00
|
|
|
// wc_cart_params is required to continue, ensure the object exists
|
|
|
|
if ( typeof wc_cart_params === 'undefined' ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2016-02-15 14:40:46 +00:00
|
|
|
} );
|
2016-02-11 19:01:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Unblock a node after processing is complete.
|
|
|
|
var unblock = function( $node ) {
|
|
|
|
$node.removeClass( 'processing' ).unblock();
|
|
|
|
};
|
|
|
|
|
2016-02-15 04:55:47 +00:00
|
|
|
// 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 );
|
2016-02-15 14:10:25 +00:00
|
|
|
$( 'div.woocommerce' ).replaceWith( $new_div );
|
2016-02-15 04:55:47 +00:00
|
|
|
};
|
2013-12-04 19:15:24 +00:00
|
|
|
|
2016-02-19 20:13:48 +00:00
|
|
|
/**
|
|
|
|
* Toggle Shipping Calculator panel
|
|
|
|
*/
|
|
|
|
var toggle_shipping = function() {
|
2014-03-18 03:08:41 +00:00
|
|
|
$( '.shipping-calculator-form' ).slideToggle( 'slow' );
|
2012-07-17 14:09:18 +00:00
|
|
|
return false;
|
2016-02-19 20:13:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles when a shipping method is selected.
|
|
|
|
*/
|
|
|
|
var shipping_method_selected = function( evt ) {
|
|
|
|
var target = evt.target;
|
|
|
|
|
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() {
|
2016-02-19 20:13:48 +00:00
|
|
|
shipping_methods[ $( target ).data( 'index' ) ] = $( target ).val();
|
2016-02-15 14:40:46 +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' );
|
2016-02-15 14:40:46 +00:00
|
|
|
} );
|
2016-02-19 20:13:48 +00:00
|
|
|
};
|
2012-12-28 13:02:12 +00:00
|
|
|
|
2016-02-19 20:13:48 +00:00
|
|
|
var shipping_calculator_submit = function( evt ) {
|
2016-02-15 16:52:25 +00:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
} );
|
2016-02-19 20:13:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$( document ).on( 'click', '.shipping-calculator-button', toggle_shipping );
|
|
|
|
$( document ).on(
|
|
|
|
'change', 'select.shipping_method, input[name^=shipping_method]',
|
|
|
|
shipping_method_selected
|
|
|
|
);
|
|
|
|
$( document ).on(
|
|
|
|
'submit',
|
|
|
|
'form.woocommerce-shipping-calculator',
|
|
|
|
shipping_calculator_submit
|
|
|
|
);
|
2016-02-15 16:52:25 +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-15 14:40:46 +00:00
|
|
|
} );
|
2016-02-11 06:26:41 +00:00
|
|
|
};
|
|
|
|
|
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-15 16:27:47 +00:00
|
|
|
// Handle form submit and route to correct logic.
|
2016-02-19 20:13:48 +00:00
|
|
|
var cart_submit = function( evt ) {
|
2016-02-11 06:26:41 +00:00
|
|
|
evt.preventDefault();
|
|
|
|
|
2016-02-15 16:27:47 +00:00
|
|
|
var $form = $( evt.target );
|
|
|
|
var $submit = $( document.activeElement );
|
|
|
|
|
|
|
|
window.console.log( $submit );
|
2016-02-11 19:01:20 +00:00
|
|
|
|
|
|
|
if ( is_blocked( $form ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-15 16:27:47 +00:00
|
|
|
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 );
|
|
|
|
}
|
2016-02-19 20:13:48 +00:00
|
|
|
};
|
2016-02-15 16:27:47 +00:00
|
|
|
|
|
|
|
// Coupon code
|
|
|
|
var apply_coupon = function( $form ) {
|
2016-02-11 19:01:20 +00:00
|
|
|
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-15 14:40:46 +00:00
|
|
|
} );
|
2016-02-15 16:27:47 +00:00
|
|
|
};
|
2016-02-11 19:55:34 +00:00
|
|
|
|
2016-02-19 20:13:48 +00:00
|
|
|
var remove_coupon_clicked = function( evt ) {
|
2016-02-11 19:55:34 +00:00
|
|
|
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();
|
|
|
|
}
|
2016-02-15 14:40:46 +00:00
|
|
|
} );
|
2016-02-19 20:13:48 +00:00
|
|
|
};
|
2016-02-15 03:57:14 +00:00
|
|
|
|
|
|
|
// Quantity Update
|
2016-02-15 16:27:47 +00:00
|
|
|
var quantity_update = function( $form ) {
|
2016-02-15 03:57:14 +00:00
|
|
|
|
|
|
|
// Provide the submit button value because wc-form-handler expects it.
|
2016-02-15 14:10:25 +00:00
|
|
|
$( '<input />' ).attr( 'type', 'hidden' )
|
|
|
|
.attr( 'name', 'update_cart' )
|
|
|
|
.attr( 'value', 'Update Cart' )
|
|
|
|
.appendTo( $form );
|
|
|
|
|
|
|
|
block( $form );
|
2016-02-15 03:57:14 +00:00
|
|
|
|
|
|
|
// Make call to actual form post URL.
|
|
|
|
$.ajax( {
|
|
|
|
type: $form.attr( 'method' ),
|
|
|
|
url: $form.attr( 'action' ),
|
|
|
|
data: $form.serialize(),
|
|
|
|
dataType: 'html',
|
2016-02-15 14:10:25 +00:00
|
|
|
success: update_wc_div,
|
|
|
|
complete: function() {
|
|
|
|
unblock( $form );
|
|
|
|
}
|
2016-02-15 14:40:46 +00:00
|
|
|
} );
|
2016-02-15 16:27:47 +00:00
|
|
|
};
|
2016-02-15 04:55:47 +00:00
|
|
|
|
|
|
|
// Item Remove
|
2016-02-19 20:13:48 +00:00
|
|
|
var item_remove_clicked = function( evt ) {
|
2016-02-15 04:55:47 +00:00
|
|
|
evt.preventDefault();
|
|
|
|
|
|
|
|
var $a = $( evt.target );
|
2016-02-15 15:41:58 +00:00
|
|
|
var $form = $a.parents( 'form' );
|
2016-02-15 04:55:47 +00:00
|
|
|
|
2016-02-15 15:41:58 +00:00
|
|
|
block( $form );
|
2016-02-15 14:10:25 +00:00
|
|
|
|
2016-02-15 04:55:47 +00:00
|
|
|
$.ajax( {
|
|
|
|
type: 'GET',
|
|
|
|
url: $a.attr( 'href' ),
|
|
|
|
dataType: 'html',
|
2016-02-15 14:10:25 +00:00
|
|
|
success: update_wc_div,
|
|
|
|
complete: function() {
|
2016-02-15 15:41:58 +00:00
|
|
|
unblock( $form );
|
2016-02-15 14:10:25 +00:00
|
|
|
}
|
2016-02-15 14:40:46 +00:00
|
|
|
} );
|
2016-02-19 20:13:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$( document ).on( 'submit', 'div.woocommerce > form', cart_submit );
|
|
|
|
$( document ).on( 'click', 'a.woocommerce-remove-coupon', remove_coupon_clicked );
|
|
|
|
$( document ).on( 'click', 'td.product-remove > a', item_remove_clicked );
|
2016-02-15 14:40:46 +00:00
|
|
|
} );
|