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

119 lines
3.0 KiB
JavaScript
Raw Normal View History

jQuery( function( $ ) {
2014-09-09 11:43:56 +00:00
// wc_add_to_cart_params is required to continue, ensure the object exists
if ( typeof wc_add_to_cart_params === 'undefined' )
return false;
2014-09-09 11:43:56 +00:00
// Ajax add to cart
$( document ).on( 'click', '.add_to_cart_button', function(e) {
2012-12-10 13:11:05 +00:00
2014-09-09 11:43:56 +00:00
// AJAX add to cart request
var $thisbutton = $( this );
2012-12-10 13:11:05 +00:00
2014-09-09 11:43:56 +00:00
if ( $thisbutton.is( '.product_type_simple' ) ) {
2012-12-10 13:11:05 +00:00
2014-09-09 11:43:56 +00:00
if ( ! $thisbutton.attr( 'data-product_id' ) )
return true;
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
2014-09-09 11:43:56 +00:00
var data = {
action: 'woocommerce_add_to_cart',
};
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;
});
2012-12-10 13:11:05 +00:00
2014-09-09 11:43:56 +00:00
// Trigger event
$( 'body' ).trigger( 'adding_to_cart', [ $thisbutton, data ] );
2013-01-22 16:31:54 +00:00
2014-09-09 11:43:56 +00:00
// Ajax action
$.post( wc_add_to_cart_params.ajax_url, data, function( response ) {
2012-12-10 13:11:05 +00:00
2014-09-09 11:43:56 +00:00
if ( ! response )
return;
2012-12-10 13:11:05 +00:00
2014-09-09 11:43:56 +00:00
var this_page = window.location.toString();
2012-12-10 13:11:05 +00:00
2014-09-09 11:43:56 +00:00
this_page = this_page.replace( 'add-to-cart', 'added-to-cart' );
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 {
2014-09-09 11:43:56 +00:00
$thisbutton.removeClass( 'loading' );
2014-09-09 11:43:56 +00:00
fragments = response.fragments;
cart_hash = response.cart_hash;
2012-12-10 13:11:05 +00:00
2014-09-09 11:43:56 +00:00
// Block fragments class
if ( fragments ) {
$.each( fragments, function( key, value ) {
$( key ).addClass( 'updating' );
});
}
2014-09-09 11:43:56 +00:00
// Block widgets and fragments
$( '.shop_table.cart, .updating, .cart_totals' ).fadeTo( '400', '0.6' ).block({
message: null,
overlayCSS: {
opacity: 0.6
}
});
2013-04-15 11:36:04 +00:00
2014-09-09 11:43:56 +00:00
// Changes button classes
$thisbutton.addClass( 'added' );
2012-12-10 13:11:05 +00:00
2014-09-09 11:43:56 +00:00
// View cart text
if ( ! wc_add_to_cart_params.is_cart && $thisbutton.parent().find( '.added_to_cart' ).size() === 0 ) {
$thisbutton.after( ' <a href="' + wc_add_to_cart_params.cart_url + '" class="added_to_cart wc-forward" title="' +
2014-09-09 11:43:56 +00:00
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
2014-09-09 11:43:56 +00:00
// Replace fragments
if ( fragments ) {
$.each( fragments, function( key, value ) {
$( key ).replaceWith( value );
});
}
2012-12-10 13:11:05 +00:00
2014-09-09 11:43:56 +00:00
// Unblock
$( '.widget_shopping_cart, .updating' ).stop( true ).css( 'opacity', '1' ).unblock();
2012-12-10 13:11:05 +00:00
2014-09-09 11:43:56 +00:00
// Cart page elements
$( '.shop_table.cart' ).load( this_page + ' .shop_table.cart:eq(0) > *', function() {
2012-12-10 13:11:05 +00:00
2014-09-09 11:43:56 +00:00
$( '.shop_table.cart' ).stop( true ).css( 'opacity', '1' ).unblock();
2014-09-09 11:43:56 +00:00
$( 'body' ).trigger( 'cart_page_refreshed' );
});
2014-09-09 11:43:56 +00:00
$( '.cart_totals' ).load( this_page + ' .cart_totals:eq(0) > *', function() {
$( '.cart_totals' ).stop( true ).css( 'opacity', '1' ).unblock();
});
2012-12-10 13:11:05 +00:00
2014-09-09 11:43:56 +00:00
// Trigger event so themes can refresh other areas
$( 'body' ).trigger( 'added_to_cart', [ fragments, cart_hash, $thisbutton ] );
2014-09-09 11:43:56 +00:00
}
});
2012-12-10 13:11:05 +00:00
2014-09-09 11:43:56 +00:00
return false;
2012-12-10 13:11:05 +00:00
2014-09-09 11:43:56 +00:00
}
2014-09-09 11:43:56 +00:00
return true;
});
});