diff --git a/commerce_checkout_login.module b/commerce_checkout_login.module index a3f2c30..7e4a598 100644 --- a/commerce_checkout_login.module +++ b/commerce_checkout_login.module @@ -20,6 +20,56 @@ function commerce_checkout_login_form_commerce_checkout_form_account_alter(&$for } /** + * Implements hook_form_alter(). + */ +function commerce_checkout_login_form_commerce_checkout_form_checkout_alter(&$form, &$form_state, $form_id) { + // If user registration information has been submitted... + if (isset($form_state['order']->data['commerce_checkout_login_register'])) { + // And the pane has been configured to display account information... + if (variable_get('commerce_order_account_pane_auth_display', FALSE)) { + // Remove the 'login' form. + unset($form['account']['login']); + // Display account information. + $form['account']['username'] = array( + '#type' => 'item', + '#title' => t('Username'), + '#markup' => check_plain($form_state['order']->data['commerce_checkout_login_register']['username']), + ); + $form['account']['mail'] = array( + '#type' => 'item', + '#title' => t('E-mail address'), + '#markup' => check_plain($form_state['order']->data['commerce_checkout_login_register']['mail']), + ); + + } + } +} + + +/** + * Implements hook_form_alter(). + */ +function commerce_checkout_login_form_commerce_checkout_form_review_alter(&$form, &$form_state, $form_id) { + // If user registration information has been submitted... + if (isset($form_state['order']->data['commerce_checkout_login_register'])) { + // If the account review pane is shown. + if (isset($form['checkout_review']['review']['#data']['account'])) { + $content[] = array( + '#type' => 'item', + '#title' => t('Username'), + '#markup' => check_plain($form_state['order']->data['commerce_checkout_login_register']['username']), + ); + $content[] = array( + '#type' => 'item', + '#title' => t('E-mail address'), + '#markup' => check_plain($form_state['order']->data['commerce_checkout_login_register']['mail']), + ); + $form['checkout_review']['review']['#data']['account']['data'] = render($content); + } + } +} + +/** * Implements hook_commerce_checkout_pane_info(). */ function commerce_checkout_login_commerce_checkout_pane_info() { @@ -183,4 +233,54 @@ function commerce_checkout_login_order_convert($order, $account) { } return FALSE; -} \ No newline at end of file +} + +/** + * Implements hook_commerce_checkout_complete(). + */ +function commerce_checkout_login_commerce_checkout_complete($order) { + if (!empty($order->data['commerce_checkout_login_register'])) { + $username = $order->data['commerce_checkout_login_register']['username']; + $mail = $order->data['commerce_checkout_login_register']['mail']; + // Create account and login. + $account = commerce_checkout_create_account($username, $mail, user_password(), TRUE, TRUE); + + $token = drupal_random_key(); + $_SESSION['pass_reset_' . $account->uid] = $token; + $reset_link = l(t('Please set your password'), 'user/' . $account->uid . '/edit', array('query' => array('pass-reset-token' => $token))); + drupal_set_message(t('Your account has been created and you have been automatically logged in. !set_pass.', array('!set_pass' => $reset_link))); + + // Send a notification email and inform the user about it. + _user_mail_notify('register_no_approval_required', $account); + drupal_set_message(t('A welcome message with further instructions has been sent to your e-mail address.')); + + // Convert the order. + ccl_login_convert_order($account->uid, $order); + } +} + +/** + * Login user and convert his/her anonymous order. + */ +function ccl_login_convert_order($uid, $order) { + global $user; + // If the user was logged in during this request, make sure we are using the + // full user object. + if ($user->uid) { + $user = user_load($user->uid); + } + else { + // Load the validated user into the global $user variable. + $user = user_load($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 order from anonymous to authenticated and clear out + // our variable from the form state. + if (empty($order->uid)) { + commerce_checkout_login_order_convert($order, $user); + } +} diff --git a/commerce_checkout_login.panes.inc b/commerce_checkout_login.panes.inc index a15f4d9..a3a6136 100644 --- a/commerce_checkout_login.panes.inc +++ b/commerce_checkout_login.panes.inc @@ -63,7 +63,7 @@ function commerce_checkout_login_account_form($form, &$form_state, $checkout_pan '#title' => t('Username'), '#size' => 25, '#required' => TRUE, - '#default_value' => isset($defaults['username']) ? $defaults['username'] : '', + '#default_value' => isset($order->data['commerce_checkout_login_register']['username']) ? $order->data['commerce_checkout_login_register']['username'] : '', '#maxlength' => USERNAME_MAX_LENGTH, '#description' => t('Spaces are allowed; punctuation is not allowed except for periods, hyphens, apostrophes, and underscores.'), ); @@ -71,6 +71,7 @@ function commerce_checkout_login_account_form($form, &$form_state, $checkout_pan $form['select']['register']['mail'] = array( '#type' => 'textfield', '#title' => t('E-mail address'), + '#default_value' => isset($order->data['commerce_checkout_login_register']['mail']) ? $order->data['commerce_checkout_login_register']['mail'] : '', '#size' => 25, '#description' => t('A valid e-mail address. All e-mails from the system will be sent to this address. The e-mail address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by e-mail.'), '#required' => TRUE, @@ -132,13 +133,6 @@ function commerce_checkout_login_account_form_validate($form, &$form_state, $che return FALSE; } else { - // Create account and login. - $account = commerce_checkout_create_account($username, $mail, user_password(), TRUE, TRUE); - _user_mail_notify('register_no_approval_required', $account); - drupal_set_message(t('A welcome message with further instructions has been sent to your e-mail address.')); - // @todo save form data for later use. Use $order->data for this. - // set the uid to enable automatic login on submit. - $form_state['commerce_checkout_login_uid'] = $account->uid; return TRUE; } break; @@ -155,32 +149,15 @@ function commerce_checkout_login_account_form_validate($form, &$form_state, $che * Account pane submit handler. */ function commerce_checkout_login_account_form_submit($form, &$form_state, $checkout_pane, $order) { - global $user; - $order->data['test'] = 'test'; // commerce_checkout_login_uid gets set during _validate_existing_account() if // the account was validated successfully. if (!empty($form_state['commerce_checkout_login_uid'])) { - // If the user was logged in during this request, make sure we are using the - // full user object. - if (!empty($user->uid)) { - $user = user_load($user->uid); - } - else { - // Load the validated 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 order from anonymous to authenticated and clear out - // our variable from the form state. - if (empty($order->uid)) { - commerce_checkout_login_order_convert($order, $user); - } + ccl_login_convert_order($form_state['commerce_checkout_login_uid'], $order); unset($form_state['commerce_checkout_login_uid']); } + elseif (!empty($form_state['values']['account_form']['select']['register'])){ + $order->data['commerce_checkout_login_register'] = $form_state['values']['account_form']['select']['register']; + } } function ccl_fetch_mail($form_state) {