You are here

function uc_cart_complete_sale_account in Ubercart 6.2

Same name and namespace in other branches
  1. 7.3 uc_cart/uc_cart.module \uc_cart_complete_sale_account()

Link a completed sale to a user.

Parameters

$order: The order object that has just been completed.

1 call to uc_cart_complete_sale_account()
uc_cart_complete_sale in uc_cart/uc_cart.module
Completes a sale, including adjusting order status and creating user account.

File

uc_cart/uc_cart.module, line 1265

Code

function uc_cart_complete_sale_account($order) {

  // Order already has a user ID, so the user was logged in during checkout.
  if ($order->uid) {
    $order->data['complete_sale'] = 'logged_in';
    return;
  }
  $result = db_query("SELECT uid FROM {users} WHERE LOWER(mail) = LOWER('%s')", $order->primary_email);

  // Email address matches an existing account.
  if ($account = db_fetch_object($result)) {
    $order->uid = $account->uid;
    $order->data['complete_sale'] = 'existing_user';
    return;
  }

  // Set up a new user.
  $fields = array(
    'name' => uc_store_email_to_username($order->primary_email),
    'mail' => $order->primary_email,
    'init' => $order->primary_email,
    'pass' => user_password(),
    'roles' => array(),
    'status' => variable_get('uc_new_customer_status_active', TRUE) ? 1 : 0,
  );

  // Override the username, if specified.
  if (isset($order->data['new_user']['name'])) {
    $fields['name'] = $order->data['new_user']['name'];
  }

  // Create the account.
  $account = user_save('', $fields);

  // Override the password, if specified.
  if (isset($order->data['new_user']['hash'])) {
    db_query("UPDATE {users} SET pass = '%s' WHERE uid = %d", $order->data['new_user']['hash'], $account->uid);
    $account->password = t('Your password');
  }
  else {
    $account->password = $fields['pass'];
    $order->password = $fields['pass'];
  }

  // Send the customer their account details if enabled.
  if (variable_get('uc_new_customer_email', TRUE)) {
    $type = variable_get('uc_new_customer_status_active', TRUE) ? 'register_no_approval_required' : 'register_pending_approval';
    drupal_mail('user', $type, $order->primary_email, uc_store_mail_recipient_language($order->primary_email), array(
      'account' => $account,
    ), uc_store_email_from());
  }
  $order->uid = $account->uid;
  $order->data['new_user']['name'] = $fields['name'];
  $order->data['complete_sale'] = 'new_user';
}