• Hi there,

    I have installed YITH Wishlist and there seems to be a problem with the counter when having Redis object cache activated.

    the counter is not updating when adding more than one product to the wishlist.

    It am also using Varnish page cache and have exluded all necessery files, cookies and request from cache.

    When shutting down redis, the wishlist counter updates just fine. Has anyone an explanation for this? Interestingly the cart counter works fine with redis activated.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter windmeile

    (@windmeile)

    UPDATE: I did some further debugging and I found, the problem is not related to the frontend, meaning it is updating the counter just fine, but with the wrong counter.

    I checked the KEYS stored in my redis object cache and found the following YITH wishlist related KEYS:

    1) “wishlists:wishlist-token-“
    2) “wishlists:wishlist-default-912”
    3) “wishlist-items:item-307”
    4) “wishlists:wishlist-user-total-count-912”
    5) “wishlist-items:item-304”
    6) “wishlists:wishlist-default-44486be4f1fa529b0f30f088b5b0f003”
    7) “wishlists:wishlist-user-total-count-44486be4f1fa529b0f30f088b5b0f003”
    8) “wishlists:wishlist-id-0”
    9) “wishlist-items:item-306”
    10) “wishlists:wc_wishlists_cache_prefix”
    11) “wishlists:wishlist-id-158”
    12) “wishlists:wishlist-items-158”
    13) “wishlist-items:item-305”

    If I check the value of the wishlists:wishlist-user-total-count* KEY I get:

    127.0.0.1:6379> GET wishlists:wishlist-user-total-count-44486be4f1fa529b0f30f088b5b0f003
    “1”

    It always gives back “1” no matter how many items are in wishlist.

    TLDR, the problem lays in populating the object cache, it seems it gets stored in object cache with the first item being added to wishlist and after that never again.

    Would ne great if a developer could look into this. Thanks.

    Thread Starter windmeile

    (@windmeile)

    it‘s a bit disappointing that no one ever got back to me even after providing thorough debugging information. however, I got it solved by hiring a developer that fixed it for me. Hope this helps anyone having the same problem or ideally gets implemented in the next version update.

    So basically the counter function that is being triggered with every add or remove from wishlist actiob checks if the counter value is in the object cache, and if it is, take it from there and if its not recalculate it. To me that makes absolutely no sense, if you add a product you should have the cached value plus one or if you remove it minus one. We fixed it by just always recalculating this value.

    Here is how to implement this workaround:

    Open yith-woocommerce-wishlist/includes/class-yith-wcwl.php

    Change this code:

    $count = wp_cache_get( 'wishlist-count-' . $wishlist->get_token(), 'wishlists' );

    into

    $count = $wishlist->count_items();
     wp_cache_set( 'wishlist-count-' . $wishlist->get_token(), $count, 'wishlists' );

    do that for logged in user function and non-logged in user function. thats it.

    maybe a developer can comment as to why the code was written that way.

    We had exactly the same problem. If Redis cache is activated, the number will not be displayed properly. We didn’t want to edit the plugin as suggested by Windmeile to ensure our changes would not be overwritten by a plugin update? Here is the fix we used, hopefully this will help more people for fixing this problem:

    add_action( 'wp_ajax_yith_wcwl_update_wishlist_count', 'epicode_overwrite_yith_wcwl_ajax_update_count', 99 );
    add_action( 'wp_ajax_nopriv_yith_wcwl_update_wishlist_count', 'epicode_overwrite_yith_wcwl_ajax_update_count', 99 );
    
    function epicode_overwrite_yith_wcwl_ajax_update_count() {
    
      // Build args array
      $args = array(
        'wishlist_id' => 'all',
        'user_id'     => is_user_logged_in() ? get_current_user_id() : YITH_WCWL_Session()->get_session_id()
      );
    
      // Get wishlist count from function instead of cache
      wp_send_json( array(
        'count' => YITH_WCWL_Wishlist_Factory::get_wishlist_items_count( $args )
      ) );
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘YITH Wishlist and Redis’ is closed to new replies.