You are here

function uc_cart_complete_sale in Ubercart 7.3

Same name and namespace in other branches
  1. 5 uc_cart/uc_cart.module \uc_cart_complete_sale()
  2. 6.2 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.

6 calls to uc_cart_complete_sale()
UbercartCartCheckoutTestCase::testCheckoutComplete in uc_cart/tests/uc_cart.test
Tests checkout complete functioning.
uc_2checkout_complete in payment/uc_2checkout/uc_2checkout.pages.inc
Finalizes 2Checkout transaction.
uc_cart_checkout_complete in uc_cart/uc_cart.pages.inc
Completes the sale and finishes checkout.
uc_cybersource_hop_post in payment/uc_cybersource/uc_cybersource.module
Processes a payment POST from the CyberSource Hosted Order Page API.
uc_payment_enter in payment/uc_payment/uc_payment.module
Enters a payment for an order.

... See full list

File

uc_cart/uc_cart.module, line 854

Code

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

  // Ensure we have the latest order data.
  $order->data = unserialize(db_query("SELECT data FROM {uc_orders} WHERE order_id = :order_id", array(
    ':order_id' => $order->order_id,
  ))
    ->fetchField());

  // Ensure that user creation and triggers are only run once.
  if (empty($order->data['complete_sale'])) {
    uc_cart_complete_sale_account($order);

    // Store account data.
    db_update('uc_orders')
      ->fields(array(
      'uid' => $order->uid,
      'data' => serialize($order->data),
    ))
      ->condition('order_id', $order->order_id)
      ->execute();

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

    // Invoke the checkout complete trigger and hook.
    $account = user_load($order->uid);
    module_invoke_all('uc_checkout_complete', $order, $account);
    rules_invoke_event('uc_checkout_complete', $order);
  }
  $type = $order->data['complete_sale'];

  // Log in new users, if requested.
  if ($type == 'new_user' && $login && !$user->uid) {
    $type = 'new_user_logged_in';
    $user = user_load($order->uid);
    $edit = array();
    user_login_finalize($edit);
  }
  $variables['!new_username'] = isset($order->data['new_user']['name']) ? $order->data['new_user']['name'] : '';
  $variables['!new_password'] = isset($order->password) ? $order->password : t('Your password');
  $messages = array(
    'uc_msg_order_submit' => uc_get_message('completion_message'),
    'uc_msg_order_' . $type => uc_get_message('completion_' . $type),
    'uc_msg_continue_shopping' => uc_get_message('continue_shopping'),
  );
  foreach ($messages as $id => &$message) {
    $message = variable_get($id, $message);
    $message = token_replace($message, array(
      'uc_order' => $order,
    ));
    if ($id == 'uc_msg_order_' . $type) {
      $message = strtr($message, $variables);
    }
  }
  $output = filter_xss_admin(implode(' ', $messages));

  // Empty that cart...
  uc_cart_empty();
  return array(
    '#theme' => 'uc_cart_complete_sale',
    '#message' => $output,
    '#order' => $order,
  );
}