Magento – Magento 2 Add custom field to customer registration form

customer-attributecustomer-registrationformsmagento2

I want to add custom fields to my Magento 2 customer registration form.
Should I use a DB UpgradeData script to add fields to the Magento DB table, or should I add my own table?

If I add my own table, how do I get my custom data and the default data to show up in the same place?

Thanks

Best Answer

You have to create a module to add the extra field to form and save with customer data, Try the below way. In this module, I have added the GST Number field.

app/code/Vendor/Module/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

app/code/Vendor/Module/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="Vendor_Module" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

app/code/Vendor/Module/Setup/InstallData.php

<?php


namespace Vendor\Module\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;

class InstallData implements InstallDataInterface
{

    private $customerSetupFactory;

    /**
     * Constructor
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'gst_number', [
            'type' => 'varchar',
            'label' => 'GST Numner',
            'input' => 'text',
            'source' => '',
            'required' => false,
            'visible' => true,
            'position' => 333,
            'system' => false,
            'backend' => ''
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'gst_number')
        ->addData(['used_in_forms' => [
                'adminhtml_customer',
                'adminhtml_checkout',
                'customer_account_create',
                'customer_account_edit'
            ]
        ]);
        $attribute->save();
    }
}

app/code/Vendor/Module/view/frontend/layout/customer_account_create.xml

<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="gst_number" template="Vendor_Module::extra_field.phtml"/> 
        </referenceContainer> 
    </body> 
</page>

app/code/Vendor/Module/view/frontend/templates/extra_field.phtml

<fieldset class="fieldset create account" data-hasrequired="<?php /* @escapeNotVerified */echo __('* Required Fields') ?>"> 
    <legend class="legend">
        <span><?php /* @escapeNotVerified */echo __('Additional Information') ?></span>
    </legend>
    <br> 
    <div class="field gst_number required"> 
        <label class="label" for="email">
            <span><?= $block->escapeHtml(__('GST Number')) ?></span>
        </label>
        <div class="control"> 
            <input type="text" name="gst_number" id="gst_number" title="<?php /* @escapeNotVerified */echo __('My Attribute') ?>" class="input-text" data-validate="{required:true}" autocomplete="off"> 
        </div> 
    </div> 
</fieldset>

Hope this will work for you.