event sales_order_invoice_save_after always fired twice

event sales_order_invoice_save_after always fired twice

August 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
event sales_order_invoice_save_after always fired twice

Optimizing Magento: Efficient Data Export to SQL Server

Magento is a robust e-commerce platform that handles a vast amount of transactional data. For many businesses, it’s crucial to export this data to their existing systems like SQL Server for further analysis or integration with other enterprise applications. However, managing and optimizing these exports can be challenging.

The Challenge: Efficient Data Export in Magento

In a recent project involving a client who uses an ERP system (QAD) that requires data from Magento, we faced an issue with the export process. Our goal was to create an efficient way to write invoices and order details from Magento to SQL Server using the QAD module.

The primary problem was that while using the Observer pattern for triggering the export after a new invoice is created (sales_order_save_after event), the data insertion process would happen twice, despite having proper validation checks in place. This led to duplicate entries and inconsistencies in the SQL Server database.

The Solution: Fine-tuning the Data Export Process

Upon deeper investigation, we discovered that the issue lay within the Observer pattern itself and how Magento manages its event triggers and registries.

The sales_order_save_after event is triggered twice due to the way Magento handles its registry. When an invoice is saved, Magento first triggers this event and then calls a saveAfter() function on the invoice model, which in turn triggers another instance of the same event. This double triggering causes our data insertion process to run twice.

The Technical Fix: Managing Magento’s Event Registry

To resolve this issue, we introduced a custom registry variable sql_salvado and managed it within our Observer method:

if(Mage::registry('sql_salvado') == 1){
    Mage::unregister('sql_salvado');
    return NULL;
}

This check ensures that if the registry variable is already set to 1, indicating a previous successful insertion, then the process terminates immediately. Conversely, setting the registry to 1 after a successful data insertion prevents further trigger instances from running the same code block.

The Result: Efficient and Accurate Data Export

By implementing this simple yet effective solution, we were able to ensure that our data export process runs only once per invoice creation event. This approach has significantly improved the performance of our system by eliminating redundant database operations and maintaining data consistency across Magento and SQL Server.

The key takeaways from this experience are:

  1. Be aware of Magento’s event triggering mechanisms, especially when dealing with Observer patterns.
  2. Use custom registry variables to manage the state of your data export process.
  3. Implement proper validation checks before performing data insertions to prevent redundant operations.
  4. Regularly monitor and optimize your system to ensure it operates at peak efficiency.

For more advanced insights into Magento development and optimization, feel free to visit our company’s website: https://laravelcompany.com/

By following these best practices and leveraging the power of Magento and SQL Server integration, you can achieve a streamlined e-commerce experience that meets your business needs.

Database Performance Extensions Orders

Our network