How to Add Custom Customer Attribute in Registration Form in Magento 2
Online store owners are always trying to collect maximum customer information which can be used for marketing purposes.
As a business owner, knowing your customer is important in order to offer the best products, services, and customer experience that makes them a loyal customer for life.
However, what type of information is required varies from business to business. Hence, you need to create custom customer attribute in Magento 2 based on your business requirements. You can Enable Middle Name in Registration Form to add middle name as custom attribute for the one who are willing for it.
For example, mobile number, interest/hobbies, images, GST number, etc. can be created which are not a part of default Magento 2 customer attributes but are important for your business to know your customers. Similarly, to show Tax/VAT Number in Registration Form in Magento 2 can be an important custom attribute for businesses to collect relevant information from customers and ensure seamless transactions while adhering to tax laws.
Now, after creating a custom customer attribute, you can add it to the signup page where all the customer details are collected. For example you can add date of birth in registration form to identify customers age group and consider it as an custom attribute.
This tutorial explains how you can add custom customer attribute in registration form in Magento 2.
Steps to Add Custom Customer Attribute in Registration Form in Magento 2:
- Create customer_account_create.xml at app/code/[Vendor]/[Module]/view/frontend/layout or for theme app/design/frontend/Theme/thememodule/Magento_Customer/layout
12345678910<?xml version="1.0"?><page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column"xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"><body><referenceContainer name="form.additional.info"><block class="Magento\Framework\View\Element\Template" name="form_additional_info_customer_custom_attribute"template="Vendor_Module::additionalInfo.phtml"/></referenceContainer></body></page> - Create additionalInfo.phtml at app/code/[Vendor]/[Module]/view/frontend templates or for theme app/design/frontend/Theme/thememodule/Magento_Customer/templates
123456789<div class="field required"><label class="label" for="custom_attribute"><span><?php echo __('Custom Attribute');?></span></label><div class="control"><input type="text" name="custom_attribute" id="custom_attribute" value=""title="<?php echo __('Custom Attribute'); ?>" class="input-text" data-validate="{required:true}"></div></div>
That’s it. It was simple. Isn’t it?
If you have queries regarding this post, feel free to ask in the Comments section below.
I would like to help you.
You may also love to read our blog post on create a custom order attribute in Magento.
Do consider sharing this post with Magento Community via social media.
Thank you.
Chandresh Chauhan
He has been with Meetanshi for more than three years now as a certified Magento developer. A silent guy whom you can always find solving clients' issues, is an avid reader too.
6 Comments
Hello. It is simple to create an attribute, but it seems nobody cares about repopulating the field value when an error occurs in the form.
For example, in registration form, if CAPTCHA is wrong, the page is reloaded to notify it. When that happens, all default fields keep the data the user has entered but the custom field appears cleared.
I have tried “$block->escapeHtmlAttr($block->getObject()->getCustomAttribute())”, the same as the default templates do, but an exception is thrown telling that “$block->getObject()” is null. Other attemps were using “$block->getFormData()” and “$block->getAttributeData()” but none works.
It seems I need a model to retrieve the data from the session. I have not found any information about this.
Can you help me, please?
This is Magento 2.3
Hello Jaime,
You need to handle value creating your own session same as the default Magento
for reference : Magento\Customer\Model\Session
Thank You
How receive this custom data in the admin customer section.
Hello,
You have to create a custom attribute named ‘custom_attribute’, similar to the registration form name.
Use this link for reference: https://meetanshi.com/blog/create-custom-customer-attribute-in-magento-2/
Thank You.
Hi, thanks for your post.
I have a question.
I have created the folder “Magento_Customer” inside the theme directory “app/design/frontend/Codazon/fastest/food_drink/”
so, I have the two files:
1.- app/design/frontend/Codazon/fastest/food_drink/Magento_Customer/layout/customer_account_create.xml
2.- app/design/frontend/Codazon/fastest/food_drink/Magento_Customer/templates/additionalInfo.phtml
question:
how can I know the Vendor and the Module to replace in “Vendor_Module” inside customer_account_create.xml?
Hello,
If you put files in your theme, then it is called in XML like:
< block class="Magento\Framework\View\Element\Template" name="form_additional_info_customer_custom_attribute" template="https://meetanshi.com/blog/add-custom-customer-attribute-registration-form-magento-2/Magento_Customer::additionalInfo.phtml"/>
Thank you.