diff --git a/modules/cart/commerce_cart.module b/modules/cart/commerce_cart.module index ebdf315ae85cd61d342669db7e76204b9bcfc681..8661af86630902b0f2096e76b32fe4a14ec37cc0 100644 --- a/modules/cart/commerce_cart.module +++ b/modules/cart/commerce_cart.module @@ -46,6 +46,7 @@ function commerce_cart_theme($existing, $type, $theme, $path) { 'url' => NULL, 'links' => [], 'dropdown' => FALSE, + 'total' => NULL, ], ], 'commerce_cart_empty_page' => [ diff --git a/modules/cart/src/Plugin/Block/CartBlock.php b/modules/cart/src/Plugin/Block/CartBlock.php index 9faee11396ff5fc965c3190e3480015548cc5310..88aaaf8b9344aa0e1387bd6d6386816907d36ece 100644 --- a/modules/cart/src/Plugin/Block/CartBlock.php +++ b/modules/cart/src/Plugin/Block/CartBlock.php @@ -2,6 +2,7 @@ namespace Drupal\commerce_cart\Plugin\Block; +use Drupal\commerce_price\Price; use Drupal\Core\Block\Attribute\Block; use Drupal\Core\Block\BlockBase; use Drupal\Core\Cache\Cache; @@ -114,7 +115,11 @@ class CartBlock extends BlockBase implements ContainerFactoryPluginInterface { return $cart->hasItems() && $cart->cart->value; }); + /** @var \Drupal\commerce_store\Entity\StoreInterface $store */ + $store = $this->entityTypeManager->getStorage('commerce_store')->loadDefault(); + $count = 0; + $total = new Price('0', !empty($store) ? $store->getDefaultCurrencyCode() : 'USD'); $cart_views = []; if (!empty($carts)) { $cart_views = $this->getCartViews($carts); @@ -122,6 +127,9 @@ class CartBlock extends BlockBase implements ContainerFactoryPluginInterface { foreach ($cart->getItems() as $order_item) { $count += (int) $order_item->getQuantity(); } + if ($cart_total = $cart->getTotalPrice()) { + $total = $total->add($cart_total); + } $cacheable_metadata->addCacheableDependency($cart); } } @@ -160,6 +168,7 @@ class CartBlock extends BlockBase implements ContainerFactoryPluginInterface { 'contexts' => ['cart'], ], '#dropdown' => $this->configuration['dropdown'], + '#total' => $total, ]; } diff --git a/modules/cart/templates/commerce-cart-block.html.twig b/modules/cart/templates/commerce-cart-block.html.twig index 89c528c8c79eb46ee1b4184e111561fddb1d8146..97cc4e35ae6c6deb3e0ca46a1f7dd8edcc668907 100644 --- a/modules/cart/templates/commerce-cart-block.html.twig +++ b/modules/cart/templates/commerce-cart-block.html.twig @@ -12,6 +12,7 @@ * - content: rendered view of shopping carts. * - links: array of links to card page. * - dropdown: flag indicating whether content should be rendered and displayed in dropdown div. + * - total: the shopping cart total. * * @ingroup themeable */