I have set up several views tables to display products. Each view is sorted by taxonomy. Everything is fine so far.

As I set up another view by a limited numbers of products to display, like a teaser on the front page.

I've got the idea to show the products by the Global Random function in the sort criteria.

So the visitor get's different products to view when he is entering the page each time.

In this case the add to card form won't work any more. The weird part is maybe every 5 to 10 times it will trigger a product to the card.

I hope somebody get an idea what's wrong here.

Kirk

Comments

rszrama’s picture

Category:bug» support
Status:Active» Closed (won't fix)

This is known behavior and a limitation of the Forms API and random Views. There are other issues about it, the gist being that Drupal must be able to rebuild the submitted form on the subsequent pageload to validate and process the submission. When you randomize the build process for the View, there's no guarantee the required form will be loaded, so Drupal just treats your POST data as invalid (or at worst a cross-site request forgery). There may be something for Views to implement to support this, but I honestly don't know how a module should do it securely.

mediapal’s picture

Ups, this sounds like a dead end?

rszrama’s picture

Yeah, unfortunately, unless there's a how-to in the Views documentation somewhere. The alternative would be to use the Rules Link Event module, http://drupal.org/project/rules_linkevent, and bind an Add to Cart Rule to the event for the Rule. I'm not sure exactly how it works, but in this way you could use a link (themed like a button even) in your View to handle the Add to Cart. Not ideal, but may be the best quick solution if randomization is a must.

If it's just a block showing a few random products, perhaps you can enable caching on the View so it shows the same set for at least a little while. That could still fail if someone straddles the line between a refresh, which could lead to a lost Add to Cart.

mediapal’s picture

To complicated to me.

Thanks a lot anyway.

Kirk

code-brighton’s picture

Ryan, I've tried caching the view as you suggested but this doesn't seem to have any impact on this issue? The products still are not added to the cart. Even though now the random products remain consistent (in my block of 3) for 1 hour due to the caching settings.
I would say random highlighted products displayed in a block in this way would be a pretty standard feature on most shopping sites, so I think this needs to be tackled in some way.
Thanks
Joe

rszrama’s picture

Ok, it's probably then that it's caching the form ID from the first time the block is rendered, so future form submissions are reusing the now invalid (or invalid for that user) form ID. There's probably no good solution here, then... the best you could do is find a Rules solution to process the Add to Cart or at least have a "read more" link themed to look like a button that leads to the product page.

adam.t’s picture

I'd like to share an easy workaround to this problem.

Install the Rules Link module. Configure a link, so that it is attached to your product content type, and its reaction rule is to add a product to the cart. When done, display this link any way you want (views field, display modes field, etc), and that's it!

I love this solution because it doesn't require you to deep hacking into Drupal, and you won't even have to write your own module.

Hope some people will find this helpful.

Tab10id’s picture

I use http://drupal.org/project/views_random_seed module for that problem. It is not ideal too, but it worked with forms.

---
Sorry for my English

udaf’s picture

thanks!

udaf’s picture

Issue summary:View changes

misspelling

GoddamnNoise’s picture

Issue summary:View changes

The solution in #7 works nice if you only need to add one product to the cart each time, but it doesn't work if you need the user to provide the number of products to add to the cart.

I've tested the #8 solution and it worked nice for me. This solution lets the user to provide the quantity to add to the cart for each product.