einicher.net

The Code must do the talking.

Magento2: Show grand_total in Minicart

3. August 2018

One of our customers needed the minicart to show the grand_total because of the huge quantity discounts they give. The Minicarts view file is Magento_Checkout/templates/cart/minicart.phtml. For the grand_total to work with Javascript you need to add it as data-bind attribute with getCartParam – there is only one problem: grand_total ist not part of the data the Minicart collects. To get it in there you need to overwrite the getSectionData method of \Magento\Checkout\CustomerData\Cart. Here are the two files — if you have questions, just ask them in the comments – I do not explain further cause i hate to read long explanations myself – the code must do the talking.

<div data-block="minicart" class="minicart-wrapper">
    <a class="action showcart" href="<?php /* @escapeNotVerified */ echo $block->getShoppingCartUrl(); ?>"
       data-bind="scope: 'minicart_content'">
	   <span class="cart-icon icons">
			<span class="text"><i class="fa fa-shopping-bag"></i></span>
			<span class="text-cart">
				<span class="text-mycart"><?php /* @escapeNotVerified */ echo __('My Cart'); ?></span>
				<!-- ko if: getCartParam('summary_count') -->
				<span class="total-price subtotal" data-bind="html: getCartParam('subtotal')"></span>
				<span class="total-price grand-total" data-bind="html: getCartParam('grand_total')"></span>
				<!-- /ko -->
			</span>
			<span class="counter qty empty"
				  data-bind="css: { empty: !!getCartParam('summary_count') == false }, blockLoader: isLoading">
				<span class="counter-number"><!-- ko text: getCartParam('summary_count') --><!-- /ko --></span>
				<span class="counter-label">
				<!-- ko if: getCartParam('summary_count') -->
					<!-- ko text: getCartParam('summary_count') --><!-- /ko -->
					<!-- ko i18n: 'items' --><!-- /ko -->
				<!-- /ko -->
				</span>
			</span>
        </span>
<?php
	namespace Vendor\ModuleName\CustomerData;

	class Cart extends \Magento\Checkout\CustomerData\Cart
	{
		public function getSectionData()
		{
		    $totals = $this->getQuote()->getTotals();
		    $grand_total = $totals['grand_total']->getValue();

		    return [
		        'summary_count' => $this->getSummaryCount(),
		        'subtotal' => isset($totals['subtotal']) ? $this->checkoutHelper->formatPrice($totals['subtotal']->getValue()) : 0,
		        'grand_total' => $grand_total ? $this->checkoutHelper->formatPrice($grand_total) : '',
		        'possible_onepage_checkout' => $this->isPossibleOnepageCheckout(),
		        'items' => $this->getRecentItems(),
		        'extra_actions' => $this->layout->createBlock('Magento\Catalog\Block\ShortcutButtons')->toHtml(),
		        'isGuestCheckoutAllowed' => $this->isGuestCheckoutAllowed(),
		    ];
		}
	}
Tags: ,

2 responses to “Magento2: Show grand_total in Minicart”

  1. Olivier Lance says:

    Hey, thanks a lot for this code sample, it put me on the right track to achieve a similar thing!

    After some research based on your method I think the “proper”/Magento way of doing what you’re doing is to use a Plugin on the `\Magento\Checkout\CustomerData\Cart` and implement `afterGetSectionData`:

    di.xml
    ——–

    Module/Plugin/Checkout/CustomerData/CartTotals.php
    —————————————————————————
    class CartTotals
    {
    /* … */
    public function __construct(\Magento\Checkout\Model\Session $checkoutSession) {
    $this->checkoutSession = $checkoutSession;
    }

    public function afterGetSectionData(\Magento\Checkout\CustomerData\Cart $subject, $result)
    {
    $totals = $this->checkoutSession->getQuote()->getTotals();
    if(isset($totals[‘grand_total’])) {
    $result[‘grand_total’] = $totals[‘grand_total’]->getValueInclTax() ?: $totals[‘grand_total’]->getValue();
    }
    return $result;
    }
    }

    Thanks a lot for your help in coming to this solution 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *