Skip to content
Webstair Technology home Start a project
Magento Development

Add extra field to magento checkout

By Webstair Technology · Published 18 May 2025 · Updated

Master Magento Checkout Customization with Code Examples

This guide adds a text field to the Magento 2 checkout, validates it and saves its value with the order. It is built as one small module, Webstair_CustomCheckout, so no core files are changed. Adobe’s own checkout customization tutorials are a useful companion to this guide.

Before you use it: these examples are written for Magento 2.4.4 or later on PHP 8.1 or later. Key class names and checkout hooks were checked against Magento 2.4 source code, but we have not run this module as a complete installation. Test it on a staging store that matches your version, theme, checkout and extensions.

1. Create the module

Create these two files so that Magento can find the module.

File: app/code/Webstair/CustomCheckout/registration.php

<?php
use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Webstair_CustomCheckout', __DIR__);

File: app/code/Webstair/CustomCheckout/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Webstair_CustomCheckout">
        <sequence>
            <module name="Magento_Checkout"/>
            <module name="Magento_Quote"/>
            <module name="Magento_Sales"/>
        </sequence>
    </module>
</config>

2. Add the field to checkout

A plugin on the checkout LayoutProcessor adds the field to the shipping address form. The plugin only runs after it is declared in di.xml.

File: app/code/Webstair/CustomCheckout/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Block\Checkout\LayoutProcessor">
        <plugin name="webstair_checkout_custom_field" type="Webstair\CustomCheckout\Plugin\Checkout\LayoutProcessorPlugin"/>
    </type>
</config>

File: app/code/Webstair/CustomCheckout/Plugin/Checkout/LayoutProcessorPlugin.php

<?php
declare(strict_types=1);

namespace Webstair\CustomCheckout\Plugin\Checkout;

use Magento\Checkout\Block\Checkout\LayoutProcessor;

class LayoutProcessorPlugin
{
    public function afterProcess(LayoutProcessor $subject, array $jsLayout): array
    {
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']
            ['children']['shippingAddress']['children']['shipping-address-fieldset']
            ['children']['custom_field'] = [
                'component' => 'Magento_Ui/js/form/element/abstract',
                'config' => [
                    'customScope' => 'shippingAddress.custom_attributes',
                    'template' => 'ui/form/field',
                    'elementTmpl' => 'ui/form/element/input',
                ],
                'dataScope' => 'shippingAddress.custom_attributes.custom_field',
                'provider' => 'checkoutProvider',
                'label' => __('Custom Field'),
                'visible' => true,
                'sortOrder' => 250,
                'validation' => [
                    'required-entry' => true,
                    'min_text_length' => 3,
                    'max_text_length' => 255,
                ],
            ];

        return $jsLayout;
    }
}
Need help with Magento or Adobe Commerce?Get a free quote →

3. How validation works

The validation list above makes the field required, with 3 to 255 characters, when the new shipping address form is shown. Browser checks can be bypassed, so step 5 also checks the length on the server whenever a value is sent.

4. Create the database columns

The value needs a column on the cart (called a quote in Magento) and another on the order.

File: app/code/Webstair/CustomCheckout/etc/db_schema.xml

<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="quote" resource="checkout">
        <column xsi:type="varchar" name="custom_field" length="255" nullable="true" comment="Checkout custom field"/>
    </table>
    <table name="sales_order" resource="sales">
        <column xsi:type="varchar" name="custom_field" length="255" nullable="true" comment="Checkout custom field"/>
    </table>
</schema>

5. Send the value and save it to the cart

Checkout does not send a new field to your own database column by itself. First, declare an extension attribute on the shipping information request.

File: app/code/Webstair/CustomCheckout/etc/extension_attributes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Checkout\Api\Data\ShippingInformationInterface">
        <attribute code="custom_field" type="string"/>
    </extension_attributes>
</config>

Next, a JavaScript mixin adds the entered value to that request.

File: app/code/Webstair/CustomCheckout/view/frontend/requirejs-config.js

var config = {
    config: {
        mixins: {
            'Magento_Checkout/js/model/shipping-save-processor/payload-extender': {
                'Webstair_CustomCheckout/js/payload-extender-mixin': true
            }
        }
    }
};

File: app/code/Webstair/CustomCheckout/view/frontend/web/js/payload-extender-mixin.js

define(['mage/utils/wrapper', 'uiRegistry'], function (wrapper, registry) {
    'use strict';

    return function (payloadExtender) {
        return wrapper.wrap(payloadExtender, function (originalExtender, payload) {
            var result = originalExtender(payload),
                provider = registry.get('checkoutProvider'),
                value = provider && provider.get('shippingAddress.custom_attributes.custom_field');

            result.addressInformation.extension_attributes.custom_field = value || '';

            return result;
        });
    };
});

On the server, a second plugin checks the value and sets it on the cart just before Magento saves the shipping information. Guest checkout uses the same service.

File: app/code/Webstair/CustomCheckout/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Api\ShippingInformationManagementInterface">
        <plugin name="webstair_save_custom_field" type="Webstair\CustomCheckout\Plugin\Checkout\SaveCustomField"/>
    </type>
</config>

File: app/code/Webstair/CustomCheckout/Plugin/Checkout/SaveCustomField.php

<?php
declare(strict_types=1);

namespace Webstair\CustomCheckout\Plugin\Checkout;

use Magento\Checkout\Api\Data\ShippingInformationInterface;
use Magento\Checkout\Api\ShippingInformationManagementInterface;
use Magento\Framework\Exception\InputException;
use Magento\Quote\Api\CartRepositoryInterface;

class SaveCustomField
{
    public function __construct(private CartRepositoryInterface $cartRepository)
    {
    }

    public function beforeSaveAddressInformation(
        ShippingInformationManagementInterface $subject,
        $cartId,
        ShippingInformationInterface $addressInformation
    ): void {
        $attributes = $addressInformation->getExtensionAttributes();
        if ($attributes === null || $attributes->getCustomField() === null) {
            return;
        }

        $value = trim(strip_tags($attributes->getCustomField()));
        $length = mb_strlen($value);
        if ($length > 0 && ($length < 3 || $length > 255)) {
            throw new InputException(__('Custom Field must be 3 to 255 characters.'));
        }

        // Magento reuses this cart object in the same request and saves it after this plugin.
        $quote = $this->cartRepository->getActive($cartId);
        $quote->setData('custom_field', $value === '' ? null : $value);
    }
}

6. Copy the value to the order

When the cart becomes an order, an observer copies the value across. There is no need to read the browser request or to save the order inside the observer.

File: app/code/Webstair/CustomCheckout/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_model_service_quote_submit_before">
        <observer name="webstair_copy_custom_field" instance="Webstair\CustomCheckout\Observer\CopyCustomFieldToOrder"/>
    </event>
</config>

File: app/code/Webstair/CustomCheckout/Observer/CopyCustomFieldToOrder.php

<?php
declare(strict_types=1);

namespace Webstair\CustomCheckout\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class CopyCustomFieldToOrder implements ObserverInterface
{
    public function execute(Observer $observer): void
    {
        $quote = $observer->getEvent()->getData('quote');
        $order = $observer->getEvent()->getData('order');

        $order->setData('custom_field', $quote->getData('custom_field'));
    }
}

7. Optional: add a block and react to typing

A layout file can place a short help message above checkout. In Magento's checkout layout, checkout.root is a block, so this example adds the new block to the content container.

File: app/code/Webstair/CustomCheckout/view/frontend/layout/checkout_index_index.xml

<?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>
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" name="webstair.checkout.help"
                   template="Webstair_CustomCheckout::help.phtml" before="-"/>
        </referenceContainer>
    </body>
</page>

File: app/code/Webstair/CustomCheckout/view/frontend/templates/help.phtml

<?php
/** @var \Magento\Framework\Escaper $escaper */
?>
<p id="webstair-field-help" aria-live="polite">
    <?= $escaper->escapeHtml(__('Custom Field needs at least 3 characters.')) ?>
</p>
<script type="text/x-magento-init">
    {"*": {"Webstair_CustomCheckout/js/field-watcher": {}}}
</script>

Magento builds checkout fields after the page loads, so this script listens to the checkout data provider instead of looking for the input as soon as the page opens.

File: app/code/Webstair/CustomCheckout/view/frontend/web/js/field-watcher.js

define(['uiRegistry', 'mage/translate'], function (registry, $t) {
    'use strict';

    return function () {
        registry.async('checkoutProvider')(function (checkoutProvider) {
            checkoutProvider.on('shippingAddress.custom_attributes.custom_field', function (value) {
                var help = document.getElementById('webstair-field-help');

                if (help) {
                    help.textContent = $t('Characters entered:') + ' ' + (value || '').length;
                }
            });
        });
    };
});

8. Install and test

On your staging store, run these commands from the Magento root folder.

bin/magento module:enable Webstair_CustomCheckout
bin/magento setup:db-declaration:generate-whitelist --module-name=Webstair_CustomCheckout
bin/magento setup:upgrade

# Needed in production mode
bin/magento setup:di:compile
bin/magento setup:static-content:deploy

bin/magento cache:flush

Place a test order as a guest. Then check that the custom_field column in the sales_order table holds what you typed. Also try an empty value and a value that is too short.

What this example does not cover

  • The field is part of the new shipping address form. Signed-in customers who choose a saved address will not see it there.
  • Orders without a shipping step, express payment buttons, multishipping, custom checkouts and orders created in the admin or through other integrations need their own handling.
  • The server does not force a value on every order. Add that rule only after covering every way your store creates orders.
  • It does not show the value in the admin, emails or invoices.

Conclusion

A checkout field is more than an input. It also needs validation, a way to send the value, a place to store it and a step that copies it to the order. Test each step with your own checkout before using it on a live store.

Need help with checkout customization? Get in touch with Webstair Technology.

Webstair Technology
Web, eCommerce and mobile development in Ahmedabad, India.

Need help with Magento or Adobe Commerce?

Send a few lines about your project. You can ask for a free quote right here, without leaving this page.

  • For store owners and agencies
  • Name, email, phone and project details are required

Or call +91 82005 38671, Monday to Friday, 9 to 5 IST.

Call usGet a free quote