You are here

public function CartManager::completeSale in Ubercart 8.4

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

Parameters

\Drupal\uc_order\OrderInterface $order: The order entity that has just been completed.

bool $login: TRUE if the user should be logged in (where configured), FALSE otherwise.

Return value

array A render array for the default order completion page.

Overrides CartManagerInterface::completeSale

File

uc_cart/src/CartManager.php, line 76

Class

CartManager
Provides the cart manager service.

Namespace

Drupal\uc_cart

Code

public function completeSale(OrderInterface $order, $login = TRUE) {

  // Empty that cart...
  $this
    ->emptyCart();

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

    // Move an order's status from "In checkout" to "Pending".
    if ($order
      ->getStateId() == 'in_checkout') {
      $order
        ->setStatusId(uc_order_state_default('post_checkout'));
    }
    $order
      ->save();

    // Invoke the checkout complete hook.
    $account = $order
      ->getOwner();
    \Drupal::moduleHandler()
      ->invokeAll('uc_checkout_complete', [
      $order,
      $account,
    ]);

    // Trigger the checkout complete event.

    /* rules_invoke_event('uc_checkout_complete', $order); */
    $event = new CheckoutCompleteEvent($order);
    \Drupal::service('event_dispatcher')
      ->dispatch($event::EVENT_NAME, $event);
  }
  $type = $order->data->complete_sale;

  // Log in new users, if requested.
  if ($type == 'new_user' && $login && $this->currentUser
    ->isAnonymous()) {
    if (\Drupal::config('uc_cart.settings')
      ->get('new_customer_login')) {
      $type = 'new_user_logged_in';
      user_login_finalize($order
        ->getOwner());
    }
  }
  $message = \Drupal::config('uc_cart.messages')
    ->get($type);
  $message = \Drupal::token()
    ->replace($message, [
    'uc_order' => $order,
  ]);
  $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');
  $message = strtr($message, $variables);
  return [
    '#theme' => 'uc_cart_complete_sale',
    '#message' => [
      '#markup' => $message,
    ],
    '#order' => $order,
  ];
}