Move "added_to_cart" actions to trigger

Doing this means that plugin/theme developers can trigger the mini-cart and buttons to update without having to rewrite and maintain their own version of this function.
This commit is contained in:
James Kemp 2016-11-17 16:05:46 +00:00 committed by GitHub
parent 1039babe2c
commit cf6c42fb01
1 changed files with 61 additions and 52 deletions

View File

@ -56,10 +56,25 @@ jQuery( function( $ ) {
} else {
$thisbutton.removeClass( 'loading' );
// Trigger event so themes can refresh other areas
$( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, $thisbutton ] );
var fragments = response.fragments;
var cart_hash = response.cart_hash;
}
});
return false;
}
return true;
});
// On "added_to_cart"
$( document.body ).on( 'added_to_cart', function( event, fragments, cart_hash, $button ) {
$button = typeof $button === "undefined" ? false : $button;
if( $button ) { $button.removeClass( 'loading' ); }
// Block fragments class
if ( fragments ) {
@ -76,15 +91,19 @@ jQuery( function( $ ) {
}
});
if( $button ) {
// Changes button classes
$thisbutton.addClass( 'added' );
$button.addClass( 'added' );
// View cart text
if ( ! wc_add_to_cart_params.is_cart && $thisbutton.parent().find( '.added_to_cart' ).length === 0 ) {
$thisbutton.after( ' <a href="' + wc_add_to_cart_params.cart_url + '" class="added_to_cart wc-forward" title="' +
if ( ! wc_add_to_cart_params.is_cart && $button.parent().find( '.added_to_cart' ).length === 0 ) {
$button.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 ) {
@ -107,16 +126,6 @@ jQuery( function( $ ) {
$( '.cart_totals' ).stop( true ).css( 'opacity', '1' ).unblock();
});
// Trigger event so themes can refresh other areas
$( document.body ).trigger( 'added_to_cart', [ fragments, cart_hash, $thisbutton ] );
}
});
return false;
}
return true;
});
});