• Hi I’m using yith wishlist plugin and I’m trying to update the wishlist counter without reloading the page, i have added this code at functions.php

     if ( defined( 'YITH_WCWL' ) && ! function_exists( 'yith_wcwl_get_items_count' ) ) {
    function yith_wcwl_get_items_count() {
    ob_start();
    ?>
    <div class="header-wishlist">
    <a href="<?php echo esc_url( YITH_WCWL()->get_wishlist_url() ); ?>">
    <span class="yith-wcwl-items-count">
    <i class="yith-wcwl-icon fa fa-heart-o"></i>
    <?php
    $count = YITH_WCWL()->count_products();
    if ( $count > 0 ) {
    echo '<span class="wishlist-count">' . esc_html( $count ) . '</span>';
    }
    ?>
    </span>
    </a>
    </div>
    <?php
    return ob_get_clean();
    }

    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() {
    // Count all wishlist products
    wp_send_json(
    array(
    'count' => YITH_WCWL()->count_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_enqueue_script( 'jquery' );

    wp_add_inline_script( 'jquery', "
    jQuery( function( $ ) {
    let refreshCounter = () => {
    $.get( yith_wcwl_l10n.ajax_url, {
    action: 'yith_wcwl_update_wishlist_count'
    }, function( data ) {
    $('.menu-item-wishlist').find('.mini-item-counter').html( data.count );
    });
    };

    $( document ).on( 'added_to_wishlist removed_from_wishlist', refreshCounter );

    // Listen for the custom event on DOM updates
    $( document ).on( 'yith_wcwl_wishlist_updated', refreshCounter );

    // Initial count refresh
    refreshCounter();
    });
    ");
    }
    add_action( 'wp_enqueue_scripts', 'yith_wcwl_enqueue_custom_script', 20 );
    }

    This action: yith_wcwl_update_wishlist_count happens just the first time the page is open and when i click to add or remove an item it doesn’t happened, this is the url wp-admin/admin-ajax.php?action=yith_wcwl_update_wishlist_count. And this happens when i add a new item it/wp-json/yith/wishlist/v1/items?_locale=user

  • You must be logged in to reply to this topic.