Code snippet: Disable product table caching for specific users
Please note: this article is aimed at developers. We have provided an article on how to add code snippets. If you're not comfortable using them then you can use our plugin customization service.
WooCommerce Product Table has a cache option which can improve table load times. In some cases, you may want to keep caching enabled for most visitors while disabling it for specific users, such as administrators who are testing changes.
Disable caching for administrators
Add this PHP code to your theme or child theme, or with a code snippets plugin:
add_filter( 'wc_product_table_use_data_cache', 'barn2_wpt_disable_cache_for_administrators', 10, 2 );
function barn2_wpt_disable_cache_for_administrators( $use_cache, $table ) {
if ( current_user_can( 'manage_options' ) ) {
return false;
}
return $use_cache;
}
This keeps the product table cache working for normal visitors, while administrators see uncached table data.
Disable caching for all logged-in users
Use this version if you want to disable the product table cache for every logged-in user:
add_filter( 'wc_product_table_use_data_cache', 'barn2_wpt_disable_cache_for_logged_in_users', 10, 2 );
function barn2_wpt_disable_cache_for_logged_in_users( $use_cache, $table ) {
if ( is_user_logged_in() ) {
return false;
}
return $use_cache;
}
Visitors who are not logged in will continue to use the product table cache.