My shopping cart is being cached by w3 total cache. This is the expected default behaviour. The thing is I can’t find an easy solution for this.
I am showing my shopping cart on almost every page, so using rejected cookies or something similar is not an option. Is it possible to tell w3 total cache not to cache a single php file like cart_widget.php? Or some other workaround?
I know there is the possibility to load the cart via ajax as the plugin does not cache GET requests with parameters (by default). Still, I would like to avoid that extra request.
EDIT: Also, w3 total cache is not regenerating the page when I edit the product, is there an easy way to tell w3 total cache to empty the cache for a single page?
2 Answers
W3 Total Cache is a very advanced plugin that has built in ways to handle almost any situation.
To exclude a page from being cached use one of the following commands below:
define('DONOTCACHEPAGE', true);
Disables page caching for a given page.
define('DONOTCACHEDB', true);
Disables database caching for given page.
define('DONOTMINIFY', true);
Disables minify for a given page.
define('DONOTCDN', true);
Disables content delivery network for a given page.
define('DONOTCACHCEOBJECT', true);
Disables object cache for a given page.
To make W3 Total empty the cache on your product edit you can hook the following function in when the product is saved:
w3tc_pgcache_flush_post($post_id);
To implement fragment caching wrap the functions or lines of code that you don’t want to cache in an mfunc:
Example 1:
<!-- mfunc any PHP code --><!-- /mfunc -->
Example 2:
<!-- mfunc -->any PHP code<!-- /mfunc -->
Example 3:
<!--MFUNC -->
echo rand();
<!--/mfunc -->
Example 4:
<!-- mclude path/to/file.php --><!-- /mclude -->
Example 5:
<!-- mclude -->path/to/file.php<!-- /mclude -->