Hi all,
todas I pulled out my hair on a crazy #ajax problem... as anonymous user a FAPI #ajax functionality did not work, but logged in it worked...
I was searching for DC or core bugs... tried out dc_ajax_add_cart module, but had the same problem...
After several hours I found out the §§$%&"§$ reason ... ;)
We had a user login block on the page for anonymous users. Its submit button had the same ID as the commerce_cart_add_to_cart_form submit button: "edit-submit".
It may be discussed where this behaviour should be changed but I think Commerce is probably easier to fix than core. My suggestion would be to set a more unique ID using https://api.drupal.org/api/drupal/includes!common.inc/function/drupal_ht...
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add to cart'),
'#weight' => 50,
'#attributes' => array('id' => 'cart-submit'),
'#id' => 'cart-submit',
);
Comments
Comment #2
rszrama CreditAttribution: rszrama at Centarro commentedUnfortunately, we can't really make this change either. This would break any theme currently used by Drupal Commerce users that targets that submit button. My suggestion would be to use hook_form_alter() in your theme to make this change on your site.
Comment #3
AnybodyThank you very much for your quick reply. Yes, that's the way we solved it. I just wanted to share my results with you because I thought other people might run into the same problems.
Nevertheless it could be a solution to set the default id ("edit-submit") using the drupal_html_id() function in Drupal commerce so that the ID will only change in the specific case that the ID is on the page twice?
Comment #4
rszrama CreditAttribution: rszrama at Centarro commentedHmm, I would've expected Drupal core to have a solution for this; does it not append a numeric ID to at least make sure it's valid HTML? There should be many other use cases where more than one form appears on a page (e.g. search block in the header on the user login page).
Comment #5
rszrama CreditAttribution: rszrama at Centarro commentedAhh, yeah, in fact it does append the numeric ID. I think your issue may really be that you should be checking the array parents instead of just the ID of the element that was submitted.
Comment #6
pawel.traczynski CreditAttribution: pawel.traczynski as a volunteer commentedI had the same problem. I was having a searchbox printed in theme + list of products with Add to Cart buttons for each of them. Since there were many of these buttons, the solution of setting the '#id' didn't work as it caused duplicated id's ammong the Add to Cart buttons. I ended up doing:
The whole code for AJAXIFYING my add to cart form using my own module was:
The above code worked beatifully for me.
I hope it helps someone :)