Hi - I wanted to have the add to cart form on my catalog pages, so added product_cart_form($node) to the teaser view for my products. Seems like a general FAPI issue with the Value form type (or docs) - in the form function $form['nid'] is set as type value with a value of the $node->nid.
If i added items to the cart lower down the page, the very first item on the page would be added to the cart each time.
I traced this, and found if I set the form['nid'] (line 1287) to be:
$form['nid'] = array(
'#type' => 'hidden',
'#default_value' => $node->nid,
);
It will then pass correct $node->nid values to the cart and the correct products were added. #default_value isn't listed as an available parameter in the docs - but it fixed this issue.
To replicate:
1) Setup a catalog page that displays teasers (I'm using views for this)
2) Add this to the node teaser view of a catalog page. print drupal_get_form('product_cart_form', $node);
3) Add the cart view block (how I first saw it...)
4) Add products lower down the catalog page, only the first item on the page will be added to the cart.
This form is usually used on a single product page, so was catching the first value.
-Greg
Comments
Comment #1
vikingew commentedPlease make a patch for it (and then set it to "Code needs review"), changed code w/o any reference to what it changed from is rather useless.
Comment #2
Anonymous (not verified) commentedSince I spent the time to both report the bug and figure out what was causing it, not sure it is fair to categorize this as useless. Will let the maintainer update if interested, it's the 4 lines above.
Seems like it needed to be investigated further since the original code in the module is correct according to the FAPI docs, not sure #value element type is working right or documented right.
Comment #3
vikingew commentedSorry if I stepped on a toe... I don't mean your efforts are useless, contrary they are very welcomed, but to be really useful to others it's nice with either a patch OR a code listing as above but which shows both before and after, preferably with - and + lines. The reason is there are many different versions in use by people, both major and minor, and they may slightly differ so it would make it easier to check also on those installations. Again, sorry if I sounded blunt, no bad intention meant.
Comment #4
brmassa commentedGreg,
you are right. hidden values doesnt have #default_value attribute.
But.. is your issue solved on changing the #default_value to just #value? if so, im commiting the change on eC4 right away.
regards,
massa
Comment #5
brmassa commentedno answer