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:
1. Create customer_account_create.xml at app/code/[Vendor]/[Module]/view/frontend/layout or for theme app/design/frontend/Theme/thememodule/Magento_Customer/layout
<?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>
2. Create additionalInfo.phtml at app/code/[Vendor]/[Module]/view/frontend templates or for theme app/design/frontend/Theme/thememodule/Magento_Customer/templates
<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?
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.