Changing the Order of Totals in Magento Transactional Email Templates
As a Magento developer, you may find yourself needing to adjust the order of totals on transactional email templates from time to time. In this post, we'll guide you through the process of moving the 'Tax' row below 'Grand total incl. tax'.
Understanding the Current Code Structure
The code you provided is responsible for iterating through the totals and printing each row in the order they are returned by $this->getTotals(). The order of these rows is determined by the Magento core during processing. However, if you want to manually control the order, we need to override this part.
Overriding the Code to Change Order
To achieve this, you'll need to follow these steps:
- Create a new file in your theme directory. You can name it something like
app/design/frontend/Vendor/Theme/Magento_Tax/templates/order/totals.phtml. - In this new file, copy the code from the original totals.phtml (which is probably located at
vendor/magento/module-tax/view/frontend/templates/order/totals.phtml). - Next, modify the foreach loop in the copied template. We'll manually define the order of our total rows:
<?php $totalBlocks = [ ‘grand_total’ => ‘Magento_Tax/block.totals.grandtotal’, ‘tax’ => ‘Magento_Tax/block.totals.tax’ ]; foreach ($totalBlocks as $_code => $_blockName): <?php if ($_blockName): ?> <?php echo $this->getChildHtml($_blockName, false); ?> <?php else:?> // Rest of the code here… <?php endif?> </pre>Here, we're defining an array called
$totalBlocks. The keys represent the total codes (like 'grand_total' and 'tax'), and the values are the block names. We've manually set the order by changing their positions in this array.- Save the file, then clear your cache to ensure the changes take effect:
php bin/magento cache:cleanConclusion and Further Reading
By following these steps, you should now be able to control the order of totals in your Magento transactional email templates. Remember that any changes made to core files will need to be reapplied if the core file is updated in a future version.
For more advanced customizations and best practices in Magento development, I recommend checking out our blog at LaravelCompany Blog. We regularly publish articles covering various aspects of Magento development to help you stay informed and up-to-date with the latest trends and techniques.
Happy coding! Let me know if you have any questions or need further assistance.
- Save the file, then clear your cache to ensure the changes take effect: