Adding Custom field in orders Magento2

Adding Custom field in orders Magento2

July 6, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Adding Custom field in orders Magento2

Adding Custom Fields to Magento2 Orders: A Step-by-Step Guide

As a new developer in the world of Magento2, you’re likely looking for ways to extend its core functionality to meet your specific business needs. One common requirement is adding custom fields to orders that update post-checkout and appear in the admin interface.

In this guide, we’ll walk through the process of creating a custom order field using Magento2’s built-in capabilities. By following these steps, you’ll be able to add a new field that appears on the checkout page, gets saved with the order, and displays in the admin grid view for easy management.

Step 1: Set Up Your Development Environment

Before we begin, ensure you have your local Magento2 development environment set up. This typically involves having Composer installed, creating a new project using composer create-project, and running it locally with a web server like Nginx or Apache.

Read more about setting up a local Magento2 environment on our blog

Step 2: Create the Custom Field

Let’s start by creating the custom field that will be added to the checkout page. To do this, we’ll use an XML layout update to insert a new input element into the checkout form.

Create a new file in your theme directory at app/design/frontend/Vendor/Theme/Magento_Checkout/layout/checkout_form.xml.

Add the following content:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="checkout.root">
            <block class="Magento\Framework\View\Element\Template" template="Vendor_Theme::field.phtml" before="-"/>
        </referenceBlock>
    </body>
</page>

This layout update adds a new block named “field” before the existing checkout form. The actual field content will be defined in the field.phtml file.

Step 3: Create the Field Template

Create the field.phtml file in your theme directory at app/design/frontend/Vendor/Theme/templates/field.phtml.

Add the following HTML:

<div class="field">
    <label for="your-custom-field"><?= $block->escapeHtml($_fieldName) ?>:</label>
    <input type="text" name="<?= $block->escapeHtml($_fieldName) ?>" id="<?= $block->escapeHtml($_fieldName) ?>" value="<?= $block->escapeHtml($_value) ?>">
</div>

This template defines a simple text input field with the label and value provided by variables passed from the controller.

Step 4: Update the Order Data

To save the custom field data with the order, we’ll use an observer to listen for the sales_order_place_after event. This event is triggered after an order has been successfully placed and allows us to update the order data accordingly.

Create a new file in your module directory at app/code/Vendor/Module/etc/events.xml.

Add the following content:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/events.xsd">
    <event name="sales_order_place_after">
        <observer name="vendor_module_update_order_data" instance="Vendor\Module\Model\Observer\UpdateOrderData"/>
    </event>
</config>

This configuration file defines a new event observer named “vendor_module_update_order_data”.

Step 5: Create the Observer

Create the observer class in your module directory at app/code/Vendor/Module/Model/Observer/UpdateOrderData.php.

Add the following content:

namespace Vendor\Module\Model\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Sales\Model\OrderFactory;

class UpdateOrderData implements ObserverInterface
{
    private $orderFactory;

    public function __construct(OrderFactory $orderFactory)
    {
        $this->orderFactory = $orderFactory;
    }

    public function execute(Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();
        $yourCustomFieldValue = $_POST['your-custom-field']; // Replace with your field name

        if ($order) {
            $order->setData('your-customer-attribute', $yourCustomFieldValue);
            $order->save();
        }
    }
}

This observer retrieves the placed order and updates a custom customer attribute (replace your-customer-attribute with your actual attribute code). The new value comes from the posted form data.

Step 6: Add the Field to Admin Grid

To display the custom field in the admin grid view, we’ll need to modify the order grid item layout.

Create or update a file in your theme directory at app/design/frontend/Vendor/Theme/Magento_Sales/layout/sales_order_grid.xml.

Add the following content:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="sales_order_grid_totals">
            <block class="Magento\Framework\View\Element\Template" template="Vendor_Theme::column.phtml"/>
        </referenceBlock>
    </body>
</page>

This layout update adds a new column to the order grid using the column.phtml template.

Create the column.phtml file in your theme directory at app/design/frontend/Vendor/Theme/templates/column.phtml.

Add the following content:

<?= $block->escapeHtml($_fieldName) ?>: <?= $block->escapeHtml($_value) ?>

This template displays the custom field value from the order object.

Conclusion

Adding a custom field to Magento2 orders involves several steps, but by following this guide, you should now be able to create a new field that appears on the checkout page, gets saved with the order post-checkout, and displays in the admin grid view for easy management.

Remember to replace the placeholders (e.g., Vendor, Module, your-custom-field, your-customer-attribute) with your actual module name, attribute code, and field names.

This approach demonstrates how Magento2’s built-in capabilities can be extended to meet specific business requirements without requiring complex customizations or third-party extensions. By understanding the core architecture and event-driven model, developers can create powerful solutions that integrate seamlessly with the platform.

For more advanced customizations and integrations in Magento2, consider exploring our comprehensive Magento development services at Laravel Company. Our team of experienced Magento specialists is ready to help you build scalable, high-performing e-commerce solutions tailored to your unique needs.

Happy coding!

Magento 2 Checkout Themes Admin

Our network