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

121 lines
3.1 KiB
JavaScript
Raw Normal View History

2015-07-10 05:13:30 +00:00
/*global wc_add_to_cart_params */
jQuery( function( $ ) {
2014-09-09 11:43:56 +00:00
// wc_add_to_cart_params is required to continue, ensure the object exists
2015-07-10 05:13:30 +00:00
if ( typeof wc_add_to_cart_params === 'undefined' ) {
2014-09-09 11:43:56 +00:00
return false;
2015-07-10 05:13:30 +00:00
}
2014-09-09 11:43:56 +00:00
// Ajax add to cart
2015-07-10 05:13:30 +00:00
$( document ).on( 'click', '.add_to_cart_button', function() {
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
if ( $thisbutton.is( '.ajax_add_to_cart' ) ) {
2012-12-10 13:11:05 +00:00
2015-07-10 05:13:30 +00:00
if ( ! $thisbutton.attr( 'data-product_id' ) ) {
2014-09-09 11:43:56 +00:00
return true;
2015-07-10 05:13:30 +00:00
}
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
var data = {};
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
2015-04-13 15:37:22 +00:00
$( document.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
2015-07-27 16:55:37 +00:00
$.post( wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ), data, function( response ) {
2012-12-10 13:11:05 +00:00
2015-07-10 05:13:30 +00:00
if ( ! response ) {
2014-09-09 11:43:56 +00:00
return;
2015-07-10 05:13:30 +00:00
}
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' );
2015-07-10 05:13:30 +00:00
var fragments = response.fragments;
var 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 ) {
2015-07-10 05:13:30 +00:00
$.each( fragments, function( key ) {
2014-09-09 11:43:56 +00:00
$( 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();
2015-04-13 15:37:22 +00:00
$( document.body ).trigger( 'cart_page_refreshed' );
2014-09-09 11:43:56 +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
2015-04-13 15:37:22 +00:00
$( document.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;
});
});