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

107 lines
3.2 KiB
JavaScript
Raw Normal View History

jQuery(document).ready(function($) {
// Ajax add to cart
2012-12-28 13:02:12 +00:00
$(document).on( 'click', '.add_to_cart_button', function() {
2012-12-10 13:11:05 +00:00
// AJAX add to cart request
var $thisbutton = $(this);
2012-12-10 13:11:05 +00:00
if ( $thisbutton.is('.product_type_simple') ) {
2012-12-10 13:11:05 +00:00
if (!$thisbutton.attr('data-product_id'))
return true;
2012-12-10 13:11:05 +00:00
$thisbutton.removeClass('added');
$thisbutton.addClass('loading');
2012-12-10 13:11:05 +00:00
var data = {
action: 'woocommerce_add_to_cart',
product_id: $thisbutton.attr('data-product_id'),
quantity: $thisbutton.attr('data-quantity')
};
2012-12-10 13:11:05 +00:00
// Trigger event
2013-02-01 15:04:10 +00:00
$('body').trigger( 'adding_to_cart', [ $thisbutton, data ] );
2012-12-10 13:11:05 +00:00
// Ajax action
2013-06-11 12:31:41 +00:00
$.post( wc_add_to_cart_params.ajax_url, data, function( response ) {
2013-01-22 16:31:54 +00:00
if ( ! response )
return;
2012-12-10 13:11:05 +00:00
var this_page = window.location.toString();
2012-12-10 13:11:05 +00:00
2012-08-24 17:44:11 +00:00
this_page = this_page.replace( 'add-to-cart', 'added-to-cart' );
2012-12-10 13:11:05 +00:00
2013-01-22 16:31:54 +00:00
if ( response.error && response.product_url ) {
window.location = response.product_url;
2013-01-22 16:31:54 +00:00
return;
}
2012-12-10 13:11:05 +00:00
// Redirect to cart option
2013-06-11 12:31:41 +00:00
if ( wc_add_to_cart_params.cart_redirect_after_add == 'yes' ) {
2013-06-11 12:31:41 +00:00
window.location = wc_add_to_cart_params.cart_url;
return;
2012-12-10 13:11:05 +00:00
} else {
2012-12-10 13:11:05 +00:00
$thisbutton.removeClass('loading');
fragments = response.fragments;
cart_hash = response.cart_hash;
// Block fragments class
if ( fragments ) {
$.each(fragments, function(key, value) {
$(key).addClass('updating');
});
}
2012-12-10 13:11:05 +00:00
// Block widgets and fragments
2013-06-11 12:31:41 +00:00
$('.shop_table.cart, .updating, .cart_totals').fadeTo('400', '0.6').block({message: null, overlayCSS: {background: 'transparent url(' + wc_add_to_cart_params.ajax_loader_url + ') no-repeat center', backgroundSize: '16px 16px', opacity: 0.6 } } );
// Changes button classes
2013-04-15 11:36:04 +00:00
$thisbutton.addClass('added');
// 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="' + 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);
});
}
2012-12-10 13:11:05 +00:00
// Unblock
$('.widget_shopping_cart, .updating').stop(true).css('opacity', '1').unblock();
2012-12-10 13:11:05 +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
$("div.quantity:not(.buttons_added), td.quantity:not(.buttons_added)").addClass('buttons_added').append('<input type="button" value="+" id="add1" class="plus" />').prepend('<input type="button" value="-" id="minus1" class="minus" />');
2012-12-10 13:11:05 +00:00
$('.shop_table.cart').stop(true).css('opacity', '1').unblock();
$('body').trigger('cart_page_refreshed');
});
$('.cart_totals').load( this_page + ' .cart_totals:eq(0) > *', function() {
$('.cart_totals').stop(true).css('opacity', '1').unblock();
});
// Trigger event so themes can refresh other areas
$('body').trigger( 'added_to_cart', [ fragments, cart_hash ] );
}
});
2012-12-10 13:11:05 +00:00
return false;
2012-12-10 13:11:05 +00:00
}
2012-12-10 13:11:05 +00:00
return true;
});
});