Customizing Magento Order Totals for Email Templates
By: [Your Name], Senior Developer @ LaravelCompany
Magento’s out-of-the-box order emails include comprehensive details about the customer’s purchase. However, there are instances where you might want to customize specific sections like the order totals displayed in these email templates.
Locating Relevant Templates and Blocks
Let’s start by identifying the key templates involved:
- Invoice Email Template: This is located at
app/design/frontend/SUPPLIER_THEME/Magento_Email/invoice_new.html. - Order Confirmation Email Template: You can find this at
app/design/frontend/SUPPLIER_THEME/Magento_Email/order_new.html.
These templates are responsible for the overall layout of your order and invoice emails.
Next, we need to pinpoint the block handling the order totals:
- Order Totals Block: In both email templates mentioned above, you’ll find a block named
order_totalsaround line 104 (the exact location may vary depending on your theme). This is where Magento assembles and displays all the order totals.
Removing/Modifying Order Totals
To remove or modify specific order totals like shipping and subtotal, follow these steps:
- Open your desired email template file (invoice_new.html or order_new.html).
- Locate the
order_totalsblock. - Inside this block, you’ll find a foreach loop that iterates through each order total line.
Here’s an example of how to remove a specific order total like ‘Subtotal’:
<tbody>
<tr>
<td class="meta-title">
<?php echo $this->escapeHtml(__('Subtotal')) ?>
</td>
<!-- Remove the following lines -->
<td class="meta-value">
<?php if ($this->getOrder()->getTaxAmount()): ?>
<?php echo $this->helper('tax')->displayPrice($this->getOrder()->getTaxAmount(), true, false) ?>
<?php else: ?>
-<?php echo $this->helper('core')->currency(-$this->getOrder()->getSubtotalInclTax()) ?>
<?php endif; ?>
</td>
</tr>
</tbody>
Simply comment out or delete the entire <tr> element responsible for the total you don’t want to display.
Implementing Custom Logic
If you need to implement some custom logic (e.g., adjust the shipping amount), you can use Magento’s core functionality:
- Create your custom order total model extending
Magento\Sales\Model\Order\Total\AbstractTotal. - Add it to your extension config file.
- Update the email templates to include your new custom order total.
Remember, whenever you modify core files like these email templates, ensure you do so properly with a local theme or an extension to avoid losing changes during updates.
Conclusion
Customizing Magento’s order and invoice emails is quite straightforward once you understand the core template structure. By locating and modifying the order_totals block in the relevant email templates, you can easily remove or adjust any order totals as needed for a better customer experience.
For more advanced customizations and best practices in Magento development, feel free to explore our resources at LaravelCompany.
Happy coding!