You are here

function uc_cart_complete_sale in Ubercart 5

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

Completes a sale, including adjusting order status and creating user account.

Parameters

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

$login: Whether or not to login a new user when this function is called.

Return value

The HTML text of the default order completion page.

3 calls to uc_cart_complete_sale()
uc_2checkout_complete in payment/uc_2checkout/uc_2checkout.module
uc_cart_checkout_complete in uc_cart/uc_cart.module
uc_paypal_ipn in payment/uc_paypal/uc_paypal.module

File

uc_cart/uc_cart.module, line 1758

Code

function uc_cart_complete_sale($order, $login = FALSE) {
  global $user;

  // Logic to create new user if necessary:
  if ($order->uid == 0) {

    // Check for an existing user account with the e-mail address from checkout.
    $result = db_query("SELECT uid FROM {users} WHERE mail = '%s'", $order->primary_email);

    // If it was found, update the order.
    if ($account = db_fetch_object($result)) {
      $order->uid = $account->uid;
      db_query("UPDATE {uc_orders} SET uid = %d WHERE order_id = %d", $order->uid, $order->order_id);
      $message_type = 'existing_user';
    }
    else {

      // Get a valid new username.
      if (empty($order->data['new_user']['name'])) {

        // Default to the first part of the e-mail address.
        $name = substr($order->primary_email, 0, strpos($order->primary_email, '@'));

        // Remove possible illegal characters.
        $name = preg_replace('/[^A-Za-z0-9_.-]/', '', $name);

        // Trim that value for spaces and length.
        $name = trim(substr($name, 0, USERNAME_MAX_LENGTH));

        // Make sure we don't hand out a duplicate username.
        while (db_num_rows(db_query("SELECT uid FROM {users} WHERE LOWER(name) = LOWER('%s')", $name)) > 0) {
          $name .= rand(0, 9);
        }
      }
      else {
        $name = $order->data['new_user']['name'];
      }

      // Setup the account fields array and save it as a new user.
      $fields = array(
        'name' => $name,
        'mail' => $order->primary_email,
        'init' => $order->primary_email,
        'pass' => empty($order->data['new_user']['pass']) ? user_password(variable_get('uc_pwd_length', 6)) : $order->data['new_user']['pass'],
        'roles' => array(),
        'status' => variable_get('uc_new_customer_status_active', TRUE) ? 1 : 0,
      );
      $account = user_save('', $fields);
      if (variable_get('uc_new_customer_email', TRUE)) {
        $variables = array(
          '!username' => $fields['name'],
          '!site' => variable_get('site_name', 'Drupal'),
          '!password' => $fields['pass'],
          '!uri' => $base_url,
          '!uri_brief' => substr($base_url, strlen('http://')),
          '!mailto' => $fields['mail'],
          '!date' => format_date(time()),
          '!login_uri' => url('user', NULL, NULL, TRUE),
          '!edit_uri' => url('user/' . $account->uid . '/edit', NULL, NULL, TRUE),
          '!login_url' => user_pass_reset_url($account),
        );
        $from = uc_store_email_from();
        $subject = _user_mail_text('welcome_subject', $variables);
        $body = _user_mail_text('welcome_body', $variables);
        drupal_mail('user-register-welcome', $order->primary_email, $subject, $body, $from);
      }
      $_SESSION['new_user'] = array(
        'name' => $fields['name'],
        'pass' => $fields['pass'],
      );
      $order->uid = $account->uid;
      db_query("UPDATE {uc_orders} SET uid = %d WHERE order_id = %d", $order->uid, $order->order_id);

      // Login the user if specified.
      if ($login) {
        drupal_execute('user_login', $fields);
      }
      $message_type = 'new_user';
    }
  }
  else {
    if ($order->uid == $user->uid) {
      $message_type = 'logged_in';
    }
    else {
      $message_type = 'existing_user';
    }
  }
  $output = check_markup(variable_get('uc_msg_order_submit', uc_get_message('completion_message')), variable_get('uc_msg_order_submit_format', 3), FALSE);
  $show_message = check_markup(variable_get('uc_msg_order_' . $message_type, uc_get_message('completion_' . $message_type)), variable_get('uc_msg_order_' . $message_type . '_format', 3), FALSE);
  if ($show_message != '') {
    $variables['!new_username'] = check_plain($_SESSION['new_user']['name']);
    $variables['!new_password'] = check_plain($_SESSION['new_user']['pass']);
    $output .= '<p>' . strtr($show_message, $variables) . '</p>';
  }
  $output .= '<p>' . check_markup(variable_get('uc_msg_continue_shopping', uc_get_message('continue_shopping')), variable_get('uc_msg_continue_shopping_format', 3), FALSE) . '</p>';
  $output = token_replace_multiple($output, array(
    'global' => NULL,
    'order' => $order,
  ));

  // Move an order's status from "In Checkout" to "Pending"
  $status = db_result(db_query("SELECT order_status FROM {uc_orders} WHERE order_id = %d", $order->order_id));
  if (uc_order_status_data($status, 'state') == 'in_checkout') {
    uc_order_update_status($order->order_id, uc_order_state_default('post_checkout'));
  }

  // Empty that cart...
  uc_cart_empty(uc_cart_get_id());

  // Clear our the session variables used to force the cart workflow.
  unset($_SESSION['cart_order'], $_SESSION['do_complete'], $_SESSION['new_user']);
  workflow_ng_invoke_event('checkout_complete', $order, $user->uid == 0 ? $account : $user);
  return $output;
}