Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

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 -->
share|improve this answer
    
Excellent answer! Works like a charm –  Lombo Dec 3 '10 at 20:19
    
Interesting, was not aware about fragment caching (recent addition?). Any idea how it actually works on technical side? –  Rarst Dec 4 '10 at 9:15
add comment

W3, like all other plugins of this kind, caches page output in static file. Basically you can't have part of static file being non-static.

I am no expert in this field, but I would suggest to look into cookies after all. For example you can try to leave cache for pages with empty cart, but make filled cart set some cookie and skip caching on that.

share|improve this answer
    
I understand your proposal but I would not want my users to see a performance hit because they added a product to their cart –  Lombo Dec 3 '10 at 19:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.