How to Add Custom Field in Address Form in Magento 2
Magento 2 is a widely used platform by various types of businesses. Hence, the checkout page, shipping or billing page may not be the same as the default Magento 2 for every store. According to the business requirement, the address forms may vary.
For example, implementing the Indian GST tax in Magento 2 store requires to add Buyer GST number field in the checkout form. It is a custom field that needs to be programmatically added to get buyer GSTIN from the customers. Here. I’ll show how to add custom field in address form in Magento 2.
Method to Add Custom Field in Address Form in Magento 2:
- Create registration.php file in app\code\[Vendor]\[Namespace]\
123456<?php\Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE,'[Vendor]_[Namespace]',__DIR__); - Create module.xml file in app\code\[Vendor]\[Namespace]\etc
1234<?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="[Vendor]_[Namespace]" setup_version="1.0.0"/></config> - Create di.xml file in app\code\[Vendor]\[Namespace]\etc
1234567<?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="gst_number" type="[Vendor]\[Namespace]\Plugin\Checkout\Model\LayoutProcessor" sortOrder="100"/></type></config> - Create LayoutProcessor.php in [Vendor]\[Namespace]\Plugin\Checkout\Model
1234567891011121314151617181920212223242526272829303132333435363738394041424344<?phpnamespace Vendor\Extension\Plugin\Checkout\Model;use Magento\Checkout\Block\Checkout\LayoutProcessor as ChekcoutLayerprocessor;class LayoutProcessor{protected $dataHelper;public function __construct(\Vendor\Extension\Helper\Data $dataHelper){$this->dataHelper = $dataHelper;}public function afterProcess(ChekcoutLayerprocessor $subject, array $jsLayout){$flag = false;if ($this->dataHelper->getBuyerGst()) {$flag = true;}$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['buyer_gst_number'] = ['component' => 'Magento_Ui/js/form/element/abstract','config' => ['customScope' => 'shippingAddress','template' => 'ui/form/field','elementTmpl' => 'ui/form/element/input','options' => [],'id' => 'gst_number'],'dataScope' => 'shippingAddress.buyer_gst_number','label' => 'GST Number#','provider' => 'checkoutProvider','visible' => $flag,'validation' => [],'sortOrder' => 252,'id' => 'buyer_gst_number'];return $jsLayout;}}
With the above method, you can add as many custom fields you want in the address form in Magento 2.
Feel free to mention your doubts in the Comments section below and I’d be happy to help!
Thanks!
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
13 Comments
Hello.
What should i do to show the values of my new fields at order address form. Once a order is created I need to review the order address form from admin, but I can’t edit the new fields. It doesn’t appear.
Hello Andres,
You need to save that attribute value in the quote and order table.
Thank You
what is Helper\Data.php? just a empty file?
Hello Nitro,
It’s just an example.
You can remove Vendor\Extension\Helper\Data from LayoutProcessor.php file’s construct.
Thank You
Hi Sanjay, Thx for sharing the code. however, the custom field is only visible on shipping form in checkout https://prnt.sc/100v7e1. What should we do in order to show the same in billing form https://prnt.sc/100v7x6 ? Can you please share.
Hello Laxman,
In the above code, I have used [shipping-step]. Do the same for [billing-step]
Thank you.
How do I create different types of input and how do I give it their corresponding attributes? for example, check the box or select ..
Hello Frank,
There will be 3 changes in the LayoutProcessor.php file as shown below:
'component' => 'Magento_Ui/js/form/element/select',
'elementTmpl' => 'ui/form/element/select',
'options' => [
[
'value' => '0',
'label' => 'No',
],
[
'value' => '1',
'label' => 'Yes',
]
]
Similarly set options for the checkbox too.
Thank you.
If i want to firstname and last name field both set in one row then, how is it possible?
Hello Sanjay,
How to save this value in database?
Hi Himani,
you need to create an observer to save the value if you have added a new field in the database.
Hello Sanjay,
I am getting error “Vendor\Namespace\Helper\Data does not exist”
Hello Lokesh,
You may have missed creating the Helper/Data.php file in your extension which is necessary to avoid this error.
Thank you.