How can I save all order place data in custom table along with Magento default tables?

How can I save all order place data in custom table along with Magento default tables?

September 21, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How can I save all order place data in custom table along with Magento default tables?

Saving Order Data in Custom Tables alongside Magento’s Default Tables

Introduction

Magento is an enterprise-level e-commerce platform that handles online transactions efficiently. When a customer places an order, the data gets stored in default tables like sales_flat_order and sales_flat_order_item. While these tables provide essential functionality, there may come a time when you need to store specific data in a custom table for additional business logic or reporting purposes.

Understanding Your Scenario

You have already created a custom table in your Magento database with selected columns from the default tables. Now, whenever an order is placed, you want this data to be automatically copied into your custom table alongside the default Magento tables.

You’re on the right track by creating a PHP file that includes an SQL query fetching needed data and inserting it into your custom table. However, you need guidance on how to trigger this process when an order is being saved in Magento.

Approaching the Solution

The saveOrder function in the Checkout model (Checkout/Model/Type/Onepage.php) is indeed a good starting point. This function is called every time an order is placed or updated in the frontend one-step checkout process. However, directly modifying this core file is not recommended due to upgrade compatibility issues.

A better approach is to use Magento’s observer pattern and events system to intercept the order saving process without directly touching the core code.

Implementing the Solution

  1. Create a Custom Module: First, create a new custom module for your functionality.
  2. Define an Event Observer: In your module, define an event observer that listens for the sales_order_place_after event. This event is dispatched after an order has been placed.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/config.xsd">
    <event name="sales_order_place_after">
        <observer name="save_to_custom_table_observer" instance="Vendor\YourModuleName\Observer\SaveToCustomTableAfterOrderPlace"/>
    </event>
</config>
  1. Create the Observer: Create a PHP class SaveToCustomTableAfterOrderPlace in your module’s Observer directory.

The Observer Class

In this observer class, you will implement the logic to fetch data from Magento’s default tables and insert it into your custom table using your existing SQL query.

<?php

namespace Vendor\YourModuleName\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\Http as HttpRequest;

class SaveToCustomTableAfterOrderPlace implements ObserverInterface
{
    protected $connection;

    public function __construct(
        \Magento\Framework\App\ResourceConnection $resourceConnection
    ) {
        $this->connection = $resourceConnection->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // Fetch order data from the observer event's order object
        $order = $observer->getEvent()->getOrder();

        // Fetch required columns from Magento default tables using your existing SQL query

        // Assuming your custom table is named 'your_module_name_custom_table'
        // Insert fetched data into your custom table using prepared statements
    }
}
  1. Flush Cache: After creating the observer, remember to flush your cache (System > Cache Management) for the changes to take effect.

Conclusion

By following this approach, you stay away from directly modifying Magento’s core files, maintaining a clean and upgrade-friendly codebase. The event observer will automatically be triggered whenever an order is placed, ensuring that data is copied into your custom table alongside Magento’s default tables.

Remember to replace placeholders like Vendor\YourModuleName with the actual vendor and module name you’re using in your project.

Also, consider visiting LaravelCompany for more detailed guides on creating custom modules in Magento 2 to enhance your development skills.

Checkout Database Extensions Deployment

Our network