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

113 lines
3.3 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, .product_type_downloadable, .product_type_virtual')) {
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'),
security: woocommerce_params.add_to_cart_nonce
};
2012-12-10 13:11:05 +00:00
// Trigger event
$('body').trigger('adding_to_cart');
2012-12-10 13:11:05 +00:00
// Ajax action
$.post( woocommerce_params.ajax_url, data, function(response) {
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
$thisbutton.removeClass('loading');
// Get response
data = $.parseJSON( response );
2012-12-10 13:11:05 +00:00
if (data.error && data.product_url) {
window.location = data.product_url;
return;
}
2012-12-10 13:11:05 +00:00
fragments = data;
// 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
2012-12-13 15:53:36 +00:00
$('.widget_shopping_cart, .shop_table.cart, .updating, .cart_totals').fadeTo('400', '0.6').block({message: null, overlayCSS: {background: 'transparent url(' + woocommerce_params.ajax_loader_url + ') no-repeat center', backgroundSize: '16px 16px', opacity: 0.6 } } );
2012-12-10 13:11:05 +00:00
// Changes button classes
2012-12-10 13:11:05 +00:00
if ( $thisbutton.parent().find('.added_to_cart').size() == 0 )
$thisbutton.addClass('added').after( ' <a href="' + woocommerce_params.cart_url + '" class="added_to_cart" title="' + woocommerce_params.i18n_view_cart + '">' + woocommerce_params.i18n_view_cart + '</a>' );
// Cart widget load
if ($('.widget_shopping_cart').size()>0) {
$('.widget_shopping_cart:eq(0)').load( this_page + ' .widget_shopping_cart:eq(0) > *', function() {
// 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
$('body').trigger('cart_widget_refreshed');
} );
} else {
// 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();
2012-12-10 13:11:05 +00:00
$('body').trigger('cart_page_refreshed');
});
2012-12-10 13:11:05 +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
// Trigger event so themes can refresh other areas
$('body').trigger('added_to_cart');
2012-12-10 13:11:05 +00:00
});
2012-12-10 13:11:05 +00:00
return false;
2012-12-10 13:11:05 +00:00
} else {
return true;
}
2012-12-10 13:11:05 +00:00
});
});