Add Custom Tab in Customer Account Information in Magento 2

we can add some more custom fields to the account information for customers of Magento 2 to collect more customer data.

Step 1 : Create the folder of module

So our first step is to create the module folder and necessary files required to register a Magento module. 

Step 2 : Create etc/module.xml file

The module.xml file is the most important in the configuration. If we do not create this file then the module will not be registered in our database. And when we run the command of the Upgrade, then all the modules are checked inside the database that the version of any model has not changed.

Now we need to create a module.xml file in the app/code/Customer/Field/etc folder with the following code:

<?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="Customer_Field" setup_version="1.0.0">
      </module>
   </config>

Step 3 : Create registration.php file

Registration.php file is also required to register the module. Registration.php was not created in Magento 1 but created in Magento 2 so that we can create modules in the app/code and also in the vendor file. 
Now we need to create a registration.php file in the app/code/Customer/Field/ folder with the following code:

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


Step 4 : Create Setup/InstallData.php
Customer attributes are created inside of InstallData and UpgradeData scripts. To add new attributes to the database
Now we need to create an InstallData.php file in the app/code/Customer/Field/Setup/ folder with the following code:


<?php
 namespace Customer\Field\Setup;
 use Magento\Customer\Setup\CustomerSetupFactory;
 use Magento\Customer\Model\Customer;
 use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
 use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
 use Magento\Framework\Setup\InstallDataInterface;
 use Magento\Framework\Setup\ModuleContextInterface;
 use Magento\Framework\Setup\ModuleDataSetupInterface;
 /**
 @codeCoverageIgnore
 */
 class InstallData implements InstallDataInterface
 {
 /**
 @var CustomerSetupFactory
 */
 protected $customerSetupFactory;
 /**
 @var AttributeSetFactory
 */
 private $attributeSetFactory;
 /**
 @param CustomerSetupFactory $customerSetupFactory
 @param AttributeSetFactory $attributeSetFactory
 */
 public function __construct(
 CustomerSetupFactory $customerSetupFactory,
 AttributeSetFactory $attributeSetFactory
 ) {
 $this->customerSetupFactory = $customerSetupFactory;
 $this->attributeSetFactory = $attributeSetFactory;
 }
 /**
 {@inheritdoc}
 */
 public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
 {
 /** @var CustomerSetup $customerSetup */
 $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
 $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
 $attributeSetId = $customerEntity->getDefaultAttributeSetId();
 /** @var $attributeSet AttributeSet */
 $attributeSet = $this->attributeSetFactory->create();
 $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
 $customerSetup->addAttribute(Customer::ENTITY, 'terms_available_comment', [
     'type' => 'varchar',
     'label' => 'Terms Available Comment',
     'input' => 'text',
     'required' => false,
     'visible' => true,
     'user_defined' => true,
     'sort_order' => 161,
     'position' => 163,
     'system' => 0,
 ]);
 $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'terms_available_comment')
     ->addData([
         'attribute_set_id' => $attributeSetId,
         'attribute_group_id' => $attributeGroupId,
         'used_in_forms' => [
         'adminhtml_customer'
         ]
     ]);
 $attribute->save();
 $setup->endSetup();
 }
 } 
Step 5 : Run These Command
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush

output:

Leave a comment

Your email address will not be published. Required fields are marked *