frontend add to cart jshinted and coding standards

This commit is contained in:
splashingpixels 2014-03-16 22:26:55 -07:00 committed by Mike Jolley
parent 1396e9179a
commit 48b9807cd1
2 changed files with 33 additions and 31 deletions

View File

@ -1,31 +1,31 @@
jQuery(function($) {
jQuery( function( $ ) {
// wc_add_to_cart_params is required to continue, ensure the object exists
if (typeof wc_add_to_cart_params === "undefined")
if ( typeof wc_add_to_cart_params === 'undefined' )
return false;
// Ajax add to cart
$(document).on( 'click', '.add_to_cart_button', function() {
$( document ).on( 'click', '.add_to_cart_button', function() {
// AJAX add to cart request
var $thisbutton = $(this);
var $thisbutton = $( this );
if ( $thisbutton.is('.product_type_simple') ) {
if ( $thisbutton.is( '.product_type_simple' ) ) {
if (!$thisbutton.attr('data-product_id'))
if ( ! $thisbutton.attr( 'data-product_id' ) )
return true;
$thisbutton.removeClass('added');
$thisbutton.addClass('loading');
$thisbutton.removeClass( 'added' );
$thisbutton.addClass( 'loading' );
var data = {
action: 'woocommerce_add_to_cart',
product_id: $thisbutton.attr('data-product_id'),
quantity: $thisbutton.attr('data-quantity')
action: 'woocommerce_add_to_cart',
product_id: $thisbutton.attr( 'data-product_id' ),
quantity: $thisbutton.attr( 'data-quantity' )
};
// Trigger event
$('body').trigger( 'adding_to_cart', [ $thisbutton, data ] );
$( 'body' ).trigger( 'adding_to_cart', [ $thisbutton, data ] );
// Ajax action
$.post( wc_add_to_cart_params.ajax_url, data, function( response ) {
@ -43,61 +43,63 @@ jQuery(function($) {
}
// Redirect to cart option
if ( wc_add_to_cart_params.cart_redirect_after_add == 'yes' ) {
if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) {
window.location = wc_add_to_cart_params.cart_url;
return;
} else {
$thisbutton.removeClass('loading');
$thisbutton.removeClass( 'loading' );
fragments = response.fragments;
cart_hash = response.cart_hash;
// Block fragments class
if ( fragments ) {
$.each(fragments, function(key, value) {
$(key).addClass('updating');
$.each( fragments, function( key, value ) {
$( key ).addClass( 'updating' );
});
}
// 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 } } );
$( '.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
$thisbutton.addClass('added');
$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>' );
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>' );
}
// Replace fragments
if ( fragments ) {
$.each(fragments, function(key, value) {
$(key).replaceWith(value);
$.each( fragments, function( key, value ) {
$( key ).replaceWith( value );
});
}
// Unblock
$('.widget_shopping_cart, .updating').stop(true).css('opacity', '1').unblock();
$( '.widget_shopping_cart, .updating' ).stop( true ).css( 'opacity', '1' ).unblock();
// Cart page elements
$('.shop_table.cart').load( this_page + ' .shop_table.cart:eq(0) > *', function() {
$( '.shop_table.cart' ).load( this_page + ' .shop_table.cart:eq(0) > *', function() {
$("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" />');
$( '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" />' );
$('.shop_table.cart').stop(true).css('opacity', '1').unblock();
$( '.shop_table.cart' ).stop( true ).css( 'opacity', '1' ).unblock();
$('body').trigger('cart_page_refreshed');
$( 'body' ).trigger( 'cart_page_refreshed' );
});
$('.cart_totals').load( this_page + ' .cart_totals:eq(0) > *', function() {
$('.cart_totals').stop(true).css('opacity', '1').unblock();
$( '.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 ] );
$( 'body' ).trigger( 'added_to_cart', [ fragments, cart_hash ] );
}
});

View File

@ -1 +1 @@
jQuery(function(a){return"undefined"==typeof wc_add_to_cart_params?!1:void a(document).on("click",".add_to_cart_button",function(){var b=a(this);if(b.is(".product_type_simple")){if(!b.attr("data-product_id"))return!0;b.removeClass("added"),b.addClass("loading");var c={action:"woocommerce_add_to_cart",product_id:b.attr("data-product_id"),quantity:b.attr("data-quantity")};return a("body").trigger("adding_to_cart",[b,c]),a.post(wc_add_to_cart_params.ajax_url,c,function(c){if(c){var d=window.location.toString();return d=d.replace("add-to-cart","added-to-cart"),c.error&&c.product_url?void(window.location=c.product_url):"yes"==wc_add_to_cart_params.cart_redirect_after_add?void(window.location=wc_add_to_cart_params.cart_url):(b.removeClass("loading"),fragments=c.fragments,cart_hash=c.cart_hash,fragments&&a.each(fragments,function(b){a(b).addClass("updating")}),a(".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:.6}}),b.addClass("added"),wc_add_to_cart_params.is_cart||0!=b.parent().find(".added_to_cart").size()||b.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>"),fragments&&a.each(fragments,function(b,c){a(b).replaceWith(c)}),a(".widget_shopping_cart, .updating").stop(!0).css("opacity","1").unblock(),a(".shop_table.cart").load(d+" .shop_table.cart:eq(0) > *",function(){a("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" />'),a(".shop_table.cart").stop(!0).css("opacity","1").unblock(),a("body").trigger("cart_page_refreshed")}),a(".cart_totals").load(d+" .cart_totals:eq(0) > *",function(){a(".cart_totals").stop(!0).css("opacity","1").unblock()}),a("body").trigger("added_to_cart",[fragments,cart_hash]),void 0)}}),!1}return!0})});
jQuery(function(e){if(typeof wc_add_to_cart_params==="undefined")return false;e(document).on("click",".add_to_cart_button",function(){var t=e(this);if(t.is(".product_type_simple")){if(!t.attr("data-product_id"))return true;t.removeClass("added");t.addClass("loading");var n={action:"woocommerce_add_to_cart",product_id:t.attr("data-product_id"),quantity:t.attr("data-quantity")};e("body").trigger("adding_to_cart",[t,n]);e.post(wc_add_to_cart_params.ajax_url,n,function(n){if(!n)return;var r=window.location.toString();r=r.replace("add-to-cart","added-to-cart");if(n.error&&n.product_url){window.location=n.product_url;return}if(wc_add_to_cart_params.cart_redirect_after_add==="yes"){window.location=wc_add_to_cart_params.cart_url;return}else{t.removeClass("loading");fragments=n.fragments;cart_hash=n.cart_hash;if(fragments){e.each(fragments,function(t,n){e(t).addClass("updating")})}e(".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:.6}});t.addClass("added");if(!wc_add_to_cart_params.is_cart&&t.parent().find(".added_to_cart").size()===0){t.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>")}if(fragments){e.each(fragments,function(t,n){e(t).replaceWith(n)})}e(".widget_shopping_cart, .updating").stop(true).css("opacity","1").unblock();e(".shop_table.cart").load(r+" .shop_table.cart:eq(0) > *",function(){e("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" />');e(".shop_table.cart").stop(true).css("opacity","1").unblock();e("body").trigger("cart_page_refreshed")});e(".cart_totals").load(r+" .cart_totals:eq(0) > *",function(){e(".cart_totals").stop(true).css("opacity","1").unblock()});e("body").trigger("added_to_cart",[fragments,cart_hash])}});return false}return true})})