We have a considerably performance-savvy view that we want cached, but it just won't work.
Stops working when cache is enabled.

To reproduce:

- Create a node type 'foo' and product type 'bar'.
- Create in node type 'foo' field of type "Commerce product reference" to type 'bar'.
- Create a view page on path '/mypath' consisting of node displays of type 'foo'
- Enable views caching 5min/5min
- Add to cart first time
- Next try to "Add to cart" another product related of same node (another product variation in Commerce Backoffice terminology).

Is it by design that the cart buttons now stops working?

Ive looked around for views issues as well which could result in this, but alas, without result.
Views 3.8

Any clues?
Thanks!

Comments

rszrama’s picture

Category: bug » support
Status: Active » Closed (works as designed)

The issue here is that Views is then caching form HTML which will be invalid for anyone besides the initial user who generated the cache. It will even be invalid for that initial user after he or she submits the form once, because it will require a new token on subsequent submits. Unfortunately, for the Add to Cart form, you just can't cache it like that. You could cache it if you were using Add to Cart links instead of forms, but that might result in other issues web crawlers generating carts - I'm not sure if the Rules Links module can prevent that.

Exploratus’s picture

So we cant cache a view for a store using add to cart buttons? Seems rather limiting doesn't it?

farald’s picture

Yes it is rather limiting, if you have front page with lots of cart buttons. We worked around the issue by serving buttons as links. Have a look at https://drupal.org/sandbox/farald/1945900

periksson’s picture

Issue summary: View changes

This might be outside the scope of this issue.

Is it possible to cache "everything else" in the view - entities, nodes etc to increase performance, leaving the view without the time-based cache?

We are looking to a solution to increase performance, so any suggestions, modules are greatly appreciated.

Best

Eric

periksson’s picture

Hi,

Would the Views Per-User Cache solve this issue?

https://www.drupal.org/project/views_puc

Best Eric

ofry’s picture

Now I can suggest only cache query results, not the HTML code :(

ofry’s picture

Version: 7.x-1.5 » 7.x-1.10
Issue summary: View changes
Status: Closed (works as designed) » Active
ofry’s picture

Category: Support request » Bug report
bojanz’s picture

Status: Active » Closed (works as designed)

This is a limitation of the Views form API, not something we control or can change at this point.

periksson’s picture

My recommendation is to unset the form token of the form. The security impact (CSRF) is low from our perspective.

Then cache the view as usual.

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if (strstr($form_id, 'commerce_cart_add_to_cart_form') || strstr($form_id, 'views_form_commerce_cart_form_default')) {
	// Remove tokens for add to cart forms
	unset($form['#token']);
}
kopeboy’s picture

Title: Views cache breaks cart buttons » How to cache a view display add to cart forms
Version: 7.x-1.10 » 7.x-1.11
Category: Bug report » Support request
Status: Closed (works as designed) » Needs review

If I disable the form token as in #6 will any Drupal Commerce functionality be affected?
ie. is the token only related to security? In which way?

My particular concern is about multiple anonymous user carts functionality (not a developer).

kopeboy’s picture

Title: How to cache a view display add to cart forms » How to cache a view display with add to cart forms
gdana’s picture

Sorry for this, eventually realised my comment and question had nothing to do with the issue posted.

rszrama’s picture

Status: Needs review » Closed (works as designed)

I agree w/ Bojan and disagree with security implications; I'd never intentionally make form submission less secure.

matsjacobsson’s picture

Hi, Hopefully some kind soul can clarify this for me...

1. Is it approved to use query caching (not HTML) as ofry #6 suggested?

2. I'm also wondering if distinct or aggregation will affect this issue in any way?

Thanks!

/Mats

xandeadx’s picture

andyg8’s picture

That was causing our site's Add to cart button to fail if used on a 2nd product in a view (even a non-Ajax view). We found we could still cache if we used Views, caching > time based > cache raw query results (1 hour) but NOT cache rendered output.