You are here

public function CheckoutController::checkout in Ubercart 8.4

Builds the cart checkout page from available checkout pane plugins.

1 string reference to 'CheckoutController::checkout'
uc_cart.routing.yml in uc_cart/uc_cart.routing.yml
uc_cart/uc_cart.routing.yml

File

uc_cart/src/Controller/CheckoutController.php, line 85

Class

CheckoutController
Controller routines for the checkout.

Namespace

Drupal\uc_cart\Controller

Code

public function checkout() {
  $cart_config = $this
    ->config('uc_cart.settings');
  $items = $this->cartManager
    ->get()
    ->getContents();
  if (count($items) == 0 || !$cart_config
    ->get('checkout_enabled')) {
    return $this
      ->redirect('uc_cart.cart');
  }

  // Send anonymous users to login page when anonymous checkout is disabled.
  if ($this
    ->currentUser()
    ->isAnonymous() && !$cart_config
    ->get('checkout_anonymous')) {
    $this
      ->messenger()
      ->addMessage($this
      ->t('You must login before you can proceed to checkout.'));
    if ($this
      ->config('user.settings')
      ->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('If you do not have an account yet, you should <a href=":url">register now</a>.', [
        ':url' => Url::fromRoute('user.register', [], [
          'query' => $this
            ->getDestinationArray(),
        ])
          ->toString(),
      ]));
    }
    return $this
      ->redirect('user.login', [], [
      'query' => $this
        ->getDestinationArray(),
    ]);
  }

  // Load an order from the session, if available.
  if ($this->session
    ->has('cart_order')) {
    $order = $this
      ->loadOrder();
    if ($order) {

      // To prevent identity theft, don't use an existing order if it has
      // changed status or owner, or if there has been no activity for 10
      // minutes.
      $request_time = $this->dateTime
        ->getRequestTime();
      if ($order
        ->getStateId() != 'in_checkout' || $this
        ->currentUser()
        ->isAuthenticated() && $this
        ->currentUser()
        ->id() != $order
        ->getOwnerId() || $order
        ->getChangedTime() < $request_time - CartInterface::CHECKOUT_TIMEOUT) {
        if ($order
          ->getStateId() == 'in_checkout' && $order
          ->getChangedTime() < $request_time - CartInterface::CHECKOUT_TIMEOUT) {

          // Mark expired orders as abandoned.
          $order
            ->setStatusId('abandoned')
            ->save();
        }
        unset($order);
      }
    }
    else {

      // Ghost session.
      $this->session
        ->remove('cart_order');
      $this
        ->messenger()
        ->addMessage($this
        ->t('Your session has expired or is no longer valid.  Please review your order and try again.'));
      return $this
        ->redirect('uc_cart.cart');
    }
  }

  // Determine if the form is being submitted or built for the first time.
  if (isset($_POST['form_id']) && $_POST['form_id'] == 'uc_cart_checkout_form') {

    // If this is a form submission, make sure the cart order is still valid.
    if (!isset($order)) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('Your session has expired or is no longer valid.  Please review your order and try again.'));
      return $this
        ->redirect('uc_cart.cart');
    }
    elseif ($this->session
      ->has('uc_cart_order_rebuild')) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('Your shopping cart contents have changed. Please review your order and try again.'));
      return $this
        ->redirect('uc_cart.cart');
    }
  }
  else {

    // Prepare the cart order.
    $rebuild = FALSE;
    if (!isset($order)) {

      // Create a new order if necessary.
      $order = Order::create([
        'uid' => $this
          ->currentUser()
          ->id(),
      ]);
      $order
        ->save();
      $this->session
        ->set('cart_order', $order
        ->id());
      $rebuild = TRUE;
    }
    elseif ($this->session
      ->has('uc_cart_order_rebuild')) {

      // Or, if the cart has changed, then remove old products and line items.
      $result = \Drupal::entityQuery('uc_order_product')
        ->condition('order_id', $order
        ->id())
        ->execute();
      if (!empty($result)) {
        $storage = $this
          ->entityTypeManager()
          ->getStorage('uc_order_product');
        $entities = $storage
          ->loadMultiple(array_keys($result));
        $storage
          ->delete($entities);
      }
      uc_order_delete_line_item($order
        ->id(), TRUE);
      $rebuild = TRUE;
    }
    if ($rebuild) {

      // Copy the cart contents to the cart order.
      $order->products = [];
      foreach ($items as $item) {
        $order->products[] = $item
          ->toOrderProduct();
      }
      $this->session
        ->remove('uc_cart_order_rebuild');
    }
    elseif (!uc_order_product_revive($order->products)) {
      $this
        ->messenger()
        ->addError($this
        ->t('Some of the products in this order are no longer available.'));
      return $this
        ->redirect('uc_cart.cart');
    }
  }
  $min = $cart_config
    ->get('minimum_subtotal');
  if ($min > 0 && $order
    ->getSubtotal() < $min) {
    $this
      ->messenger()
      ->addError($this
      ->t('The minimum order subtotal for checkout is @min.', [
      '@min' => uc_currency_format($min),
    ]));
    return $this
      ->redirect('uc_cart.cart');
  }

  // Invoke the customer starts checkout hook.
  $this
    ->moduleHandler()
    ->invokeAll('uc_cart_checkout_start', [
    $order,
  ]);

  // Trigger the checkout start event.

  /* rules_invoke_event('uc_cart_checkout_start', $order); */
  $event = new CheckoutStartEvent($order);
  \Drupal::service('event_dispatcher')
    ->dispatch($event::EVENT_NAME, $event);
  return $this
    ->formBuilder()
    ->getForm('Drupal\\uc_cart\\Form\\CheckoutForm', $order);
}