/** * Outputs a cart view for each non-empty cart belonging to the current user. * * @return array * A render array. */ public function cartPage() { $build = []; $carts = $this->cartProvider->getCarts(); $carts = array_filter($carts, function ($cart) { return $cart->hasLineItems(); }); 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]; } } else { $build['empty'] = ['#prefix' => '<div class="cart-empty-page">', '#markup' => $this->t('Your shopping cart is empty.'), '#suffix' => '</div.']; } return $build; }
/** * Builds the cart block. * * @return array * A render array. */ public function build() { /** @var \Drupal\commerce_order\Entity\OrderInterface[] $carts */ $carts = $this->cartProvider->getCarts(); $carts = array_filter($carts, function ($cart) { /** @var \Drupal\commerce_order\Entity\OrderInterface $cart */ return $cart->hasLineItems(); }); $count = 0; $cart_views = []; if (!empty($carts)) { $cart_views = $this->getCartViews($carts); foreach ($carts as $cart_id => $cart) { foreach ($cart->getLineItems() as $line_item) { $count += (int) $line_item->getQuantity(); } } } $links = []; $links[] = ['#type' => 'link', '#title' => t('Cart'), '#url' => Url::fromRoute('commerce_cart.page')]; return ['#attached' => ['library' => ['commerce_cart/cart_block']], '#theme' => 'commerce_cart_block', '#icon' => ['#theme' => 'image', '#uri' => drupal_get_path('module', 'commerce') . '/icons/ffffff/cart.png', '#alt' => $this->t('Shopping cart')], '#count' => $count, '#item_text' => $this->configuration['item_text'], '#url' => Url::fromRoute('commerce_cart.page')->toString(), '#content' => $cart_views, '#links' => $links]; }