Wishlist - How to solve Wishlist issue with Cache Plugin

When you use wishlist with a cache plugin you usually have two orders of problems

1. the wishlist page shows incorrect content; solvable by excluding the wishlist page from the cache, as highly dynamic content

2. The Add to Wishlist button, which varies depending on the status of the product in the wishlist, is incorrect (for example, allowing you to add the product, even if it is already added to the list)

This second problem can be solved by reloading the button template in AJAX when the page is completely loaded

One way of doing this could be by adding the following code to the end of the theme's file or functions.php

 

if( defined( 'YITH_WCWL' ) && ! function_exists( 'yith_wcwl_add_ajax_refresh_script' ) ){
function yith_wcwl_add_ajax_refresh_script( ){
wp_add_inline_script( 'jquery-yith-wcwl', "
jQuery( function( $ ){
$( '.yith-wcwl-add-to-wishlist' ).each( function(){
var t = this,
jqT = $(this),
product_id = [].filter.call( t.classList, function(i){ return /add-to-wishlist-[0-9]*/.test(i) } )[0].replace( 'add-to-wishlist-', '' );

$.get(
yith_wcwl_l10n.ajax_url,
{
action: 'yith_wcwl_print_shortcode',
product_id: product_id
},
function( data ){
if( ! data.length ){
return;
}

jqT.html( $(data).filter( '.add-to-wishlist-' + product_id ).html() );
},
'HTML'
);
} );
} );
" );
}
add_action( 'wp_enqueue_scripts', 'yith_wcwl_add_ajax_refresh_script' );
}

if( defined( 'YITH_WCWL' ) && ! function_exists( 'yith_wcwl_ajax_print_shortcode' ) ){
function yith_wcwl_ajax_print_shortcode(){
$product_id = isset( $_GET['product_id'] ) ? $_GET['product_id'] : false;

if( ! $product_id ){
echo '';
die;
}

echo do_shortcode( "[yith_wcwl_add_to_wishlist product_id='{$product_id}']" );
die;
}
add_action( 'wp_ajax_yith_wcwl_print_shortcode', 'yith_wcwl_ajax_print_shortcode' );
add_action( 'wp_ajax_nopriv_yith_wcwl_print_shortcode', 'yith_wcwl_ajax_print_shortcode' );
}
Was this article helpful?
0 out of 0 found this helpful

Back to Help Center

Powered by Zendesk