add custom field to onepage checkout in magento

add custom field to onepage checkout in magento

July 27, 2026 · Illustrated author avatarBy Stefan Bogdanescu
add custom field to onepage checkout in magento

Adding Custom Fields to the Onepage Checkout in Magento

As an experienced developer working on your e-commerce project using the Magento platform, you may find yourself wanting to add custom fields to the checkout process to collect additional information from your customers.

In version 1.4.1 of Magento, the order tables have been flattened for performance improvements. This means that adding a new column directly is not as straightforward as it used to be in previous versions. However, with careful understanding and application of Magento’s architecture, you can achieve this feat by following these steps:

Step 1: Adding Custom Columns to Quote and Order Tables

To add columns to the quote and order tables, you will need to create a setup script within your module’s Mymod/sql/mymod_setup/mysql4-install-0.1.0.php folder.

Here is an example of how you can structure this file:

<?php
$installer = $this;
$installer->startSetup();
$table = $installer->getConnection()
    ->newTable($installer->getTable('sales/quote'))
    ->addColumn('my_custom_field', Varien_Db_Ddl_Table::TYPE_VARCHAR, 255, array(
        'nullable' => false,
    ))
    ->setComment('My Custom Field');
$installer->getConnection()->createTable($table);
$installer->endSetup();

In this example, a new column named my_custom_field of type VARCHAR with a maximum length of 255 characters is being added to the sales/quote table.

Step 2: Saving Custom Fields to the Database

Once you have added the necessary columns to the database using the setup script, the next step is to save the values entered by the customer into these custom fields during the checkout process.

To achieve this, Magento provides event observers that allow you to intercept and modify data at specific points in the checkout workflow. One such event is sales_order_place_after.

You can create an observer for this event within your module’s config.xml file:

<config>
    <global>
        <models>
            <mymodule>
                <class>Mymodule_Model</class>
            </mymodule>
        </models>
        <events>
            <sales_order_place_after>
                <observers>
                    <mymodule_save_custom_field>
                        <type>singleton</type>
                        <class>mymodule/observer</class>
                        <method>saveCustomFieldToOrder</method>
                    </mymodule_save_custom_field>
                </observers>
            </sales_order_place_after>
        </events>
    </global>
</config>

In this configuration, an observer named mymodule_save_custom_field is registered for the sales_order_place_after event. This observer will call the saveCustomFieldToOrder method in the Mymodule_Model_Observer class.

The implementation of the saveCustomFieldToOrder method would look something like this:

public function saveCustomFieldToOrder($observer)
{
    $order = $observer->getEvent()->getOrder();
    $quoteItemId = $order->getQuoteId();

    // Retrieve the quote item and get the custom field value entered by the customer
    $quote = Mage::getModel('sales/quote')->load($quoteItemId);
    $customFieldValue = $quote->getMyCustomField();

    // Save the custom field value to the order object
    $order->setMyCustomField($customFieldValue);

    return $this;
}

In this example, the observer retrieves the quote item associated with the placed order and extracts the value of the custom field entered by the customer. It then sets this value in the my_custom_field property of the order object.

Step 3: Displaying Custom Fields on Checkout Page

To display your custom fields on the checkout page, you need to modify the onepage checkout template file located at app/design/frontend/default/theme/template/checkout/onepage/review.phtml.

You can add HTML input elements for your custom fields within the appropriate section of the template:

<div class="field">
    <label for="my_custom_field"><?php echo Mage::helper('mymodule')->__('My Custom Field') ?></label>
    <input type="text" name="my_custom_field" id="my_custom_field" value="<?php echo $this->getQuote()->getMyCustomField() ?>" class="input-text">
</div>

Replace theme with the actual theme folder where your template is located.

Conclusion

Adding custom fields to the onepage checkout in Magento requires a combination of database schema modifications, observer setup, and template file updates. By carefully following these steps and understanding Magento’s event-driven architecture, you can successfully add custom columns to the quote and order tables and save customer-entered values to the database during the checkout process.

Remember to thoroughly test your implementation on different scenarios to ensure data consistency and proper functioning of your custom fields throughout the checkout workflow.

If you require further assistance or need a professional Magento developer to guide you through this process, feel free to reach out to us at https://laravelcompany.com. We specialize in providing expert development services for e-commerce projects using various platforms, including Magento.

Checkout Database Performance Extensions

Our network