You are here

commerce_checkout_login.module in Commerce Checkout Login 7

Same filename and directory in other branches
  1. 7.2 commerce_checkout_login.module

Adds an inline login form to the Account Information checkout pane.

File

commerce_checkout_login.module
View source
<?php

/**
 * @file
 * Adds an inline login form to the Account Information checkout pane.
 */

/**
 * 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.
 */
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',
          ),
        );
      }
    }
  }
}

/**
 * Ajax callback: returns the account information pane to an AJAX request.
 */
function commerce_checkout_login_checkout_form_refresh($form, $form_state) {
  return $form['account']['login'];
}

/**
 * Validate callback: validate the user credentials
 */
function commerce_checkout_login_checkout_form_validate($form, &$form_state) {

  // @todo Add flood control support as in user_login_authenticate_validate().
  $account = user_load_by_mail($form_state['values']['account']['login']['mail']);

  // If the user account is blocked...
  if (user_is_blocked($account->name)) {

    // Display an appropriate error message.
    form_set_error('account][login][email', t('The username %name has not been activated or is blocked.', array(
      '%name' => $account->name,
    )));
  }
  elseif ($uid = user_authenticate($account->name, $form_state['values']['account']['login']['password'])) {

    // Otherwise, if the user authenticates based on the name of the loaded
    // account and the supplied password, retain the uid to login the user on
    // final submission.
    $form_state['commerce_checkout_login_uid'] = $uid;
  }
  else {

    // But indicate that we could not login the user if validation failed.
    form_set_error('account][login][password', t('Sorry, unrecognized username or password. <a href="@password">Have you forgotten your password?</a>', array(
      '@password' => url('user/password'),
    )));
  }
}

/**
 * Submit callback: attempt a login of the user now.
 */
function commerce_checkout_login_checkout_form_submit($form, &$form_state) {
  global $user;
  if (!empty($form_state['commerce_checkout_login_uid'])) {

    // Load the specified user into the global $user variable.
    $user = user_load($form_state['commerce_checkout_login_uid']);

    // "Finalize" the login by triggering the appropriate system messages, IP
    // address and login timestamp logging, and user login hook.
    user_login_finalize();

    // Convert the current cart order to an authenticated cart for the current
    // user and clear out our variable from the form state.
    commerce_cart_order_convert($form_state['order'], $user);
    unset($form_state['commerce_checkout_login_uid']);
  }
}

Functions

Namesort descending Description
commerce_checkout_login_checkout_form_refresh Ajax callback: returns the account information pane to an AJAX request.
commerce_checkout_login_checkout_form_submit Submit callback: attempt a login of the user now.
commerce_checkout_login_checkout_form_validate Validate callback: validate the user credentials
commerce_checkout_login_form_commerce_checkout_form_alter Implements hook_form_FORM_ID_alter().