Alter Commerce Add To Cart Form


Add to cart form in Commerce ( doesn't have a constant id. To catch and alter all add to cart forms we need to do the following trick:

Get raw version
  1. /**
  2.  * Implements hook_form_alter().
  3.  */
  4. function YOUR_MODULE_NAME_form_alter(&$form, &$form_state, $form_id) {
  5. if ((strpos($form_id, 'commerce_cart_add_to_cart_form_') === 0)) {
  6. // Do your add to cart form alterations.
  7. }
  8. }


andyg5000's picture

You can also use the hook_form_FORM_ID_alter() hook which will only be executed when that form is loaded as opposed to all forms.

  1. /**
  2.  * Implements hook_form_FORM_ID_alter().
  3.  */
  4. function YOUR_MODULE_NAME_form_commerce_cart_add_to_cart_form_alter(&$form, $form_state) {
  5. // Do your add to cart form alterations.
  6. }
marilena's picture

drupal novice asking for help!
I have a list of products in my site, when a user clicks on one a popup window is opened with the product display and the add to cart button. When the user clicks add to cart, I want the popup to close.
Can anyone suggest a snippet or where to add it, in which file of the Commerce module?