Changing the Order of Totals in Magento Shopping Cart
As an experienced e-commerce developer, I understand how crucial it is to meet your clients’ specific requirements, even when it comes to the seemingly minor details like the order of totals in a shopping cart. In this post, we’ll guide you through changing the display sequence of subtotal, tax, shipping charge, and grand total in Magento.
Understanding the Default Behavior
In standard Magento installations, the default display order is Subtotal â Tax â Shipping Charge â Grand Total. This structure is fixed and controlled by the core files and templates of the platform.
However, with some careful customization, we can modify this sequence to fit your business needsâsuch as displaying shipping charges before tax.
Where to Make the Changes
To achieve this, you’ll need to edit two key files:
app/code/core/Mage/Sales/Model/Quote.phpapp/design/frontend/default/[your-theme]/template/checkout/cart/totals.phtml
Let’s dive into each file and see what needs to be modified.
Editing app/code/core/Mage/Sales/Model/Quote.php
In this core file, you’ll find the method getTotals(). This is where Magento calculates and organizes your cart totals. To change the order of shipping charges and tax totals, update the following lines:
'total shipping', $shipping
'total tax', $tax,
Simply swap their positions to this:
'total tax', $tax,
'total shipping', $shipping
This will invert the order of tax and shipping in your cart.
Editing app/design/frontend/default/[your-theme]/template/checkout/cart/totals.phtml
The template file totals.phtml is responsible for rendering the actual HTML output of the totals. In this file, look for the section where Magento loops through the $totals array.
foreach ($totals as $code => $total) {
// Rest of the loop content...
}
You can leverage Magento’s built-in sorting capabilities to organize your totals by their names ($total['title']). Update this line:
asort($totals, SORT_STRING);
This will sort the $totals array alphabetically based on the title. Since ‘Shipping’ comes before ‘Tax’ in most languages, this will ensure that shipping appears first.
Conclusion
By editing these two filesâQuote.php and totals.phtmlâyou can successfully rearrange your Magento shopping cart totals to display as requested: Subtotal â Shipping Charge â Tax â Grand Total.
Remember that modifying core files is generally discouraged by Magento, as it may cause issues during upgrades. For a cleaner separation of concerns, consider using an extension or creating your own module for this customization.
If you’re looking for professional Magento development services or need expert guidance on other e-commerce projects, don’t hesitate to reach out to Laravel Company. Our team specializes in delivering high-quality, optimized solutions tailored to meet your business objectives.