Magento 2 – How to Add Customer Custom Field in Registration Form

custom-fieldcustomer-attributemagento2register

I want to know how to create custom field for customer and display that custom field in the registration page using plugin interpertor.

Best Answer

For creating attribute create InstallData.php in you custom module and upgrade you module

namespace Compony\Custom\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, 'custom_field', [
            'type' => 'varchar',
            'label' => 'Custom Field',
            'input' => 'text',
            'source' => '',
            'required' => false,
            'visible' => true,
            'position' => 333,
            'system' => false,
            'backend' => ''
        ]);

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

For Adding field in Dashboard edit page

Using Object in phtml :

../vendor/magento/module-customer/view/frontend/templates/form/edit.phtml

<?php $objm = \Magento\Framework\App\ObjectManager::getInstance(); ?>
<?php $customerSession = $objm->get('Magento\Customer\Model\Session'); ?>
<input type="text" class="input-text" value="<?php echo $customerSession->getCustomer()->getCustomField();?>" name="custom_field" id="custom_field" data-input="custom_field"  />

You can also do by overriding Magento\Customer\Block\Form\Edit if you don't want to use Object in phtml file

Using Overriding Block :

../Compony/Custom/Block/Magento/Customer/Form/Edit.php

<?php

namespace Compony\Custom\Block\Magento\Customer\Form;

class Edit extends \Magento\Customer\Block\Form\Edit
{
    public function getCustomField()
    {
        return $this->customerSession->getCustomer()->getCustomField();
    }

}

../Compony/Custom/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <preference for="Magento\Customer\Block\Form\Edit" type="Compony\Custom\Block\Magento\Customer\Form\Edit" />

</config>

../vendor/magento/module-customer/view/frontend/templates/form/edit.phtml

add below code where you want your field

<input type="text" class="input-text" value="<?php echo $block->getCustomField();?>" name="custom_field" id="custom_field" data-input="custom_field"  />
Related Topic