I first migrated my shop to the online server and had the nasty problem of the UID 0 dropping off and hence the anonymous cart failing to work.

The next problem I had, was tracked down to using views for one of my shop's pages where it was pulling 8 randomly chosen products and displaying them. Here products failed to add to cart from too. And the problem was intermittent. Sometimes they added, sometimes they didn't.

If I changed page the products added fine. Happened on beta 5 and beta 4.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

DigitalFrontiersMedia’s picture

Status: Active » Postponed (maintainer needs more info)

Not sure what you mean by "changed page". Can you detail this a bit more. And maybe export your View and post it here?

rszrama’s picture

Priority: Critical » Normal
Status: Postponed (maintainer needs more info) » Fixed
Issue tags: +dc2009 code sprint

Hmm... I'm going to take a stab at this and mark it fixed... we can always reopen if necessary. Basically, this isn't a bug report with Ubercart but a limitation of randomized Views and the Forms API. When the add to cart form is clicked, it submits the POST data for the form to the same URL. The View is then rebuilt on the following pageload, but it's never normally displayed to the user. Drupal depends on having the form built in the following pageload, so it will only process the add to cart when the random products chosen include the product that was added to the cart. Drupal can then process that form and do redirect upon its completion.

jptaranto’s picture

So basically add to cart will not work when using a random view?

rszrama’s picture

Correct, at least without some custom module ensuring that the form you're submitting will always be present on the regenerated page.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

ñull’s picture

Version: 6.x-2.0-beta5 » 6.x-2.0-rc3
Status: Closed (fixed) » Active
FileSize
31.01 KB

I have a random order "special offer" block on the front page that only shows one node. The add to cart button can be included in two ways: by including the add to cart button or by including the the form field. Both have the same problem that they wont add the product to the cart. Here a dump of the form HTML:

<form action="/es" accept-charset="UTF-8" method="post" id="uc-product-add-to-cart-form-55">
<div><input name="qty" id="edit-qty" value="1" type="hidden">
<input name="op" id="edit-submit-55" value="Add to cart" class="form-submit node-add-to-cart" type="submit">
<input name="form_build_id" id="form-8d7281a887b042445ff99e0583c6c6a7" value="form-8d7281a887b042445ff99e0583c6c6a7" type="hidden">
<input name="form_token" id="edit-uc-product-add-to-cart-form-55-form-token" value="cadc335354f677502fa3f7cc1f3809a3" type="hidden">
<input name="form_id" id="edit-uc-product-add-to-cart-form-55" value="uc_product_add_to_cart_form_55" type="hidden">

</div></form>

Attached the exported view. The "ofertas" block begins at line 980. Hope someone can fix this!

rszrama’s picture

Status: Active » Closed (fixed)

Same as before, the form can't submit with a random View because Drupal's Forms API depends on the same page being loaded as is with the same form being displayed. The random View causes the HTML to change between pageloads, which means that form no longer exists. If you can somehow work it out to use a cart link, that would work.

strawman’s picture

did anybody resolve this issue?
I'm still having the problem with Drupal 6.14, ubercart 2.0 and views 2.6

any thoughts? still need a exported View? is there another way to go about this?

rszrama’s picture

Please review my comment in #7. The randomized View makes form submission impossible through Drupal's Forms API, because it requires the form to be present on the page after submission. Since the View is random, this isn't always the case.

jazzitup’s picture

Version: 6.x-2.2 » 6.x-2.0-rc3
Status: Active » Closed (fixed)

+1 subscribe

edit: I accepted this workaround: http://www.ubercart.org/issue/12802/add_cart_button_doesnt_work_views_an...
But still, problem does exist, because this is just a temporary solution.

anrikun’s picture

+1 subscribing

anrikun’s picture

Version: 6.x-2.0-rc3 » 6.x-2.2
Status: Closed (fixed) » Active
TR’s picture

Version: 6.x-2.0-rc3 » 6.x-2.2

Please read the comments in this thread. There is no bug here.

sphism’s picture

Erm... buy now button doesn't allow the user to buy now - surely that's a bug

for example, couldn't the buy now button fire off a function which does actually buy now - kinda like cart links

Or perhaps the button could read "buy now unless this page happens to be random or cached"

dukem’s picture

FileSize
328 bytes

I used hook_views_query_alter() to make this work. See the attached file as an example.
My setup - drupal 6.19, views-6.x-2.11, ubercart-6.x-2.4.

tm01xx’s picture

The workaround works perfectly for me. I have spent a day on this bug and fortunately got your fix at the end.

For long term, this should go into the core of ubercart really.

jnettik’s picture

@dukem

Your workaround is awesome. Thank you. I had to make one small tweak to get it working for me:

function site_hacks_views_query_alter(&$view, &$query) {
  if($view->name == 'uc_products') {
    if ($_POST != array() && strpos($_POST['form_id'], 'uc_catalog_buy_it_now_form') !== false) {
	    $tmp = explode('_', $_POST['form_id']);
	    $nid = $tmp[count($tmp)-1];
	    $query->add_where('0', "node.nid = %d", $nid);
    }
  }
}

Had to change the form id value in the 3rd line was all.

designate’s picture

How/ where to apply this workaround [UC version 6.x-2.7]?

jnettik’s picture

designate: It should go in a custom module. In my case it was a module named site_hacks.module.

SilviuChingaru’s picture

Version: 6.x-2.2 » 7.x-3.x-dev
Status: Closed (fixed) » Active

Still present in D7 3.0... I think this should be fixed in core.

rszrama’s picture

Status: Active » Closed (fixed)

Implementing the hack basically bypasses essential access control. A user could theoretically POST data to the form that then adds a node to the Views result for a product they otherwise wouldn't be able to see. The problem isn't just that the product isn't in the View on the refresh, it's that the form isn't being processed because it's never being built. In other words - this POST is not trusted.

longwave’s picture

http://drupal.org/project/views_random_seed might be an option where Global: Random fails.

rszrama’s picture

Thinking about it some more - it may be that Views could still alter the query to apply node access since it's using the Views query builder... maybe someone can confirm? (That said, we still can't trust the POST data, so we'd at least need to figure out a way to validate the form ID w/ the user and form token.)

designate’s picture

#22 provides perfect solution - in my case. Playing around with the custom reset seed to meet your personal needs. I do believe not to set this interval to short so customers have time to notice the block with random products and buy product directly thru buy now button. Thanks longwave for reply!

hockey2112’s picture

Issue summary: View changes

#22 works for me, to an extent. When the seed is set very small (1 second, 5 seconds, etc), the add to cart button often fails. I recommend making the seed duration at least a minute or two (in line with Designate's advice in #24), although users who load the block at 1:59 of the 2:00 minutes will likely have "add to cart" issues.

longwave’s picture

It might be helpful if we added a hook_views_analyze to detect the combination of Add to Cart fields and Global: Random, and suggest switching to views_random_seed instead.

hockey2112’s picture

I ran into this issue again, 3 years later, with the sort "Global: Random" setting. Has anyone come across a solution better than Views Random Seed?

rszrama’s picture

No, there's no better option. It's just a limitation of the way the Forms API works.

ncswi’s picture

I can reproduce this problem and I can fix this problem.

It's in the Views caching. If you set the views cache to time-based (custom) and adjust it to 5 seconds, then this makes the add to cart work (assuming the process to add a product to the cart takes 5 seconds.

Once the views cache is cleared, you can add another product to the cart.

While this is not optimal as it causes longer page loads on the catalog pages, it does solve the problem until a better solutions can be implemented. D7