You are here

public function CartController::cartPage in Commerce Core 8.2

Outputs a cart view for each non-empty cart belonging to the current user.

Return value

array A render array.

1 string reference to 'CartController::cartPage'
commerce_cart.routing.yml in modules/cart/commerce_cart.routing.yml
modules/cart/commerce_cart.routing.yml

File

modules/cart/src/Controller/CartController.php, line 47

Class

CartController
Provides the cart page.

Namespace

Drupal\commerce_cart\Controller

Code

public function cartPage() {
  $build = [];
  $cacheable_metadata = new CacheableMetadata();
  $cacheable_metadata
    ->addCacheContexts([
    'user',
    'session',
  ]);
  $carts = $this->cartProvider
    ->getCarts();
  $carts = array_filter($carts, function ($cart) {

    /** @var \Drupal\commerce_order\Entity\OrderInterface $cart */
    return $cart
      ->hasItems();
  });
  if (!empty($carts)) {
    $cart_views = $this
      ->getCartViews($carts);
    foreach ($carts as $cart_id => $cart) {
      $build[$cart_id] = [
        '#prefix' => '<div class="cart cart-form">',
        '#suffix' => '</div>',
        '#type' => 'view',
        '#name' => $cart_views[$cart_id],
        '#arguments' => [
          $cart_id,
        ],
        '#embed' => TRUE,
      ];
      $cacheable_metadata
        ->addCacheableDependency($cart);
    }
  }
  else {
    $build['empty'] = [
      '#theme' => 'commerce_cart_empty_page',
    ];
  }
  $build['#cache'] = [
    'contexts' => $cacheable_metadata
      ->getCacheContexts(),
    'tags' => $cacheable_metadata
      ->getCacheTags(),
    'max-age' => $cacheable_metadata
      ->getCacheMaxAge(),
  ];
  return $build;
}