Solving Magento Order Grand Total Issues in Onepage.php
Hello developers! Today, I’m going to help you resolve an issue many of us have encountered while working on Magento’s onepage checkout system. Let me walk you through a common problem involving the order grand total and provide a step-by-step solution.
The Problem: Incorrect Order Grand Total in Transactional Emails
You might be facing this issue when trying to send custom transactional emails from the saveOrder() method of app/code/core/Mage/Checkout/Type/Onepage.php.
The problem lies in the fact that the order hasn’t been fully saved yet, and therefore Magento’s internal calculations aren’t complete. This results in a wrong grand total being displayed in your custom email template.
Understanding the Core Issue
When manipulating the saveOrder() method to send different emails based on payment methods, you’re essentially intercepting the standard flow before the order is completely finalized. The issue arises because the grand total calculation happens after the save() method returns, and it’s not immediately accessible in your custom code.
The Solution: Delayed Total Calculation
To resolve this, we need to find a way to access the correct grand total even though the order isn’t fully saved yet. Here’s how you can achieve that:
- Calculate the Total Manually Before Sending the Email
You can use Magento’s internal API to calculate the grand total without saving the order completely. This approach involves accessing the convertToArray() method of the Mage_Sales_Model_Quote_Item class.
- Update Your Code in Onepage.php
Replace your current email sending code with the following:
if($order->getPayment()->getMethodInstance()->getCode()!='X') {
$order->sendNewOrderEmail();
} else {
// Calculate total manually
$items = $order->getItemsCollection();
foreach ($items as $item) {
$rowTotal = $item->getRowTotal();
$total += $rowTotal;
}
$name = $order->getBillingAddress()->getName();
// Rest of your email code...
$mailer->setTemplateParams(array(
'order' => $order,
'grand_total' => $total
)
);
// ... and continue with the rest of your email sending process.
}
- Update Your Transactional Email Template
In your transactional email template, update the {{var order.getGrandTotal()}} reference to use the new variable you passed:
<p>Your order total is: {{var grand_total}}</p>
The Reason for This Approach
Delaying the calculation until right before sending the email ensures that all discounts and adjustments are applied accurately. By calculating the total manually, we can overcome the timing issue caused by intercepting the normal save process.
Conclusion
In this post, I’ve explained why you might be facing incorrect grand totals in your custom transactional emails when working with Magento’s onepage checkout system. The solution involves calculating the total manually before sending the email, ensuring all discounts and adjustments are applied correctly.
Remember to always test these changes thoroughly and consider using a staging environment before deploying them to production.
For more advanced techniques and best practices related to customizing Magento checkout processes, feel free to explore our Magento Customization Guide at Laravel Company.
Happy coding!