2014-03-17 05:26:55 +00:00
|
|
|
jQuery( function( $ ) {
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2013-12-04 19:15:24 +00:00
|
|
|
// wc_add_to_cart_params is required to continue, ensure the object exists
|
2014-03-17 05:26:55 +00:00
|
|
|
if ( typeof wc_add_to_cart_params === 'undefined' )
|
2013-12-04 19:15:24 +00:00
|
|
|
return false;
|
|
|
|
|
2012-07-17 14:09:18 +00:00
|
|
|
// Ajax add to cart
|
2014-03-17 05:26:55 +00:00
|
|
|
$( document ).on( 'click', '.add_to_cart_button', function() {
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2012-07-17 14:09:18 +00:00
|
|
|
// AJAX add to cart request
|
2014-03-17 05:26:55 +00:00
|
|
|
var $thisbutton = $( this );
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2014-03-17 05:26:55 +00:00
|
|
|
if ( $thisbutton.is( '.product_type_simple' ) ) {
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2014-03-17 05:26:55 +00:00
|
|
|
if ( ! $thisbutton.attr( 'data-product_id' ) )
|
2013-09-25 11:35:06 +00:00
|
|
|
return true;
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2014-03-17 05:26:55 +00:00
|
|
|
$thisbutton.removeClass( 'added' );
|
|
|
|
$thisbutton.addClass( 'loading' );
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2012-07-17 14:09:18 +00:00
|
|
|
var data = {
|
2014-03-17 05:26:55 +00:00
|
|
|
action: 'woocommerce_add_to_cart',
|
|
|
|
product_id: $thisbutton.attr( 'data-product_id' ),
|
|
|
|
quantity: $thisbutton.attr( 'data-quantity' )
|
2012-07-17 14:09:18 +00:00
|
|
|
};
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2012-07-17 14:09:18 +00:00
|
|
|
// Trigger event
|
2014-03-17 05:26:55 +00:00
|
|
|
$( 'body' ).trigger( 'adding_to_cart', [ $thisbutton, data ] );
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2012-07-17 14:09:18 +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
|
|
|
|
2012-07-17 14:09:18 +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 ) {
|
2013-01-28 10:59:19 +00:00
|
|
|
window.location = response.product_url;
|
2013-01-22 16:31:54 +00:00
|
|
|
return;
|
2012-07-17 14:09:18 +00:00
|
|
|
}
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2013-02-10 23:02:52 +00:00
|
|
|
// Redirect to cart option
|
2014-03-17 05:26:55 +00:00
|
|
|
if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) {
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2013-06-11 12:31:41 +00:00
|
|
|
window.location = wc_add_to_cart_params.cart_url;
|
2013-02-10 23:02:52 +00:00
|
|
|
return;
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2013-02-10 23:02:52 +00:00
|
|
|
} else {
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2014-03-17 05:26:55 +00:00
|
|
|
$thisbutton.removeClass( 'loading' );
|
2012-07-17 14:09:18 +00:00
|
|
|
|
2013-02-10 23:02:52 +00:00
|
|
|
fragments = response.fragments;
|
|
|
|
cart_hash = response.cart_hash;
|
|
|
|
|
|
|
|
// Block fragments class
|
|
|
|
if ( fragments ) {
|
2014-03-17 05:26:55 +00:00
|
|
|
$.each( fragments, function( key, value ) {
|
|
|
|
$( key ).addClass( 'updating' );
|
2013-02-10 23:02:52 +00:00
|
|
|
});
|
|
|
|
}
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2013-02-10 23:02:52 +00:00
|
|
|
// Block widgets and fragments
|
2014-03-17 05:26:55 +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 } } );
|
2013-01-28 10:59:19 +00:00
|
|
|
|
2013-02-10 23:02:52 +00:00
|
|
|
// Changes button classes
|
2014-03-17 05:26:55 +00:00
|
|
|
$thisbutton.addClass( 'added' );
|
2013-04-15 11:36:04 +00:00
|
|
|
|
|
|
|
// View cart text
|
2014-03-17 05:26:55 +00:00
|
|
|
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
|
|
|
|
2013-02-10 23:02:52 +00:00
|
|
|
// Replace fragments
|
|
|
|
if ( fragments ) {
|
2014-03-17 05:26:55 +00:00
|
|
|
$.each( fragments, function( key, value ) {
|
|
|
|
$( key ).replaceWith( value );
|
2013-02-10 23:02:52 +00:00
|
|
|
});
|
|
|
|
}
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2013-02-10 23:02:52 +00:00
|
|
|
// Unblock
|
2014-03-17 05:26:55 +00:00
|
|
|
$( '.widget_shopping_cart, .updating' ).stop( true ).css( 'opacity', '1' ).unblock();
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2013-02-10 23:02:52 +00:00
|
|
|
// Cart page elements
|
2014-03-17 05:26:55 +00:00
|
|
|
$( '.shop_table.cart' ).load( this_page + ' .shop_table.cart:eq(0) > *', function() {
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2014-03-17 05:26:55 +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
|
|
|
|
2014-03-17 05:26:55 +00:00
|
|
|
$( '.shop_table.cart' ).stop( true ).css( 'opacity', '1' ).unblock();
|
2013-02-10 23:02:52 +00:00
|
|
|
|
2014-03-17 05:26:55 +00:00
|
|
|
$( 'body' ).trigger( 'cart_page_refreshed' );
|
2013-02-10 23:02:52 +00:00
|
|
|
});
|
|
|
|
|
2014-03-17 05:26:55 +00:00
|
|
|
$( '.cart_totals' ).load( this_page + ' .cart_totals:eq(0) > *', function() {
|
|
|
|
$( '.cart_totals' ).stop( true ).css( 'opacity', '1' ).unblock();
|
2013-02-10 23:02:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Trigger event so themes can refresh other areas
|
2014-03-17 05:26:55 +00:00
|
|
|
$( 'body' ).trigger( 'added_to_cart', [ fragments, cart_hash ] );
|
2013-02-10 23:02:52 +00:00
|
|
|
}
|
2012-07-17 14:09:18 +00:00
|
|
|
});
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2012-07-17 14:09:18 +00:00
|
|
|
return false;
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2012-07-17 14:09:18 +00:00
|
|
|
}
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2013-09-25 11:35:06 +00:00
|
|
|
return true;
|
2012-07-17 14:09:18 +00:00
|
|
|
});
|
|
|
|
|
2013-12-04 19:15:24 +00:00
|
|
|
});
|