You are here

function commerce_checkout_login_form_commerce_checkout_form_alter in Commerce Checkout Login 7

Implements hook_form_FORM_ID_alter().

This module works by altering the checkout form to add an additional bit of AJAX to the Account Information checkout pane via this hook.

File

./commerce_checkout_login.module, line 15
Adds an inline login form to the Account Information checkout pane.

Code

function commerce_checkout_login_form_commerce_checkout_form_alter(&$form, &$form_state) {
  global $user;

  // If the Account Information pane is on the current checkout page and the
  // user is not logged in...
  if (!$user->uid && !empty($form['account'])) {
    $form['account']['login']['mail'] += array(
      '#ajax' => array(
        'callback' => 'commerce_checkout_login_checkout_form_refresh',
        'wrapper' => 'account-login-container',
      ),
    );

    // Check the form state to see if an e-mail address has been specified.
    if (!empty($form_state['values']['account']['login'])) {
      $mail = trim($form_state['values']['account']['login']['mail']);

      // Don't attempt to load the user for an invalid e-mail address.
      if ($error = user_validate_mail($mail)) {
        form_set_error('account][login][mail', $error);
      }
      elseif ($account = user_load_by_mail($mail)) {

        // If a user already exists for the given e-mail address, display a
        // message letting the customer know this.
        $form['account']['login']['mail']['#description'] = t('There is already an account registered to %mail. You can login now to use your account information during checkout.', array(
          '%mail' => $mail,
        ));
        $form['account']['login']['password'] = array(
          '#type' => 'password',
          '#title' => t('Password'),
        );
        $form['account']['login']['login_now'] = array(
          '#type' => 'submit',
          '#value' => t('Login now'),
          '#limit_validation_errors' => array(
            array(
              'account',
            ),
          ),
          '#validate' => array(
            'commerce_checkout_login_checkout_form_validate',
          ),
          '#submit' => array(
            'commerce_checkout_login_checkout_form_submit',
          ),
        );
      }
    }
  }
}