Magento Get Last Order Id on Payment Info Block Email

Magento Get Last Order Id on Payment Info Block Email

September 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Get Last Order Id on Payment Info Block Email

Retrieving Magento Order ID on Payment Info Email Block: A Comprehensive Solution

If you’re facing issues with retrieving the correct order ID for the payment info block on Magento emails, especially when using third-party extensions like Fontis Australia, this guide will provide a thorough solution to resolve your problem.

Understanding the Current Challenge

The Fontis Australia extension generates a reference number for BPAY payments that requires the actual order ID as input. However, in the current setup, the payment info block on the order acknowledgement email always shows ‘000000’ because it cannot retrieve the correct order ID from the checkout session.

The Root Cause

The issue lies in how Magento handles the order processing and email sending process. When an order is placed, Magento first saves it to the database with a temporary order ID (increment_id) that is not unique across all orders. It then sends the order confirmation email before finalizing the order and generating the unique order ID.

A Developer’s Perspective

To fix this issue, we need to modify the Fontis Australia extension to retrieve the correct order ID from a different source within Magento during the email sending process. The getLastRealOrderId() method is not suitable for this purpose because it only retrieves the last “real” (finalized) order ID, which may be an older order than the one being processed.

Retrieving the Correct Order ID

Here’s a step-by-step solution to retrieve the correct order ID:

  1. Identify the Email Sending Event: In Magento, emails are sent using event observers. For the Fontis Australia extension, you’ll need to find the observer that handles the ‘sales_order_save_after’ event.

  2. Access the Current Order Object: Within the observer, retrieve the current order object using $observer->getEvent()->getOrder() or $event->getOrder(). This will give you access to the most recent order being processed.

$order = $observer->getEvent()->getOrder();
  1. Retrieve the Order ID: Now that we have the order object, we can retrieve the unique order ID using the getIncrementId() or getRealOrderId() method. Since you’re dealing with a temporary order before finalization, use getIncrementId().
$orderId = $order->getIncrementId();
  1. Update the Payment Info Block: With the correct order ID in hand, update the payment info block in your extension to generate the reference number using this ID.

  2. Code Integration: If you’re not familiar with Magento observer events and how to modify third-party extensions, I recommend checking out these resources on our LaravelCompany blog:

Best Practices and Additional Tips

  1. Avoid Hardcoding Values: Always retrieve the necessary data dynamically from Magento objects to ensure consistency across different environments.

  2. Test Thoroughly: After implementing this solution, make sure to test it extensively with various payment methods and order scenarios to ensure it works correctly in all cases.

Conclusion

Retrieving the correct order ID for the payment info block on Magento emails requires a deep understanding of how Magento processes orders and sends emails. By following the steps outlined in this guide, you should be able to resolve the issue with Fontis Australia extension or any other similar scenarios where you need to access specific order data during the email sending process.

Don’t hesitate to explore our LaravelCompany blog for more in-depth tutorials on Magento development and best practices. Happy coding!

Checkout Database Extensions Email

Our network