Making WP Rocket work with WooCommerce recently viewed products widget

The  WooCommerce Recently Viewed Products widget displays a list of the products a visitor/customer previously viewed on your shop.

With the default WP Rocket settings, the list will be cached during the first visit, and the same will be shown to all visitors, rendering the widget useless and even returning wrong results.

With the mu-plugin below, you'll be able to use the widget with caching without any issue:

<?php
/**
 * Plugin Name: Dynamic Cache WooCommerce recently viewed products
 * Description: Create a specific cache for each value of woocommerce_recently_viewed cookie
 * Author:      WP Rocket team
 * License:     GNU General Public License v3 or later
 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
 */

// Basic security, prevents file from being loaded directly.
defined( 'ABSPATH' ) or die( 'Cheatin&#8217; uh?' );

add_filter( 'rocket_htaccess_mod_rewrite' , '__return_false' );
add_filter( 'rocket_cache_dynamic_cookies', '__rocket_dynamic_cache_wc_recently_viewed_products' );

function __rocket_dynamic_cache_wc_recently_viewed_products( $dynamic_cookies ) {
	$dynamic_cookies[] = 'woocommerce_recently_viewed';

    return $dynamic_cookies;
}

If you don't know how to create a MU-plugin, please check out our doc:  How to Make a Custom (MU) Plugin