You are here

public function CartProvider::finalizeCart in Commerce Core 8.2

Finalizes the given cart order.

Removes the cart flag from the order and saves it. If the user is anonymous, moves the cart ID from the active to the completed cart session.

Parameters

\Drupal\commerce_order\Entity\OrderInterface $cart: The cart order.

bool $save_cart: Whether to immediately save the cart or not.

Overrides CartProviderInterface::finalizeCart

File

modules/cart/src/CartProvider.php, line 115

Class

CartProvider
Default implementation of the cart provider.

Namespace

Drupal\commerce_cart

Code

public function finalizeCart(OrderInterface $cart, $save_cart = TRUE) {
  $cart->cart = FALSE;
  if ($save_cart) {
    $cart
      ->save();
  }

  // The cart is anonymous, move it to the 'completed' session.
  if (!$cart
    ->getCustomerId()) {
    $this->cartSession
      ->deleteCartId($cart
      ->id(), CartSession::ACTIVE);
    $this->cartSession
      ->addCartId($cart
      ->id(), CartSession::COMPLETED);
  }

  // Remove the cart order from the internal cache, if present.
  if (isset($this->cartData[$cart
    ->getCustomerId()][$cart
    ->id()])) {
    unset($this->cartData[$cart
      ->getCustomerId()][$cart
      ->id()]);
  }
}