0

I need to add a custom function to the Confirm Order button on a Commerce Checkout form. I can't figure out how to call it. The button ID is #edit-continue and its displayed text is Confirm Order. I thought this would show up in the $form[submit] array, but that array is empty. So I checked $form['buttons']['continue']['#submit'], which seems to be where Drupal Commerce stores the submit functions.

I created a module and two functions.

function MYMODULE_form_commerce_checkout_form_checkout_alter($form, $form_state, $form_id){ 
   drupal_set_message('ALTERING CHECKING OUT! THIS WORKS' . $form_id);   
   $form['buttons']['continue']['#value'] = 'Send Me A Quote!';
   $form['buttons']['continue']['#submit'][] = 'MYMODULE_submit';       

function MYMODULE_submit_function($form, &$form_state) {
  die('Triggered submit');
}

drupal_set_message() shows the message, but the submit function isn't invoked, and the text in the button is not altered. So clearly, the problem is that I am not targeting the button properly. How do I "address" that button?

To make it clearer, I need to overload that button, not replace the commerce checkout functionality.

PS: I have no access to the console or Devel. So I'm limited to debugging with var_dump().

1
  • So what you're trying to achieve with your custom php? Maybe you should try Rules ?
    – Lukas C
    Commented Jun 8, 2013 at 8:22

3 Answers 3

1

Your changes to $form here are lost because you are passing by value. Pass by reference using &$form instead:

function MYMODULE_form_commerce_checkout_form_checkout_alter(&$form, &$form_state, $form_id) {
2
  • Also have a look at this for more information on passing variables as references in PHP: php.net/manual/en/language.references.pass.php
    – berliner
    Commented Mar 11, 2014 at 22:57
  • Yes, this is correct. Since $form is an array, the hook needs to define its parameter as &$form. If $form were an object, using $form as parameter would let the changes be passed to the calling function.
    – avpaderno
    Commented Sep 6, 2020 at 11:00
0

You must be using hook form alter so do a dpm($form) before adding the custom function to the submit handler and then do dpm($form) again after adding your custom handler and see if it shows your custom handler over there or not. dpm() will work if you have devel module installed otherwise do a var_dump() instead of dpm(), debugging the code should take you to the answer.

2
  • I don't have Devel installed. I did a var_dump() on the $form array and the 'Confirm' button is in the ['buttons'] section. I added this above. Any ideas?
    – jchwebdev
    Commented Jun 8, 2013 at 17:25
  • Also... another possible noob question: why doesn't the #ID attribute of the button match anything in the $form array?
    – jchwebdev
    Commented Jun 8, 2013 at 22:26
0

Based solely on the code you gave above, you've got at least one glaring issue. This code is okay:

function MYMODULE_form_commerce_checkout_form_checkout_alter($form, $form_state, $form_id){ 
   drupal_set_message('ALTERING CHECKING OUT! THIS WORKS' . $form_id);   
   $form['buttons']['continue']['#value'] = 'Send Me A Quote!';
   $form['buttons']['continue']['#submit'][] = 'MYMODULE_submit';  

However, in this block:

function MYMODULE_submit_function($form, &$form_state) {
  die('Triggered submit');
}

MYMODULE_submit_function should actually be the same as the handler you added in the form alter (i.e. MYMODULE_submit without the _function).

It's looking for that exact function name and won't add anything else to it like it does with the other form submit handlers that are called in drupal_get_form().

1
  • $form, which is an array, isn't passed by reference. Any change in $form is local to the function. That is why the code shown from the OP doesn't work.
    – avpaderno
    Commented Sep 6, 2020 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.