woocommerce/assets/js/frontend/add-to-cart.js

215 lines
5.6 KiB
JavaScript
Raw Normal View History

/* global wc_add_to_cart_params */
jQuery( function( $ ) {
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
}
/**
* 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 );
$( 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 )
.on( 'added_to_cart', this.updateButton )
.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 );
};
2019-04-12 10:58:49 +00:00
/**
* Add add to cart event.
*/
AddToCartHandler.prototype.addRequest = function( request ) {
this.requests.push( request );
if ( 1 === this.requests.length ) {
2019-04-12 10:58:49 +00:00
this.run();
}
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;
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-04-12 10:58:49 +00:00
$.ajax( this.requests[0] );
2019-07-15 10:59:06 +00:00
};
2019-04-12 10:58:49 +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
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
e.preventDefault();
2014-09-09 11:43:56 +00:00
$thisbutton.removeClass( 'added' );
$thisbutton.addClass( 'loading' );
2012-12-10 13:11:05 +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 ] ) ) {
$( document.body ).trigger( 'ajax_request_not_sent.adding_to_cart', [ false, false, $thisbutton ] );
return true;
}
var data = {};
2012-12-10 13:11:05 +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 ) {
data[ key ] = value;
2014-09-09 11:43:56 +00:00
});
2012-12-10 13:11:05 +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'
});
}
};
2013-04-15 11:36:04 +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() {
window.location = $thisbutton.attr( 'href' );
return;
2019-04-12 11:05:07 +00:00
},
dataType: 'json'
});
};
/**
* Update cart page elements after add to cart events.
*/
AddToCartHandler.prototype.updateButton = function( e, fragments, cart_hash, $button ) {
$button = typeof $button === 'undefined' ? false : $button;
2012-12-10 13:11:05 +00:00
if ( $button ) {
$button.removeClass( 'loading' );
2020-05-11 23:00:23 +00:00
if ( fragments ) {
$button.addClass( 'added' );
}
2012-12-10 13:11:05 +00:00
// View cart text.
if ( fragments && ! wc_add_to_cart_params.is_cart && $button.parent().find( '.added_to_cart' ).length === 0 ) {
2020-08-24 02:40:43 +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
$( document.body ).trigger( 'wc_cart_button_updated', [ $button ] );
2014-09-09 11:43:56 +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' );
}
};
/**
* Init AddToCartHandler.
*/
new AddToCartHandler();
});