uc_cart_complete_sale

Versions
5 – 6
uc_cart_complete_sale($order, $login = FALSE)

Complete 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.

▾ 4 functions call uc_cart_complete_sale()

uc_2checkout_finalize in contributions/ubercart/payment/uc_2checkout/uc_2checkout.pages.inc
uc_cart_checkout_complete in contributions/ubercart/uc_cart/uc_cart.pages.inc
Completes the sale and finishes checkout.
uc_google_checkout_new_order in contributions/ubercart/payment/uc_google_checkout/uc_google_checkout.module
uc_paypal_ipn in contributions/ubercart/payment/uc_paypal/uc_paypal.pages.inc

Code

contributions/ubercart/uc_cart/uc_cart.module, line 1189

<?php
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;
      $account = user_load($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'])) {
        $name = uc_store_email_to_username($order->primary_email);
      }
      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);

      // Send the customer their account details if enabled.
      if (variable_get('uc_new_customer_email', TRUE)) {
        // Manually set the password so it appears in the e-mail.
        $account->password = $fields['pass'];

        // Send the e-mail through the user module.
        drupal_mail('user', 'register_no_approval_required', $order->primary_email, NULL, array('account' => $account), uc_store_email_from());
      }

      // Store the login details in the session for use on the page display.
      $_SESSION['new_user'] = array('name' => $fields['name'], 'pass' => $fields['pass']);

      // Update the order's uid in this request and in the database.
      $order->uid = $account->uid;
      unset($order->data['new_user']['pass']);
      db_query("UPDATE {uc_orders} SET uid = %d, data = '%s' WHERE order_id = %d", $order->uid, serialize($order->data), $order->order_id);

      // Login the user if specified.
      if ($login) {
        $form_state = array('values' => $fields);
        drupal_execute('user_login', $form_state);
      }

      $message_type = 'new_user';
    }
  }
  else {
    if ($order->uid == $user->uid) {
      $message_type = 'logged_in';
      $account = clone $user;
    }
    else {
      $message_type = 'existing_user';
      $account = user_load($order->uid);
    }
  }

  $output = '<p>'. check_markup(variable_get('uc_msg_order_submit', uc_get_message('completion_message')),
                         variable_get('uc_msg_order_submit_format', FILTER_FORMAT_DEFAULT), FALSE) .'</p>';
  $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', FILTER_FORMAT_DEFAULT), 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', FILTER_FORMAT_DEFAULT),
                      FALSE) .'</p>';

  $output_message = token_replace_multiple($output, array('global' => NULL, 'order' => $order));
  $themed_output = theme('uc_cart_complete_sale', $output_message);

  // 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']);

  module_invoke_all('uc_checkout_complete', $order, $account);
  ca_pull_trigger('uc_checkout_complete', $order, $account);

  return $themed_output;
}
?>

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters (without spaces) shown in the image.