2016-11-17 17:08:19 +00:00
|
|
|
/* global wc_add_to_cart_params */
|
2014-03-17 05:26:55 +00:00
|
|
|
jQuery( function( $ ) {
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2015-07-10 05:13:30 +00:00
|
|
|
if ( typeof wc_add_to_cart_params === 'undefined' ) {
|
2014-09-09 11:43:56 +00:00
|
|
|
return false;
|
2015-07-10 05:13:30 +00:00
|
|
|
}
|
2013-12-04 19:15:24 +00:00
|
|
|
|
2017-06-05 11:43:52 +00:00
|
|
|
/**
|
|
|
|
* AddToCartHandler class.
|
|
|
|
*/
|
|
|
|
var AddToCartHandler = function() {
|
2019-04-12 10:58:49 +00:00
|
|
|
this.requests = [];
|
|
|
|
this.addRequest = this.addRequest.bind( this );
|
|
|
|
this.run = this.run.bind( this );
|
|
|
|
|
2017-10-25 15:52:31 +00:00
|
|
|
$( document.body )
|
2019-04-12 10:58:49 +00:00
|
|
|
.on( 'click', '.add_to_cart_button', { addToCartHandler: this }, this.onAddToCart )
|
2019-04-12 11:05:07 +00:00
|
|
|
.on( 'click', '.remove_from_cart_button', { addToCartHandler: this }, this.onRemoveFromCart )
|
2017-06-05 11:43:52 +00:00
|
|
|
.on( 'added_to_cart', this.updateButton )
|
2020-03-14 00:27:45 +00:00
|
|
|
.on( 'ajax_request_not_sent.adding_to_cart', this.updateButton )
|
2019-04-12 11:05:07 +00:00
|
|
|
.on( 'added_to_cart removed_from_cart', { addToCartHandler: this }, this.updateFragments );
|
2017-06-05 11:43:52 +00:00
|
|
|
};
|
|
|
|
|
2019-04-12 10:58:49 +00:00
|
|
|
/**
|
|
|
|
* Add add to cart event.
|
|
|
|
*/
|
|
|
|
AddToCartHandler.prototype.addRequest = function( request ) {
|
|
|
|
this.requests.push( request );
|
|
|
|
|
2019-07-12 13:56:10 +00:00
|
|
|
if ( 1 === this.requests.length ) {
|
2019-04-12 10:58:49 +00:00
|
|
|
this.run();
|
2019-07-12 13:56:10 +00:00
|
|
|
}
|
2019-07-15 10:59:06 +00:00
|
|
|
};
|
2019-04-12 10:58:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Run add to cart events.
|
|
|
|
*/
|
2019-07-15 10:59:06 +00:00
|
|
|
AddToCartHandler.prototype.run = function() {
|
2019-04-12 10:58:49 +00:00
|
|
|
var requestManager = this,
|
|
|
|
originalCallback = requestManager.requests[0].complete;
|
|
|
|
|
2019-07-12 13:56:10 +00:00
|
|
|
requestManager.requests[0].complete = function() {
|
2019-04-12 10:58:49 +00:00
|
|
|
if ( typeof originalCallback === 'function' ) {
|
|
|
|
originalCallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
requestManager.requests.shift();
|
|
|
|
|
|
|
|
if ( requestManager.requests.length > 0 ) {
|
|
|
|
requestManager.run();
|
|
|
|
}
|
2019-07-12 13:56:10 +00:00
|
|
|
};
|
2019-04-12 10:58:49 +00:00
|
|
|
|
2019-07-12 13:56:10 +00:00
|
|
|
$.ajax( this.requests[0] );
|
2019-07-15 10:59:06 +00:00
|
|
|
};
|
2019-04-12 10:58:49 +00:00
|
|
|
|
2017-06-05 11:43:52 +00:00
|
|
|
/**
|
|
|
|
* Handle the add to cart event.
|
|
|
|
*/
|
|
|
|
AddToCartHandler.prototype.onAddToCart = function( e ) {
|
2014-09-09 11:43:56 +00:00
|
|
|
var $thisbutton = $( this );
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2015-10-30 17:54:49 +00:00
|
|
|
if ( $thisbutton.is( '.ajax_add_to_cart' ) ) {
|
2015-07-10 05:13:30 +00:00
|
|
|
if ( ! $thisbutton.attr( 'data-product_id' ) ) {
|
2014-09-09 11:43:56 +00:00
|
|
|
return true;
|
2015-07-10 05:13:30 +00:00
|
|
|
}
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2017-06-05 11:43:52 +00:00
|
|
|
e.preventDefault();
|
|
|
|
|
2014-09-09 11:43:56 +00:00
|
|
|
$thisbutton.removeClass( 'added' );
|
|
|
|
$thisbutton.addClass( 'loading' );
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2020-03-14 00:26:37 +00:00
|
|
|
// Allow 3rd parties to validate and quit early.
|
2020-05-11 23:00:23 +00:00
|
|
|
if ( false === $( document.body ).triggerHandler( 'should_send_ajax_request.adding_to_cart', [ $thisbutton ] ) ) {
|
2020-03-14 00:26:37 +00:00
|
|
|
$( document.body ).trigger( 'ajax_request_not_sent.adding_to_cart', [ false, false, $thisbutton ] );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-20 12:28:26 +00:00
|
|
|
var data = {};
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2020-02-21 10:17:31 +00:00
|
|
|
// Fetch changes that are directly added by calling $thisbutton.data( key, value )
|
|
|
|
$.each( $thisbutton.data(), function( key, value ) {
|
|
|
|
data[ key ] = value;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Fetch data attributes in $thisbutton. Give preference to data-attributes because they can be directly modified by javascript
|
|
|
|
// while `.data` are jquery specific memory stores.
|
2019-10-12 17:04:46 +00:00
|
|
|
$.each( $thisbutton[0].dataset, function( key, value ) {
|
2016-11-17 17:08:19 +00:00
|
|
|
data[ key ] = value;
|
2014-09-09 11:43:56 +00:00
|
|
|
});
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2016-11-17 17:08:19 +00:00
|
|
|
// Trigger event.
|
2015-04-13 15:37:22 +00:00
|
|
|
$( document.body ).trigger( 'adding_to_cart', [ $thisbutton, data ] );
|
2013-01-22 16:31:54 +00:00
|
|
|
|
2019-04-12 10:58:49 +00:00
|
|
|
e.data.addToCartHandler.addRequest({
|
|
|
|
type: 'POST',
|
|
|
|
url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ),
|
|
|
|
data: data,
|
|
|
|
success: function( response ) {
|
|
|
|
if ( ! response ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( response.error && response.product_url ) {
|
|
|
|
window.location = response.product_url;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect to cart option
|
|
|
|
if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) {
|
|
|
|
window.location = wc_add_to_cart_params.cart_url;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trigger event so themes can refresh other areas.
|
|
|
|
$( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, $thisbutton ] );
|
|
|
|
},
|
|
|
|
dataType: 'json'
|
2017-06-05 11:43:52 +00:00
|
|
|
});
|
2016-11-17 16:05:46 +00:00
|
|
|
}
|
2017-06-05 11:43:52 +00:00
|
|
|
};
|
2013-04-15 11:36:04 +00:00
|
|
|
|
2017-07-13 11:15:13 +00:00
|
|
|
/**
|
|
|
|
* Update fragments after remove from cart event in mini-cart.
|
|
|
|
*/
|
|
|
|
AddToCartHandler.prototype.onRemoveFromCart = function( e ) {
|
|
|
|
var $thisbutton = $( this ),
|
|
|
|
$row = $thisbutton.closest( '.woocommerce-mini-cart-item' );
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
$row.block({
|
|
|
|
message: null,
|
|
|
|
overlayCSS: {
|
|
|
|
opacity: 0.6
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-04-12 11:05:07 +00:00
|
|
|
e.data.addToCartHandler.addRequest({
|
|
|
|
type: 'POST',
|
|
|
|
url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'remove_from_cart' ),
|
|
|
|
data: {
|
|
|
|
cart_item_key : $thisbutton.data( 'cart_item_key' )
|
|
|
|
},
|
|
|
|
success: function( response ) {
|
|
|
|
if ( ! response || ! response.fragments ) {
|
|
|
|
window.location = $thisbutton.attr( 'href' );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$( document.body ).trigger( 'removed_from_cart', [ response.fragments, response.cart_hash, $thisbutton ] );
|
|
|
|
},
|
|
|
|
error: function() {
|
2017-07-13 11:15:13 +00:00
|
|
|
window.location = $thisbutton.attr( 'href' );
|
|
|
|
return;
|
2019-04-12 11:05:07 +00:00
|
|
|
},
|
|
|
|
dataType: 'json'
|
2017-07-13 11:15:13 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-06-05 11:43:52 +00:00
|
|
|
/**
|
|
|
|
* Update cart page elements after add to cart events.
|
|
|
|
*/
|
|
|
|
AddToCartHandler.prototype.updateButton = function( e, fragments, cart_hash, $button ) {
|
2016-11-17 17:08:19 +00:00
|
|
|
$button = typeof $button === 'undefined' ? false : $button;
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2016-11-17 17:08:19 +00:00
|
|
|
if ( $button ) {
|
|
|
|
$button.removeClass( 'loading' );
|
2020-03-14 00:27:45 +00:00
|
|
|
|
2020-05-11 23:00:23 +00:00
|
|
|
if ( fragments ) {
|
2020-03-14 00:27:45 +00:00
|
|
|
$button.addClass( 'added' );
|
|
|
|
}
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2016-11-17 17:08:19 +00:00
|
|
|
// View cart text.
|
2020-03-14 00:27:45 +00:00
|
|
|
if ( fragments && ! wc_add_to_cart_params.is_cart && $button.parent().find( '.added_to_cart' ).length === 0 ) {
|
2016-11-17 17:08:19 +00:00
|
|
|
$button.after( ' <a href="' + wc_add_to_cart_params.cart_url + '" class="added_to_cart wc-forward" title="' +
|
|
|
|
wc_add_to_cart_params.i18n_view_cart + '">' + wc_add_to_cart_params.i18n_view_cart + '</a>' );
|
|
|
|
}
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2017-06-05 11:43:52 +00:00
|
|
|
$( document.body ).trigger( 'wc_cart_button_updated', [ $button ] );
|
2014-09-09 11:43:56 +00:00
|
|
|
}
|
2017-06-05 11:43:52 +00:00
|
|
|
};
|
2014-09-09 11:41:14 +00:00
|
|
|
|
2017-06-05 11:43:52 +00:00
|
|
|
/**
|
|
|
|
* Update fragments after add to cart events.
|
|
|
|
*/
|
|
|
|
AddToCartHandler.prototype.updateFragments = function( e, fragments ) {
|
|
|
|
if ( fragments ) {
|
|
|
|
$.each( fragments, function( key ) {
|
|
|
|
$( key )
|
|
|
|
.addClass( 'updating' )
|
|
|
|
.fadeTo( '400', '0.6' )
|
|
|
|
.block({
|
|
|
|
message: null,
|
|
|
|
overlayCSS: {
|
|
|
|
opacity: 0.6
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$.each( fragments, function( key, value ) {
|
|
|
|
$( key ).replaceWith( value );
|
|
|
|
$( key ).stop( true ).css( 'opacity', '1' ).unblock();
|
|
|
|
});
|
|
|
|
|
|
|
|
$( document.body ).trigger( 'wc_fragments_loaded' );
|
|
|
|
}
|
|
|
|
};
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2017-06-05 11:43:52 +00:00
|
|
|
/**
|
|
|
|
* Init AddToCartHandler.
|
|
|
|
*/
|
|
|
|
new AddToCartHandler();
|
2013-12-04 19:15:24 +00:00
|
|
|
});
|