Issue with Wishlist Counter Update
-
Good morning,
I am using your plugin ‘YITH WooCommerce Wishlist’ in the free version and have reworked the code published on WordPress Support as follows:
if ( defined( 'YITH_WCWL' ) && ! function_exists( 'yith_wcwl_get_items_count' ) ) {
function yith_wcwl_get_items_count() {
$count = yith_wcwl_count_all_products();
$output = '<div class="wishlist-button">';
$output .= '<a href="' . esc_url( YITH_WCWL()->get_wishlist_url() ) . '" title="Wishlist" aria-label="Wishlist">';
$output .= '<span class="yith-wcwl-items-count">';
$output .= '<i class="yith-wcwl-icon fa fa-heart"><span class="counter' . ($count > 0 ? ' has-items' : '') . '">' . ($count > 0 ? esc_html( $count ) : '') . '</span></i>';
$output .= '</span>';
$output .= '</a>';
$output .= '</div>';
return $output;
}
add_shortcode( 'yith_wcwl_items_count', 'yith_wcwl_get_items_count' );
}
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 ) {
var counter = $('.yith-wcwl-items-count').find('.counter');
if (data.count > 0) {
counter.addClass('has-items').html(data.count);
} else {
counter.removeClass('has-items').html('');
}
} );
} );
} );
"
);
}
add_action( 'wp_enqueue_scripts', 'yith_wcwl_enqueue_custom_script', 20 );
}I was able to get a perfectly functioning counter button on all pages of the site, but with the update to version 4.0.0 of the plugin, the counter only works dynamically within the wishlist page, generating the following error when removing items:
XHRGET
https://www.stagingene.netsons.org/wp-json/yith/wishlist/v1/products/?_locale=user
[HTTP/3 400 1981ms]I have attempted various approaches to make the counter work on other pages, but without success. It seems that the
added_to_wishlist
event is not being triggered correctly, despite various attempts to monitor the click on the add buttons.Would it be possible to receive assistance on this issue? Any suggestions or alternative solutions would be greatly appreciated.
Thank you in advance for your attention and support.
Best regards
- You must be logged in to reply to this topic.