1

The module "commerce_checkout_buttons" alters the buttons in the commerce checkout process as seen below in using HOOK_form_alter. In this code, only the review form is being altered, but the changes seem to be global in the checkout process?

For example, the site I'm working with has a one-page checkout, and the Review pane doesn't include anything. Yet, the buttons are altered.

Can anyone explain how this works?

function commerce_checkout_buttons_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'commerce_checkout_form_checkout':
    case 'commerce_checkout_form_shipping':
    case 'commerce_checkout_form_review':
      if (isset($form['buttons']['back']['#value'])) {
        $form['buttons']['back']['#value'] = t('Back');
      }
      if (isset($form['buttons']['continue']['#value'])) {
        $form['buttons']['continue']['#value'] = t('Continue');
      }
      if (isset($form['buttons']['back']['#prefix'])) {
        unset($form['buttons']['back']['#prefix']);
      }
      if (isset($form['buttons']['cancel']['#prefix'])) {
        unset($form['buttons']['cancel']['#prefix']);
      }
      break;
  }
}
2

The problem was me not properly understanding how fallthrough works in PHP's switch statement.

Without break statements, once a case statement is found with a value that matches the value of the switch expression, PHP will continue to execute the statements until the end of the switch block.

In this case, the match occurs with form_id 'commerce_checkout_form_checkout' and PHP executes all statements under 'commerce_checkout_form_review'. So, effectively, this code alters checkout buttons by matching all three of the form_ids listed.

2

If you remove the code shown below, it would only affect the review pane.

case 'commerce_checkout_form_checkout':
    break;
case 'commerce_checkout_form_shipping':
    break;
  • Thanks. This helped me understand that I didn't understand the PHP switch statement :) I added my own answer to hopefully make this clear. – Dave Bruns Feb 6 '14 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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