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() {
|
2017-10-25 15:52:31 +00:00
|
|
|
$( document.body )
|
2017-06-05 11:43:52 +00:00
|
|
|
.on( 'click', '.add_to_cart_button', this.onAddToCart )
|
2017-07-13 11:15:13 +00:00
|
|
|
.on( 'click', '.remove_from_cart_button', this.onRemoveFromCart )
|
2017-06-05 11:43:52 +00:00
|
|
|
.on( 'added_to_cart', this.updateButton )
|
|
|
|
.on( 'added_to_cart', this.updateCartPage )
|
2017-07-13 11:15:13 +00:00
|
|
|
.on( 'added_to_cart removed_from_cart', this.updateFragments );
|
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
|
|
|
|
2015-03-20 12:28:26 +00:00
|
|
|
var data = {};
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2014-09-09 11:43:56 +00:00
|
|
|
$.each( $thisbutton.data(), 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
|
|
|
|
2016-11-17 17:08:19 +00:00
|
|
|
// Ajax action.
|
2015-07-27 16:55:37 +00:00
|
|
|
$.post( wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ), data, function( response ) {
|
2015-07-10 05:13:30 +00:00
|
|
|
if ( ! response ) {
|
2014-09-09 11:43:56 +00:00
|
|
|
return;
|
2015-07-10 05:13:30 +00:00
|
|
|
}
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2014-09-09 11:43:56 +00:00
|
|
|
if ( response.error && response.product_url ) {
|
|
|
|
window.location = response.product_url;
|
|
|
|
return;
|
|
|
|
}
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2014-09-09 11:43:56 +00:00
|
|
|
// 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;
|
2016-11-17 16:05:46 +00:00
|
|
|
}
|
2013-01-28 10:59:19 +00:00
|
|
|
|
2017-06-05 11:43:52 +00:00
|
|
|
// Trigger event so themes can refresh other areas.
|
|
|
|
$( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, $thisbutton ] );
|
|
|
|
});
|
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
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$.post( wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'remove_from_cart' ), { cart_item_key : $thisbutton.data( 'cart_item_key' ) }, function( response ) {
|
|
|
|
if ( ! response || ! response.fragments ) {
|
|
|
|
window.location = $thisbutton.attr( 'href' );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$( document.body ).trigger( 'removed_from_cart', [ response.fragments, response.cart_hash ] );
|
|
|
|
}).fail( function() {
|
|
|
|
window.location = $thisbutton.attr( 'href' );
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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' );
|
|
|
|
$button.addClass( 'added' );
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2016-11-17 17:08:19 +00:00
|
|
|
// View cart text.
|
|
|
|
if ( ! wc_add_to_cart_params.is_cart && $button.parent().find( '.added_to_cart' ).length === 0 ) {
|
|
|
|
$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 cart page elements after add to cart events.
|
|
|
|
*/
|
|
|
|
AddToCartHandler.prototype.updateCartPage = function() {
|
|
|
|
var page = window.location.toString().replace( 'add-to-cart', 'added-to-cart' );
|
2016-11-17 16:05:46 +00:00
|
|
|
|
2016-11-17 17:08:19 +00:00
|
|
|
$( '.shop_table.cart' ).load( page + ' .shop_table.cart:eq(0) > *', function() {
|
2016-11-17 16:05:46 +00:00
|
|
|
$( '.shop_table.cart' ).stop( true ).css( 'opacity', '1' ).unblock();
|
|
|
|
$( document.body ).trigger( 'cart_page_refreshed' );
|
|
|
|
});
|
|
|
|
|
2016-11-17 17:08:19 +00:00
|
|
|
$( '.cart_totals' ).load( page + ' .cart_totals:eq(0) > *', function() {
|
2016-11-17 16:05:46 +00:00
|
|
|
$( '.cart_totals' ).stop( true ).css( 'opacity', '1' ).unblock();
|
2017-06-05 11:43:52 +00:00
|
|
|
$( document.body ).trigger( 'cart_totals_refreshed' );
|
2016-11-17 16:05:46 +00:00
|
|
|
});
|
2017-06-05 11:43:52 +00:00
|
|
|
};
|
2016-11-17 16:05:46 +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
|
|
|
});
|