Hi. How can we help?

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' );
}

if ( defined( 'YITH_WCWL' ) && ! function_exists( 'yith_wcwl_enqueue_custom_script' ) ) {
function yith_wcwl_enqueue_custom_script() {
wp_add_inline_script(
'jquery-yith-wcwl',
"
jQuery( function( $ ) {
$( document ).on( 'added_to_wishlist removed_from_wishlist', function() {
$.get( yith_wcwl_l10n.ajax_url, {
action: 'yith_wcwl_update_wishlist_count'
}, function( data ) {
$('.your-counter-selector').html( data.count );
} );
} );
} );
"
);
}
add_action( 'wp_enqueue_scripts', 'yith_wcwl_enqueue_custom_script', 20 );
}

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?
19 out of 49 found this helpful

Back to Help Center >