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

131 lines
3.2 KiB
JavaScript
Raw Normal View History

/* global wc_add_to_cart_params */
2016-03-12 08:37:02 +00:00
/*!
* WooCommerce Add to Cart JS
*/
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
}
// Ajax add to cart.
2015-07-10 05:13:30 +00:00
$( document ).on( 'click', '.add_to_cart_button', function() {
2012-12-10 13:11:05 +00:00
// AJAX add to cart request.
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' ) ) {
2012-12-10 13:11:05 +00:00
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
2014-09-09 11:43:56 +00:00
$thisbutton.removeClass( 'added' );
$thisbutton.addClass( 'loading' );
2012-12-10 13:11:05 +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 ) {
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
// 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 ) {
2012-12-10 13:11:05 +00:00
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;
}
2014-09-09 11:43:56 +00:00
// Redirect to cart option
if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) {
2012-12-10 13:11:05 +00:00
2014-09-09 11:43:56 +00:00
window.location = wc_add_to_cart_params.cart_url;
return;
2012-12-10 13:11:05 +00:00
2014-09-09 11:43:56 +00:00
} else {
// Trigger event so themes can refresh other areas
$( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, $thisbutton ] );
}
});
2012-12-10 13:11:05 +00:00
return false;
}
2013-04-15 11:36:04 +00:00
return true;
});
2012-12-10 13:11:05 +00:00
// On "added_to_cart"
$( document.body ).on( 'added_to_cart', function( event, fragments, cart_hash, $button ) {
var page = window.location.toString().replace( 'add-to-cart', 'added-to-cart' );
$button = typeof $button === 'undefined' ? false : $button;
2012-12-10 13:11:05 +00:00
if ( $button ) {
$button.removeClass( 'loading' );
}
2012-12-10 13:11:05 +00:00
// Block fragments class.
if ( fragments ) {
$.each( fragments, function( key ) {
$( key ).addClass( 'updating' );
});
}
2012-12-10 13:11:05 +00:00
// Block widgets and fragments.
$( '.shop_table.cart, .updating, .cart_totals' )
.fadeTo( '400', '0.6' )
.block({
message: null,
overlayCSS: {
opacity: 0.6
}
});
2012-12-10 13:11:05 +00:00
if ( $button ) {
// Changes button classes.
$button.addClass( 'added' );
2012-12-10 13:11:05 +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
// Replace fragments.
if ( fragments ) {
$.each( fragments, function( key, value ) {
$( key ).replaceWith( value );
});
$( document.body ).trigger( 'wc_fragments_loaded' );
2014-09-09 11:43:56 +00:00
}
// Unblock.
$( '.widget_shopping_cart, .updating' ).stop( true ).css( 'opacity', '1' ).unblock();
// Cart page elements.
$( '.shop_table.cart' ).load( page + ' .shop_table.cart:eq(0) > *', function() {
$( '.shop_table.cart' ).stop( true ).css( 'opacity', '1' ).unblock();
$( document.body ).trigger( 'cart_page_refreshed' );
});
$( '.cart_totals' ).load( page + ' .cart_totals:eq(0) > *', function() {
$( '.cart_totals' ).stop( true ).css( 'opacity', '1' ).unblock();
});
2014-09-09 11:43:56 +00:00
});
});