Wishlist - How to count number of products wishlist in ajax

You can try to add the following code to functions.php of your theme or child

if( defined( 'YITH_WCWL' ) && ! function_exists( 'yith_wcwl_ajax_update_count' ) ){
function yith_wcwl_ajax_update_count(){
wp_send_json( array(
'count' => yith_wcwl_count_all_products()
) );
}
add_action( 'wp_ajax_yith_wcwl_update_wishlist_count', 'yith_wcwl_ajax_update_count' );
add_action( 'wp_ajax_nopriv_yith_wcwl_update_wishlist_count', 'yith_wcwl_ajax_update_count' );
}

Then, we'll need some js magic
Just copy wp-content/plugins/yith-woocommerce-wishlist-premium/assets/js/unminified/jquery.yith-wcwl.js and paste it under wp-content/themes/<your theme or child directory>/woocommerce/, renaming it as wishlist.js


Then, you can try to add the following code at the end of this brand new file

jQuery( document ).ready( function( $ ){
$(document).on( 'added_to_wishlist removed_from_wishlist', function(){
var counter = $('.your-counter-selector');

$.ajax({
url: yith_wcwl_l10n.ajax_url,
data: {
action: 'yith_wcwl_update_wishlist_count'
},
dataType: 'json',
success: function( data ){
counter.html( data.count );
},
beforeSend: function(){
counter.block();
},
complete: function(){
counter.unblock();
}
})
} )
});

This should force an ajax call to update wishlist count each time an item is added or removed from wishlist
Please, note that you should replace .your-counter-selector with selector of DOM item containing your counter

Was this article helpful?
0 out of 0 found this helpful

Back to Help Center

Powered by Zendesk