2014-03-17 05:26:55 +00:00
|
|
|
jQuery( function( $ ) {
|
2012-07-17 14:09:18 +00:00
|
|
|
|
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;
|
2013-12-04 19:15:24 +00:00
|
|
|
|
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;
|
|
|
|
}
|
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' ) {
|
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 {
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2014-09-09 11:43:56 +00:00
|
|
|
$thisbutton.removeClass( 'loading' );
|
2013-02-10 23:02:52 +00:00
|
|
|
|
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' );
|
|
|
|
});
|
|
|
|
}
|
2013-01-28 10:59:19 +00:00
|
|
|
|
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: { background: 'transparent url(' + wc_add_to_cart_params.ajax_loader_url + ') no-repeat center', backgroundSize: '16px 16px', 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="' +
|
|
|
|
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
|
|
|
$( '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" />' );
|
2013-02-10 23:02:52 +00:00
|
|
|
|
2014-09-09 11:43:56 +00:00
|
|
|
$( '.shop_table.cart' ).stop( true ).css( 'opacity', '1' ).unblock();
|
2013-02-10 23:02:52 +00:00
|
|
|
|
2014-09-09 11:43:56 +00:00
|
|
|
$( 'body' ).trigger( 'cart_page_refreshed' );
|
|
|
|
});
|
2013-02-10 23:02:52 +00:00
|
|
|
|
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 ] );
|
|
|
|
}
|
|
|
|
});
|
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:41:14 +00:00
|
|
|
|
2014-09-09 11:43:56 +00:00
|
|
|
return true;
|
|
|
|
});
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2013-12-04 19:15:24 +00:00
|
|
|
});
|