From 3c482eda6256a42986e46818312a61d4b0835cd7 Mon Sep 17 00:00:00 2001 From: TonyTheFerg <tony@greatlakestapingtools.com> Date: Tue, 4 Mar 2025 06:25:15 -0500 Subject: [PATCH 1/2] Issue #2982938: Display order total in cart block --- modules/cart/commerce_cart.module | 1 + modules/cart/src/Plugin/Block/CartBlock.php | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/modules/cart/commerce_cart.module b/modules/cart/commerce_cart.module index ebdf315ae..8661af866 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 9faee1139..88aaaf8b9 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, ]; } -- GitLab From 69d8ea4ac11155939b49b4a9beefa4f88a8315ba Mon Sep 17 00:00:00 2001 From: TonyTheFerg <tony@greatlakestapingtools.com> Date: Tue, 4 Mar 2025 06:36:05 -0500 Subject: [PATCH 2/2] add variable description to template --- modules/cart/templates/commerce-cart-block.html.twig | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/cart/templates/commerce-cart-block.html.twig b/modules/cart/templates/commerce-cart-block.html.twig index 89c528c8c..97cc4e35a 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 */ -- GitLab