Saving the value of custom field into sales_flat_order_address table

Saving the value of custom field into sales_flat_order_address table

September 21, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Saving the value of custom field into sales_flat_order_address table

Saving Custom Field Values to sales_flat_order_address Table

When you create a custom field for customer addresses and want to save its value into the sales_flat_order_address table during checkout, there are a few key steps to follow in Magento. Let’s walk through this process step-by-step.

Step 1: Prepare Your Custom Field

Firstly, ensure your custom field is set up correctly on the backend. In your case, you have already created a new_address_code field for customer addresses using an observer when a new address is added or during checkout.

<customer_address_save_after>
    <observers>
        <myautoincrement>
            <class>samp/observer</class>
            <method>saveadrsmethod</method>
        </myautoincrement>
    </observers>
</customer_address_save_after>

Step 2: Generate the Custom Field Value

In your observer class, you are successfully generating an auto-generated value for this field. However, the issue arises when Magento switches from the quote to order context during checkout.

if ($customerAddress->getId()) {
    $customerAddress->setData('new_address_code', 'aftertesting-' . $customerAddress->getData('firstname') . '=myid' . $id);
    $ObcustomerAddress = $observer->getCustomerAddress();
    $ObcustomerAddress->getResource()->saveAttribute($ObcustomerAddress, 'new_address_code');
}

Step 3: Save the Field Value to sales_flat_order_address

To save this value into the sales_flat_order_address table during order placement, you need to create another observer for the sales_order_place_before event.

<sales_order_place_before>
    <observers>
        <myautoincrement>
            <class>samp/observer</class>
            <method>beforeorder</method>
        </myautoincrement>
    </observers>
</sales_order_place_before>

Step 4: Integrate the Custom Field Value

In your observer method for order placement, you need to retrieve the customer address data and set it in the appropriate fields of the order object.

public function beforeorder($observer)
{
    $order = $observer->getEvent()->getOrder();
    $customerAddress = $order->getBillingAddress();

    if ($customerAddress && $customerAddress->getId()) {
        $address = $order->getBillingAddress();
        $address->setData('new_address_code', $customerAddress->getData('new_address_code'));
        $order->setBillingAddress($address);
        $order->setShippingAddress($address);
    }
}

Step 5: Verify the Data in sales_flat_order_address

After implementing this logic, Magento should automatically save your custom field value to the sales_flat_order_address table when an order is placed.

Remember, it’s crucial to ensure that the event observer is properly configured and enabled in your Magento system configuration. If you’re having trouble with the data not being saved, double-check that the event is firing correctly and that the customer address object contains the expected data before setting the new field value.

For a comprehensive guide on working with events and observers in Magento, I recommend checking out this detailed article from our team at Laravel Company. It provides a step-by-step walkthrough of the entire process.

Checkout Database Extensions Admin

Our network