Display Grand Total Instead of Subtotal in Mini Cart Magento 2
Are you looking to display the grand total instead of the subtotal in your mini cart on Magento 2? You’re not alone! Many stores want their customers to see a clear, final price before checking out. Let’s walk through how to achieve this.
Understanding the Default Behavior
By default, the Magento mini cart displays the subtotal - which is the sum of all item prices in the cart. The grand total includes tax and any additional fees, and it’s only shown on the checkout page by default.
Changing the Mini Cart Display
To display the grand total instead of the subtotal in your mini cart, you’ll need to override the default behavior. Here’s a step-by-step guide:
-
** Locate the Mini Cart Template ** First, navigate to
app/design/frontend/{Vender}/{Theme}/Magento_Checkout/templates/cart/summary.html. -
** Open the File in Your Preferred IDE ** Use your preferred development environment (like PhpStorm) to open the
summary.htmlfile. -
** Find and Replace the Code ** Look for this line of code:
<span data-bind="i18n: 'Subtotal'"></span> -
** Add a Custom Observer ** To fetch and display the grand total, we’ll create an observer in
events.xmlunderapp/code/{Vendor}/{Module}/etc. The observer will listen to thecheckout.cart.summary.render_beforeevent. -
** Implement the Observer Class ** Create your observer class (e.g.,
GrandTotalObserver.php) and implement the logic:
public function execute(\Magento\Framework\Event\Observer $observer)
{
/** @var \Magento\Checkout\Model\Cart $cart */
$cart = $observer->getEvent()->getData('quote');
$grandTotal = $cart->getGrandTotal();
return $this;
}
- ** Update the Template **
Back in
summary.html, replace<span data-bind="i18n: 'Subtotal'"></span>with:
<span data-bind="text: getFormattedPrice($data.getGrandTotal())"></span>
- ** Rebuild and Clean Cache **
After making these changes, run the following commands in your terminal:
bin/magento setup:upgradebin/magento cache:clean
Using Magento 2 Extensions for Easier Upgrades
If you’re using an extension like Porto or Uptempo, they often provide built-in functionality to display grand totals. Check the documentation of your theme for these features.
For example, if you’re using the Porto theme from Themeco, look for the Custom Code section under the Theme Options > Mini Cart. You should see options for displaying a total including tax.
Conclusion
Displaying the grand total instead of subtotal in your Magento 2 mini cart can significantly improve the user experience. By following this guide, you’ll have clear instructions to make the change yourself or understand how to implement it using an extension from reputable vendors like Themeco (check out their official documentation: https://themeco.com/magento-ports/documentation/).
Remember that customizations like these should be thoroughly tested, especially before a live deployment. Always keep your Magento version and extensions up-to-date for optimal performance and security.
Happy coding!