How to add a custom field in checkout form in magento 2 with database entry

How to add a custom field in checkout form in magento 2 with database entry

September 23, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to add a custom field in checkout form in magento 2 with database entry

Adding a custom field like a “Job Number” after the email field in the checkout form of Magento 2 involves modifying core files, which is generally not recommended due to upgrade compatibility issues. However, if you understand the risks involved, you can achieve this by following these steps.

Overview

Magento 2 uses XML layout updates and JavaScript to manage the checkout forms. To add a new field, we need to update the checkout form layout file (checkout_index_index.xml) and create a new UI component file for the Job Number field.

Step-by-Step Guide

1. Create a New Module

First, create a new module in your app directory if you haven’t already:

app/code/Vendor/JobNumber

2. Add the Checkout Form Field

Open the checkout_index_index.xml file located at Vendor_JobNumber/view/frontend/layout/checkout_index_index.xml.

Add the following XML code after the <referenceBlock name="checkoutsteps"> block, inside the <body> tag:

<block class="Magento\Framework\View\Element\Template" template="Vendor_JobNumber::field.phtml">
    <arguments>
        <argument name="job_number_label" xsi:type="string" translate="true">Job Number</argument>
        <argument name="job_number_name" xsi:type="string">job_number</argument>
    </arguments>
</block>

This code creates a new block with a template field.phtml and passes the label and input name for the Job Number field.

3. Create the Template File

Create a folder Vendor_JobNumber/view/frontend/templates/, then inside this folder, create another folder called checkout. In the checkout folder, create a file named field.phtml.

Add the following HTML code to field.phtml:

<div class="field field-job-number required">
    <label for="job_number" data-bind="attr: {'for': jobNumberLabel}">*</label>
    <input type="text"
           name="job_number"
           id="job_number"
           data-bind="attr: {id: jobNumberId, class: 'input-text', name: jobNumberName}, value: jobNumberValue" />
</div>

This code creates an input field for the Job Number with necessary bindings.

4. Update checkout_form_index.xml

Open Vendor_JobNumber/view/frontend/layout/checkout_form_index.xml and find the <referenceBlock name="checkoutForm"> block, inside the <body> tag.

Add the following XML code after the paymentMethod.allow_fields:

<field name="job_number">
    <argument name="data" xsi:type="array">
        <item name="name" xsi:type="string">job_number</item>
        <item name="formElement" xsi:type="string">input</item>
        <item name="label" xsi:type="string" translate="true">Job Number</item>
    </argument>
</field>

This adds the Job Number field to the checkout form.

5. Save and Enable the Module

Enable your module using the command:

bin/magento module:enable Vendor_JobNumber
bin/magento setup:upgrade

Conclusion

Adding a custom field like a “Job Number” after the email field in Magento 2 requires modifying core files and understanding the layout and JavaScript logic. While this method works, it introduces potential upgrade compatibility issues.

For a more sustainable solution, consider using third-party extensions or customizing checkout fields through the Magento Admin interface where available. If you need to create complex custom fields with database integration, consider using a third-party extension like Amasty Checkout Fields for better flexibility and upgrade safety.

Always remember that modifying core files should be done cautiously and only when necessary. If you’re not comfortable with these changes, it’s recommended to seek help from a Magento developer or consult the official documentation.

This guide provides a basic overview of adding a custom field in checkout form in Magento 2. For more complex requirements, consider the many resources available on the Magento community forums and Laravel Company for further learning.

Magento 2 Checkout Database Extensions

Our network