diff --git a/commerce_checkout_login.info b/commerce_checkout_login.info index 89359e4..a25c032 100644 --- a/commerce_checkout_login.info +++ b/commerce_checkout_login.info @@ -3,6 +3,7 @@ description = Adds an inline login form to the Account Information checkout pane package = Commerce (contrib) dependencies[] = commerce_checkout dependencies[] = commerce_order +dependencies[] = entity core = 7.x ; Simple tests diff --git a/commerce_checkout_login.module b/commerce_checkout_login.module index e812f22..5f10c36 100644 --- a/commerce_checkout_login.module +++ b/commerce_checkout_login.module @@ -5,6 +5,51 @@ * Adds an inline login form to the Account Information checkout pane. */ +/** + * Implements hook_form_FORM_ID_alter(). + * + * Capture checkout pane settings + */ +function commerce_checkout_login_form_commerce_checkout_pane_settings_form_alter(&$form, &$form_state, $form_id) { + // Exit if not account checkout pane + if (empty($form['checkout_pane']) || empty($form['checkout_pane']['#value']) || + empty($form['checkout_pane']['#value']['pane_id']) || $form['checkout_pane']['#value']['pane_id'] != 'account') { + return; + } + + $form['settings']['commerce_checkout_login'] = array( + '#type' => 'fieldset', + '#title' => t('Commerce checkout login configuration'), + ); + $settings_form = &$form['settings']['commerce_checkout_login']; + + $settings_form['commerce_checkout_login_existing_user_login_required'] = array( + '#type' => 'checkbox', + '#title' => t('Require login of existing users.'), + '#description' => t('If checked, the password field is required to proceed to the next checkout page. The customer will be logged in and the order converted to an authenticated user order.'), + '#default_value' => variable_get('commerce_checkout_login_existing_user_login_required', FALSE), + ); + + $settings_form['commerce_checkout_login_new_user_create'] = array( + '#type' => 'checkbox', + '#title' => t('Automatically create a user account for new customers'), + '#description' => t('If checked, a new user will be created based on the order\'s email address. The user is created on submit of the checkout page.'), + '#default_value' => variable_get('commerce_checkout_login_new_user_create', FALSE), + ); + $settings_form['commerce_checkout_login_new_user_login'] = array( + '#type' => 'checkbox', + '#title' => t('Automatically login the new user.'), + '#description' => t('If checked, the new user will be logged in and the order converted to an authenticated user order.'), + '#default_value' => variable_get('commerce_checkout_login_new_user_login', FALSE), + '#prefix' => '
', + '#suffix' => '
', + '#states' => array( + 'invisible' => array( + ':input[name="commerce_checkout_login_new_user_create"]' => array('checked' => FALSE), + ), + ), + ); +} /** * Implements hook_form_FORM_ID_alter(). @@ -34,6 +79,8 @@ function commerce_checkout_login_form_commerce_checkout_form_alter(&$form, &$for form_set_error('account][login][mail', $error); } elseif ($account = user_load_by_mail($mail)) { + $login_required = variable_get('commerce_checkout_login_existing_user_login_required', FALSE); + // 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)); @@ -41,6 +88,7 @@ function commerce_checkout_login_form_commerce_checkout_form_alter(&$form, &$for $form['account']['login']['password'] = array( '#type' => 'password', '#title' => t('Password'), + '#required' => $login_required, ); $form['account']['login']['login_now'] = array( @@ -50,6 +98,18 @@ function commerce_checkout_login_form_commerce_checkout_form_alter(&$form, &$for '#validate' => array('commerce_checkout_login_checkout_form_validate'), '#submit' => array('commerce_checkout_login_checkout_form_submit'), ); + + // Add validate and submit to overall checkout form if login is required + if ($login_required && isset($form['buttons']['continue'])) { + $form['buttons']['continue']['#validate'][] = 'commerce_checkout_login_checkout_form_validate'; + $form['buttons']['continue']['#submit'][] = 'commerce_checkout_login_checkout_form_submit'; + } + } + elseif (variable_get('commerce_checkout_login_new_user_create', FALSE)) { + // New user creation + if (isset($form['buttons']['continue'])) { + $form['buttons']['continue']['#submit'][] = 'commerce_checkout_login_checkout_form_new_user_submit'; + } } } } @@ -87,7 +147,7 @@ function commerce_checkout_login_checkout_form_validate($form, &$form_state) { } /** - * Submit callback: attempt a login of the user now. + * Submit callback: attempt a login an existing user. */ function commerce_checkout_login_checkout_form_submit($form, &$form_state) { global $user; @@ -106,3 +166,60 @@ function commerce_checkout_login_checkout_form_submit($form, &$form_state) { unset($form_state['commerce_checkout_login_uid']); } } + +/** + * Submit callback: attempt to create the user and login. + */ +function commerce_checkout_login_checkout_form_new_user_submit($form, &$form_state) { + global $user; + + $values = &$form_state['values']; + + // Exit if user has logged in since the pane form was created + if (!empty($user->uid)) { + /** @todo: check $order->uid and commerce_cart_order_convert() ? ******/ + return; + } + + // Load a fresh order object + $order = commerce_order_load($form_state['order']->order_id); + + // Ensure order mail is as entered in form + if (empty($order->mail) && !empty($values['account']['login']['mail'])) { + $order->mail = $values['account']['login']['mail']; + } + + // Wrap the order object + $order_wrapper = entity_metadata_wrapper('commerce_order', $order); + + // Get generated user name + $username = $order_wrapper->mail_username->value(); + + // Exit if user got created already + if (user_load_by_name($username) || (!empty($order->mail) && user_load_by_mail($order->mail))) { + return; + } + + // Create new user + $account = user_save(NULL, array( + 'name' => $username, + 'mail' => $order->mail, + 'init' => $order->mail, + 'status' => 1, + 'timezone' => variable_get('user_default_timezone', DRUPAL_USER_TIMEZONE_DEFAULT) ? '' : variable_get('date_default_timezone', ''), + )); + + if (!empty($account)) { + if (variable_get('commerce_checkout_login_new_user_login', FALSE)) { + // Load the specified user into the global $user variable. + $user = $account; + + // "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 + commerce_cart_order_convert($order, $user); + } + } +}