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-19 20:26:27 +00:00
|
|
|
// Utility functions for the file.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a url for a given AJAX endpoint.
|
|
|
|
*
|
|
|
|
* @param {String} endpoint The AJAX Endpoint
|
|
|
|
* @return {String} The URL to use for the request
|
|
|
|
*/
|
2016-02-11 20:13:01 +00:00
|
|
|
var get_url = function( endpoint ) {
|
|
|
|
return wc_cart_params.wc_ajax_url.toString().replace(
|
|
|
|
'%%endpoint%%',
|
|
|
|
endpoint
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-02-19 20:26:27 +00:00
|
|
|
/**
|
|
|
|
* Check if a node is blocked for processing.
|
|
|
|
*
|
|
|
|
* @param {JQuery Object} $node
|
|
|
|
* @return {bool} True if the DOM Element is UI Blocked, false if not.
|
|
|
|
*/
|
2016-02-11 19:01:20 +00:00
|
|
|
var is_blocked = function( $node ) {
|
2016-06-24 11:18:31 +00:00
|
|
|
return $node.is( '.processing' ) || $node.parents( '.processing' ).length;
|
2016-02-11 19:01:20 +00:00
|
|
|
};
|
|
|
|
|
2016-02-19 20:26:27 +00:00
|
|
|
/**
|
|
|
|
* Block a node visually for processing.
|
|
|
|
*
|
|
|
|
* @param {JQuery Object} $node
|
|
|
|
*/
|
2016-02-11 19:01:20 +00:00
|
|
|
var block = function( $node ) {
|
2016-06-24 11:18:31 +00:00
|
|
|
if ( ! is_blocked( $node ) ) {
|
|
|
|
$node.addClass( 'processing' ).block( {
|
|
|
|
message: null,
|
|
|
|
overlayCSS: {
|
|
|
|
background: '#fff',
|
|
|
|
opacity: 0.6
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
2016-02-11 19:01:20 +00:00
|
|
|
};
|
|
|
|
|
2016-02-19 20:26:27 +00:00
|
|
|
/**
|
|
|
|
* Unblock a node after processing is complete.
|
|
|
|
*
|
|
|
|
* @param {JQuery Object} $node
|
|
|
|
*/
|
2016-02-11 19:01:20 +00:00
|
|
|
var unblock = function( $node ) {
|
|
|
|
$node.removeClass( 'processing' ).unblock();
|
|
|
|
};
|
|
|
|
|
2016-02-19 20:26:27 +00:00
|
|
|
/**
|
|
|
|
* Update the .woocommerce div with a string of html.
|
|
|
|
*
|
|
|
|
* @param {String} html_str The HTML string with which to replace the div.
|
2016-12-15 16:37:58 +00:00
|
|
|
* @param {bool} preserve_notices Should notices be kept? False by default.
|
2016-02-19 20:26:27 +00:00
|
|
|
*/
|
2016-12-15 16:37:58 +00:00
|
|
|
var update_wc_div = function( html_str, preserve_notices ) {
|
2016-06-01 11:45:14 +00:00
|
|
|
var $html = $.parseHTML( html_str );
|
2016-11-11 16:00:32 +00:00
|
|
|
var $new_form = $( '.woocommerce-cart-form', $html );
|
2016-06-01 11:45:14 +00:00
|
|
|
var $new_totals = $( '.cart_totals', $html );
|
2016-11-11 16:00:32 +00:00
|
|
|
var $notices = $( '.woocommerce-error, .woocommerce-message, .woocommerce-info', $html );
|
2016-04-08 17:02:45 +00:00
|
|
|
|
2016-11-11 16:00:32 +00:00
|
|
|
// No form, cannot do this.
|
|
|
|
if ( $( '.woocommerce-cart-form' ).length === 0 ) {
|
2018-12-03 17:42:33 +00:00
|
|
|
window.location.reload();
|
2016-11-11 16:00:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-05-03 16:03:43 +00:00
|
|
|
|
|
|
|
// Remove errors
|
2016-12-15 16:37:58 +00:00
|
|
|
if ( ! preserve_notices ) {
|
|
|
|
$( '.woocommerce-error, .woocommerce-message, .woocommerce-info' ).remove();
|
|
|
|
}
|
2016-05-03 16:03:43 +00:00
|
|
|
|
2016-04-20 04:36:48 +00:00
|
|
|
if ( $new_form.length === 0 ) {
|
2016-06-27 10:42:28 +00:00
|
|
|
// If the checkout is also displayed on this page, trigger reload instead.
|
|
|
|
if ( $( '.woocommerce-checkout' ).length ) {
|
2018-12-19 15:19:47 +00:00
|
|
|
window.location.reload();
|
2016-06-27 10:42:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:21:32 +00:00
|
|
|
// No items to display now! Replace all cart content.
|
|
|
|
var $cart_html = $( '.cart-empty', $html ).closest( '.woocommerce' );
|
2016-11-11 16:00:32 +00:00
|
|
|
$( '.woocommerce-cart-form__contents' ).closest( '.woocommerce' ).replaceWith( $cart_html );
|
2016-05-03 16:03:43 +00:00
|
|
|
|
2016-06-27 10:42:28 +00:00
|
|
|
// Display errors
|
2016-11-11 16:00:32 +00:00
|
|
|
if ( $notices.length > 0 ) {
|
2018-04-19 17:27:46 +00:00
|
|
|
show_notice( $notices );
|
2016-08-31 17:07:21 +00:00
|
|
|
}
|
2019-01-15 18:44:28 +00:00
|
|
|
|
|
|
|
// Notify plugins that the cart was emptied.
|
2019-01-16 14:36:35 +00:00
|
|
|
$( document.body ).trigger( 'wc_cart_emptied' );
|
2016-04-20 04:36:48 +00:00
|
|
|
} else {
|
2016-06-27 10:42:28 +00:00
|
|
|
// If the checkout is also displayed on this page, trigger update event.
|
|
|
|
if ( $( '.woocommerce-checkout' ).length ) {
|
|
|
|
$( document.body ).trigger( 'update_checkout' );
|
|
|
|
}
|
2016-07-04 10:10:02 +00:00
|
|
|
|
2016-11-11 16:00:32 +00:00
|
|
|
$( '.woocommerce-cart-form' ).replaceWith( $new_form );
|
2020-06-08 20:05:27 +00:00
|
|
|
$( '.woocommerce-cart-form' ).find( ':input[name="update_cart"]' ).prop( 'disabled', true ).attr( 'aria-disabled', true );
|
2016-04-20 10:21:32 +00:00
|
|
|
|
2016-11-11 16:00:32 +00:00
|
|
|
if ( $notices.length > 0 ) {
|
|
|
|
show_notice( $notices );
|
2016-08-31 17:07:21 +00:00
|
|
|
}
|
2016-06-15 14:51:53 +00:00
|
|
|
|
|
|
|
update_cart_totals_div( $new_totals );
|
2016-04-20 04:36:48 +00:00
|
|
|
}
|
2016-06-01 11:45:14 +00:00
|
|
|
|
|
|
|
$( document.body ).trigger( 'updated_wc_div' );
|
2016-02-15 04:55:47 +00:00
|
|
|
};
|
2013-12-04 19:15:24 +00:00
|
|
|
|
2016-06-15 14:51:53 +00:00
|
|
|
/**
|
|
|
|
* Update the .cart_totals div with a string of html.
|
|
|
|
*
|
|
|
|
* @param {String} html_str The HTML string with which to replace the div.
|
|
|
|
*/
|
|
|
|
var update_cart_totals_div = function( html_str ) {
|
|
|
|
$( '.cart_totals' ).replaceWith( html_str );
|
|
|
|
$( document.body ).trigger( 'updated_cart_totals' );
|
|
|
|
};
|
|
|
|
|
2016-02-22 16:15:47 +00:00
|
|
|
/**
|
2018-04-19 17:27:46 +00:00
|
|
|
* Shows new notices on the page.
|
2016-02-22 16:15:47 +00:00
|
|
|
*
|
|
|
|
* @param {Object} The Notice HTML Element in string or object form.
|
|
|
|
*/
|
2016-05-03 16:03:43 +00:00
|
|
|
var show_notice = function( html_element, $target ) {
|
|
|
|
if ( ! $target ) {
|
2020-05-05 03:49:29 +00:00
|
|
|
$target = $( '.woocommerce-notices-wrapper:first' ) ||
|
|
|
|
$( '.cart-empty' ).closest( '.woocommerce' ) ||
|
|
|
|
$( '.woocommerce-cart-form' );
|
2016-05-03 16:03:43 +00:00
|
|
|
}
|
2018-04-19 17:27:46 +00:00
|
|
|
$target.prepend( html_element );
|
2016-02-22 16:15:47 +00:00
|
|
|
};
|
|
|
|
|
2016-02-19 20:13:48 +00:00
|
|
|
|
|
|
|
/**
|
2016-02-19 20:33:28 +00:00
|
|
|
* Object to handle AJAX calls for cart shipping changes.
|
2016-02-19 20:13:48 +00:00
|
|
|
*/
|
2016-02-19 20:33:28 +00:00
|
|
|
var cart_shipping = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize event handlers and UI state.
|
|
|
|
*/
|
2016-04-19 17:55:36 +00:00
|
|
|
init: function( cart ) {
|
2016-11-11 16:00:32 +00:00
|
|
|
this.cart = cart;
|
2021-01-07 22:16:59 +00:00
|
|
|
this.toggle_shipping = this.toggle_shipping.bind( this );
|
|
|
|
this.shipping_method_selected = this.shipping_method_selected.bind( this );
|
|
|
|
this.shipping_calculator_submit = this.shipping_calculator_submit.bind( this );
|
2016-02-22 16:15:47 +00:00
|
|
|
|
2016-02-19 20:33:28 +00:00
|
|
|
$( document ).on(
|
|
|
|
'click',
|
|
|
|
'.shipping-calculator-button',
|
|
|
|
this.toggle_shipping
|
|
|
|
);
|
|
|
|
$( document ).on(
|
|
|
|
'change',
|
2018-01-18 16:31:17 +00:00
|
|
|
'select.shipping_method, :input[name^=shipping_method]',
|
2016-02-19 20:33:28 +00:00
|
|
|
this.shipping_method_selected
|
|
|
|
);
|
|
|
|
$( document ).on(
|
|
|
|
'submit',
|
|
|
|
'form.woocommerce-shipping-calculator',
|
|
|
|
this.shipping_calculator_submit
|
|
|
|
);
|
|
|
|
|
|
|
|
$( '.shipping-calculator-form' ).hide();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggle Shipping Calculator panel
|
|
|
|
*/
|
|
|
|
toggle_shipping: function() {
|
|
|
|
$( '.shipping-calculator-form' ).slideToggle( 'slow' );
|
2017-07-10 12:14:27 +00:00
|
|
|
$( document.body ).trigger( 'country_to_state_changed' ); // Trigger select2 to load.
|
2016-02-19 20:33:28 +00:00
|
|
|
return false;
|
|
|
|
},
|
2016-02-19 20:13:48 +00:00
|
|
|
|
2016-02-19 20:33:28 +00:00
|
|
|
/**
|
|
|
|
* Handles when a shipping method is selected.
|
|
|
|
*/
|
2017-11-15 22:10:06 +00:00
|
|
|
shipping_method_selected: function() {
|
2016-05-24 00:34:43 +00:00
|
|
|
var shipping_methods = {};
|
2012-12-28 13:02:12 +00:00
|
|
|
|
2020-05-05 03:49:29 +00:00
|
|
|
// eslint-disable-next-line max-len
|
2018-01-18 16:31:17 +00:00
|
|
|
$( 'select.shipping_method, :input[name^=shipping_method][type=radio]:checked, :input[name^=shipping_method][type=hidden]' ).each( function() {
|
2017-11-02 16:15:29 +00:00
|
|
|
shipping_methods[ $( this ).data( 'index' ) ] = $( this ).val();
|
2016-02-19 20:33:28 +00:00
|
|
|
} );
|
2012-12-28 13:02:12 +00:00
|
|
|
|
2016-02-19 20:33:28 +00:00
|
|
|
block( $( 'div.cart_totals' ) );
|
2012-12-28 13:02:12 +00:00
|
|
|
|
2016-02-19 20:33:28 +00:00
|
|
|
var data = {
|
|
|
|
security: wc_cart_params.update_shipping_method_nonce,
|
|
|
|
shipping_method: shipping_methods
|
|
|
|
};
|
2016-02-15 16:52:25 +00:00
|
|
|
|
2016-06-15 14:51:53 +00:00
|
|
|
$.ajax( {
|
|
|
|
type: 'post',
|
|
|
|
url: get_url( 'update_shipping_method' ),
|
|
|
|
data: data,
|
|
|
|
dataType: 'html',
|
|
|
|
success: function( response ) {
|
|
|
|
update_cart_totals_div( response );
|
|
|
|
},
|
|
|
|
complete: function() {
|
|
|
|
unblock( $( 'div.cart_totals' ) );
|
|
|
|
$( document.body ).trigger( 'updated_shipping_method' );
|
|
|
|
}
|
2016-02-19 20:33:28 +00:00
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a shipping calculator form submit.
|
|
|
|
*
|
|
|
|
* @param {Object} evt The JQuery event.
|
|
|
|
*/
|
|
|
|
shipping_calculator_submit: function( evt ) {
|
|
|
|
evt.preventDefault();
|
|
|
|
|
2016-06-15 17:02:58 +00:00
|
|
|
var $form = $( evt.currentTarget );
|
2016-02-19 20:33:28 +00:00
|
|
|
|
2016-06-01 11:45:14 +00:00
|
|
|
block( $( 'div.cart_totals' ) );
|
2016-06-24 11:18:31 +00:00
|
|
|
block( $form );
|
2016-02-19 20:33:28 +00:00
|
|
|
|
|
|
|
// Provide the submit button value because wc-form-handler expects it.
|
|
|
|
$( '<input />' ).attr( 'type', 'hidden' )
|
2016-06-15 14:51:53 +00:00
|
|
|
.attr( 'name', 'calc_shipping' )
|
|
|
|
.attr( 'value', 'x' )
|
|
|
|
.appendTo( $form );
|
2016-02-19 20:33:28 +00:00
|
|
|
|
|
|
|
// Make call to actual form post URL.
|
|
|
|
$.ajax( {
|
|
|
|
type: $form.attr( 'method' ),
|
|
|
|
url: $form.attr( 'action' ),
|
|
|
|
data: $form.serialize(),
|
|
|
|
dataType: 'html',
|
|
|
|
success: function( response ) {
|
2016-06-01 11:45:14 +00:00
|
|
|
update_wc_div( response );
|
2016-02-19 20:33:28 +00:00
|
|
|
},
|
|
|
|
complete: function() {
|
|
|
|
unblock( $form );
|
2016-06-01 11:45:14 +00:00
|
|
|
unblock( $( 'div.cart_totals' ) );
|
2016-02-19 20:33:28 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
2016-02-19 20:13:48 +00:00
|
|
|
};
|
|
|
|
|
2016-02-19 20:26:27 +00:00
|
|
|
/**
|
2016-02-22 16:15:47 +00:00
|
|
|
* Object to handle cart UI.
|
2016-02-19 20:26:27 +00:00
|
|
|
*/
|
2016-02-22 16:15:47 +00:00
|
|
|
var cart = {
|
|
|
|
/**
|
|
|
|
* Initialize cart UI events.
|
|
|
|
*/
|
|
|
|
init: function() {
|
2021-01-07 22:16:59 +00:00
|
|
|
this.update_cart_totals = this.update_cart_totals.bind( this );
|
|
|
|
this.input_keypress = this.input_keypress.bind( this );
|
|
|
|
this.cart_submit = this.cart_submit.bind( this );
|
|
|
|
this.submit_click = this.submit_click.bind( this );
|
|
|
|
this.apply_coupon = this.apply_coupon.bind( this );
|
|
|
|
this.remove_coupon_clicked = this.remove_coupon_clicked.bind( this );
|
|
|
|
this.quantity_update = this.quantity_update.bind( this );
|
|
|
|
this.item_remove_clicked = this.item_remove_clicked.bind( this );
|
|
|
|
this.item_restore_clicked = this.item_restore_clicked.bind( this );
|
|
|
|
this.update_cart = this.update_cart.bind( this );
|
2016-02-11 06:26:41 +00:00
|
|
|
|
2016-06-01 11:45:14 +00:00
|
|
|
$( document ).on(
|
2019-04-12 11:14:54 +00:00
|
|
|
'wc_update_cart added_to_cart',
|
2017-08-16 10:34:35 +00:00
|
|
|
function() { cart.update_cart.apply( cart, [].slice.call( arguments, 1 ) ); } );
|
2016-05-02 20:26:14 +00:00
|
|
|
$( document ).on(
|
|
|
|
'click',
|
2018-01-18 16:31:17 +00:00
|
|
|
'.woocommerce-cart-form :input[type=submit]',
|
2016-05-02 20:26:14 +00:00
|
|
|
this.submit_click );
|
2016-09-22 20:52:23 +00:00
|
|
|
$( document ).on(
|
|
|
|
'keypress',
|
2018-01-18 16:31:17 +00:00
|
|
|
'.woocommerce-cart-form :input[type=number]',
|
2016-09-22 20:52:23 +00:00
|
|
|
this.input_keypress );
|
2016-02-22 16:15:47 +00:00
|
|
|
$( document ).on(
|
|
|
|
'submit',
|
2016-11-11 16:00:32 +00:00
|
|
|
'.woocommerce-cart-form',
|
2016-02-22 16:15:47 +00:00
|
|
|
this.cart_submit );
|
|
|
|
$( document ).on(
|
|
|
|
'click',
|
|
|
|
'a.woocommerce-remove-coupon',
|
|
|
|
this.remove_coupon_clicked );
|
|
|
|
$( document ).on(
|
|
|
|
'click',
|
2016-11-11 16:00:32 +00:00
|
|
|
'.woocommerce-cart-form .product-remove > a',
|
2016-02-22 16:15:47 +00:00
|
|
|
this.item_remove_clicked );
|
2017-07-04 09:01:59 +00:00
|
|
|
$( document ).on(
|
|
|
|
'click',
|
|
|
|
'.woocommerce-cart .restore-item',
|
|
|
|
this.item_restore_clicked );
|
2016-04-20 10:10:39 +00:00
|
|
|
$( document ).on(
|
|
|
|
'change input',
|
2016-11-11 16:00:32 +00:00
|
|
|
'.woocommerce-cart-form .cart_item :input',
|
2016-04-20 10:10:39 +00:00
|
|
|
this.input_changed );
|
|
|
|
|
2020-06-08 20:05:27 +00:00
|
|
|
$( '.woocommerce-cart-form :input[name="update_cart"]' ).prop( 'disabled', true ).attr( 'aria-disabled', true );
|
2016-04-20 10:10:39 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-05-02 20:26:14 +00:00
|
|
|
* After an input is changed, enable the update cart button.
|
2016-04-20 10:10:39 +00:00
|
|
|
*/
|
|
|
|
input_changed: function() {
|
2020-06-08 20:05:27 +00:00
|
|
|
$( '.woocommerce-cart-form :input[name="update_cart"]' ).prop( 'disabled', false ).attr( 'aria-disabled', false );
|
2016-02-22 16:15:47 +00:00
|
|
|
},
|
|
|
|
|
2016-06-01 11:45:14 +00:00
|
|
|
/**
|
|
|
|
* Update entire cart via ajax.
|
|
|
|
*/
|
2016-12-15 16:37:58 +00:00
|
|
|
update_cart: function( preserve_notices ) {
|
2016-11-11 16:00:32 +00:00
|
|
|
var $form = $( '.woocommerce-cart-form' );
|
2016-06-01 11:45:14 +00:00
|
|
|
|
|
|
|
block( $form );
|
|
|
|
block( $( 'div.cart_totals' ) );
|
|
|
|
|
|
|
|
// Make call to actual form post URL.
|
|
|
|
$.ajax( {
|
|
|
|
type: $form.attr( 'method' ),
|
|
|
|
url: $form.attr( 'action' ),
|
|
|
|
data: $form.serialize(),
|
|
|
|
dataType: 'html',
|
|
|
|
success: function( response ) {
|
2016-12-15 16:37:58 +00:00
|
|
|
update_wc_div( response, preserve_notices );
|
2016-06-01 11:45:14 +00:00
|
|
|
},
|
|
|
|
complete: function() {
|
|
|
|
unblock( $form );
|
|
|
|
unblock( $( 'div.cart_totals' ) );
|
2018-04-12 11:49:34 +00:00
|
|
|
$.scroll_to_notices( $( '[role="alert"]' ) );
|
2016-06-01 11:45:14 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
2016-02-22 16:15:47 +00:00
|
|
|
/**
|
|
|
|
* Update the cart after something has changed.
|
|
|
|
*/
|
|
|
|
update_cart_totals: function() {
|
|
|
|
block( $( 'div.cart_totals' ) );
|
|
|
|
|
|
|
|
$.ajax( {
|
|
|
|
url: get_url( 'get_cart_totals' ),
|
|
|
|
dataType: 'html',
|
2016-06-15 14:51:53 +00:00
|
|
|
success: function( response ) {
|
|
|
|
update_cart_totals_div( response );
|
|
|
|
},
|
|
|
|
complete: function() {
|
|
|
|
unblock( $( 'div.cart_totals' ) );
|
2016-02-22 16:15:47 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
2016-09-22 20:52:23 +00:00
|
|
|
/**
|
|
|
|
* Handle the <ENTER> key for quantity fields.
|
|
|
|
*
|
|
|
|
* @param {Object} evt The JQuery event
|
|
|
|
*
|
|
|
|
* For IE, if you hit enter on a quantity field, it makes the
|
|
|
|
* document.activeElement the first submit button it finds.
|
|
|
|
* For us, that is the Apply Coupon button. This is required
|
|
|
|
* to catch the event before that happens.
|
|
|
|
*/
|
|
|
|
input_keypress: function( evt ) {
|
|
|
|
|
|
|
|
// Catch the enter key and don't let it submit the form.
|
|
|
|
if ( 13 === evt.keyCode ) {
|
2018-05-08 11:40:13 +00:00
|
|
|
var $form = $( evt.currentTarget ).parents( 'form' );
|
|
|
|
|
|
|
|
try {
|
|
|
|
// If there are no validation errors, handle the submit.
|
|
|
|
if ( $form[0].checkValidity() ) {
|
|
|
|
evt.preventDefault();
|
|
|
|
this.cart_submit( evt );
|
|
|
|
}
|
|
|
|
} catch( err ) {
|
|
|
|
evt.preventDefault();
|
|
|
|
this.cart_submit( evt );
|
|
|
|
}
|
2016-09-22 20:52:23 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-02-22 16:15:47 +00:00
|
|
|
/**
|
|
|
|
* Handle cart form submit and route to correct logic.
|
|
|
|
*
|
|
|
|
* @param {Object} evt The JQuery event
|
|
|
|
*/
|
|
|
|
cart_submit: function( evt ) {
|
2018-01-18 16:31:17 +00:00
|
|
|
var $submit = $( document.activeElement ),
|
|
|
|
$clicked = $( ':input[type=submit][clicked=true]' ),
|
|
|
|
$form = $( evt.currentTarget );
|
2016-09-22 20:52:23 +00:00
|
|
|
|
|
|
|
// For submit events, currentTarget is form.
|
|
|
|
// For keypress events, currentTarget is input.
|
|
|
|
if ( ! $form.is( 'form' ) ) {
|
|
|
|
$form = $( evt.currentTarget ).parents( 'form' );
|
|
|
|
}
|
2016-02-22 16:15:47 +00:00
|
|
|
|
2016-11-11 16:00:32 +00:00
|
|
|
if ( 0 === $form.find( '.woocommerce-cart-form__contents' ).length ) {
|
2016-08-01 15:19:30 +00:00
|
|
|
return;
|
2016-04-06 17:14:54 +00:00
|
|
|
}
|
2016-08-01 15:19:30 +00:00
|
|
|
|
2016-02-22 16:15:47 +00:00
|
|
|
if ( is_blocked( $form ) ) {
|
|
|
|
return false;
|
2016-02-11 06:26:41 +00:00
|
|
|
}
|
|
|
|
|
2018-01-18 16:31:17 +00:00
|
|
|
if ( $clicked.is( ':input[name="update_cart"]' ) || $submit.is( 'input.qty' ) ) {
|
2016-06-15 19:13:46 +00:00
|
|
|
evt.preventDefault();
|
2016-02-22 16:15:47 +00:00
|
|
|
this.quantity_update( $form );
|
2016-02-11 18:51:29 +00:00
|
|
|
|
2018-01-18 16:31:17 +00:00
|
|
|
} else if ( $clicked.is( ':input[name="apply_coupon"]' ) || $submit.is( '#coupon_code' ) ) {
|
2016-06-15 19:13:46 +00:00
|
|
|
evt.preventDefault();
|
2016-02-22 16:15:47 +00:00
|
|
|
this.apply_coupon( $form );
|
|
|
|
}
|
|
|
|
},
|
2016-02-11 18:51:29 +00:00
|
|
|
|
2016-05-02 20:26:14 +00:00
|
|
|
/**
|
|
|
|
* Special handling to identify which submit button was clicked.
|
|
|
|
*
|
|
|
|
* @param {Object} evt The JQuery event
|
|
|
|
*/
|
|
|
|
submit_click: function( evt ) {
|
2018-01-18 16:31:17 +00:00
|
|
|
$( ':input[type=submit]', $( evt.target ).parents( 'form' ) ).removeAttr( 'clicked' );
|
2016-05-02 20:26:14 +00:00
|
|
|
$( evt.target ).attr( 'clicked', 'true' );
|
|
|
|
},
|
|
|
|
|
2016-02-22 16:15:47 +00:00
|
|
|
/**
|
|
|
|
* Apply Coupon code
|
|
|
|
*
|
|
|
|
* @param {JQuery Object} $form The cart form.
|
|
|
|
*/
|
|
|
|
apply_coupon: function( $form ) {
|
|
|
|
block( $form );
|
2016-02-11 06:26:41 +00:00
|
|
|
|
2016-02-22 16:15:47 +00:00
|
|
|
var cart = this;
|
|
|
|
var $text_field = $( '#coupon_code' );
|
|
|
|
var coupon_code = $text_field.val();
|
2016-02-15 16:27:47 +00:00
|
|
|
|
2016-02-22 16:15:47 +00:00
|
|
|
var data = {
|
|
|
|
security: wc_cart_params.apply_coupon_nonce,
|
|
|
|
coupon_code: coupon_code
|
|
|
|
};
|
2016-02-11 19:01:20 +00:00
|
|
|
|
2016-02-22 16:15:47 +00:00
|
|
|
$.ajax( {
|
|
|
|
type: 'POST',
|
|
|
|
url: get_url( 'apply_coupon' ),
|
|
|
|
data: data,
|
|
|
|
dataType: 'html',
|
|
|
|
success: function( response ) {
|
2016-10-26 16:24:35 +00:00
|
|
|
$( '.woocommerce-error, .woocommerce-message, .woocommerce-info' ).remove();
|
2016-02-22 16:15:47 +00:00
|
|
|
show_notice( response );
|
2017-02-21 10:35:11 +00:00
|
|
|
$( document.body ).trigger( 'applied_coupon', [ coupon_code ] );
|
2016-02-22 16:15:47 +00:00
|
|
|
},
|
|
|
|
complete: function() {
|
|
|
|
unblock( $form );
|
|
|
|
$text_field.val( '' );
|
2016-12-15 16:37:58 +00:00
|
|
|
cart.update_cart( true );
|
2016-02-22 16:15:47 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
},
|
2016-02-11 19:01:20 +00:00
|
|
|
|
2016-02-22 16:15:47 +00:00
|
|
|
/**
|
|
|
|
* Handle when a remove coupon link is clicked.
|
|
|
|
*
|
|
|
|
* @param {Object} evt The JQuery event
|
|
|
|
*/
|
|
|
|
remove_coupon_clicked: function( evt ) {
|
|
|
|
evt.preventDefault();
|
2016-02-15 16:27:47 +00:00
|
|
|
|
2016-11-11 16:00:32 +00:00
|
|
|
var cart = this;
|
|
|
|
var $wrapper = $( evt.currentTarget ).closest( '.cart_totals' );
|
|
|
|
var coupon = $( evt.currentTarget ).attr( 'data-coupon' );
|
2016-02-15 16:27:47 +00:00
|
|
|
|
2016-11-11 16:00:32 +00:00
|
|
|
block( $wrapper );
|
2016-02-11 19:55:34 +00:00
|
|
|
|
2016-02-22 16:15:47 +00:00
|
|
|
var data = {
|
|
|
|
security: wc_cart_params.remove_coupon_nonce,
|
|
|
|
coupon: coupon
|
|
|
|
};
|
2016-02-15 03:57:14 +00:00
|
|
|
|
2016-02-22 16:15:47 +00:00
|
|
|
$.ajax( {
|
|
|
|
type: 'POST',
|
|
|
|
url: get_url( 'remove_coupon' ),
|
|
|
|
data: data,
|
|
|
|
dataType: 'html',
|
|
|
|
success: function( response ) {
|
2016-10-26 16:24:35 +00:00
|
|
|
$( '.woocommerce-error, .woocommerce-message, .woocommerce-info' ).remove();
|
2016-02-22 16:15:47 +00:00
|
|
|
show_notice( response );
|
2017-02-17 11:58:15 +00:00
|
|
|
$( document.body ).trigger( 'removed_coupon', [ coupon ] );
|
2016-11-11 16:00:32 +00:00
|
|
|
unblock( $wrapper );
|
2016-02-22 16:15:47 +00:00
|
|
|
},
|
|
|
|
complete: function() {
|
2016-12-15 16:37:58 +00:00
|
|
|
cart.update_cart( true );
|
2016-02-22 16:15:47 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
},
|
2016-02-15 04:55:47 +00:00
|
|
|
|
2016-02-22 16:15:47 +00:00
|
|
|
/**
|
|
|
|
* Handle a cart Quantity Update
|
|
|
|
*
|
|
|
|
* @param {JQuery Object} $form The cart form.
|
|
|
|
*/
|
|
|
|
quantity_update: function( $form ) {
|
|
|
|
block( $form );
|
2016-06-01 11:45:14 +00:00
|
|
|
block( $( 'div.cart_totals' ) );
|
2016-02-19 20:13:48 +00:00
|
|
|
|
2017-01-25 19:20:38 +00:00
|
|
|
// 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 );
|
|
|
|
|
2016-02-22 16:15:47 +00:00
|
|
|
// Make call to actual form post URL.
|
|
|
|
$.ajax( {
|
|
|
|
type: $form.attr( 'method' ),
|
|
|
|
url: $form.attr( 'action' ),
|
|
|
|
data: $form.serialize(),
|
|
|
|
dataType: 'html',
|
2017-01-25 19:20:38 +00:00
|
|
|
success: function( response ) {
|
|
|
|
update_wc_div( response );
|
|
|
|
},
|
2016-02-22 16:15:47 +00:00
|
|
|
complete: function() {
|
|
|
|
unblock( $form );
|
2016-06-01 11:45:14 +00:00
|
|
|
unblock( $( 'div.cart_totals' ) );
|
2018-04-12 11:34:34 +00:00
|
|
|
$.scroll_to_notices( $( '[role="alert"]' ) );
|
2016-02-22 16:15:47 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle when a remove item link is clicked.
|
|
|
|
*
|
|
|
|
* @param {Object} evt The JQuery event
|
|
|
|
*/
|
|
|
|
item_remove_clicked: function( evt ) {
|
|
|
|
evt.preventDefault();
|
|
|
|
|
2016-06-15 17:02:58 +00:00
|
|
|
var $a = $( evt.currentTarget );
|
2016-02-22 16:15:47 +00:00
|
|
|
var $form = $a.parents( 'form' );
|
|
|
|
|
|
|
|
block( $form );
|
2016-06-01 11:45:14 +00:00
|
|
|
block( $( 'div.cart_totals' ) );
|
2016-02-22 16:15:47 +00:00
|
|
|
|
|
|
|
$.ajax( {
|
|
|
|
type: 'GET',
|
|
|
|
url: $a.attr( 'href' ),
|
|
|
|
dataType: 'html',
|
2017-07-04 19:35:23 +00:00
|
|
|
success: function( response ) {
|
|
|
|
update_wc_div( response );
|
|
|
|
},
|
2016-02-22 16:15:47 +00:00
|
|
|
complete: function() {
|
|
|
|
unblock( $form );
|
2016-06-01 11:45:14 +00:00
|
|
|
unblock( $( 'div.cart_totals' ) );
|
2018-04-12 11:34:34 +00:00
|
|
|
$.scroll_to_notices( $( '[role="alert"]' ) );
|
2016-02-22 16:15:47 +00:00
|
|
|
}
|
|
|
|
} );
|
2017-07-04 09:01:59 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle when a restore item link is clicked.
|
|
|
|
*
|
|
|
|
* @param {Object} evt The JQuery event
|
|
|
|
*/
|
|
|
|
item_restore_clicked: function( evt ) {
|
|
|
|
evt.preventDefault();
|
|
|
|
|
|
|
|
var $a = $( evt.currentTarget );
|
|
|
|
var $form = $( 'form.woocommerce-cart-form' );
|
|
|
|
|
|
|
|
block( $form );
|
|
|
|
block( $( 'div.cart_totals' ) );
|
|
|
|
|
2016-02-22 16:15:47 +00:00
|
|
|
$.ajax( {
|
|
|
|
type: 'GET',
|
|
|
|
url: $a.attr( 'href' ),
|
|
|
|
dataType: 'html',
|
2017-07-04 08:33:51 +00:00
|
|
|
success: function( response ) {
|
|
|
|
update_wc_div( response );
|
|
|
|
},
|
2016-02-22 16:15:47 +00:00
|
|
|
complete: function() {
|
|
|
|
unblock( $form );
|
2016-06-01 11:45:14 +00:00
|
|
|
unblock( $( 'div.cart_totals' ) );
|
2016-02-22 16:15:47 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
};
|
2016-02-19 20:33:28 +00:00
|
|
|
|
2016-04-19 17:55:36 +00:00
|
|
|
cart_shipping.init( cart );
|
2016-02-22 16:15:47 +00:00
|
|
|
cart.init();
|
2016-02-15 14:40:46 +00:00
|
|
|
} );
|