Let’s we Create a simple Mobile number Customer attribute in magento 2. We need to create simple module for add new customer attribute in magento 2.
Create registration.php file to register our module. File Path, app/code/Rbj/CustomerAttribute/registration.php
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Rbj_CustomerAttribute', __DIR__ );
Create module.xml file defined our module setup version to setup_module table. Create module.xml file defined our module setup version to setup_module table. File Path, app/code/Rbj/CustomerAttribute/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="Rbj_CustomerAttribute" setup_version="1.0.0"> <sequence> <module name="Magento_Customer"/> </sequence> </module> </config>
We need to define our mobile customer attribute into extension_attributes.xml file. File path,app/code/Rbj/CustomerAttribute/etc/extension_attributes.xml
Pass Classname inside for attribute in below xml as Magento\Customer\Api\Data\CustomerInterface because Its base Customer interface for create customer attribute in Magento 2. Set attribute code as your customer attribute code and define type as string.
<?xml version="1.0" ?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd"> <extension_attributes for="Magento\Customer\Api\Data\CustomerInterface"> <attribute code="mobile" type="string"/> </extension_attributes> </config>
For Create Customer attibute we need to define InstallData.php file to add entry in database of our attribute.
File Path, app/code/Rbj/CustomerAttribute/Setup/InstallData.php
<?php namespace Rbj\CustomerAttribute\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, 'mobile', [ 'type' => 'varchar', // type of attribute 'label' => 'Mobile Number', 'input' => 'text', // input type 'source' => '', 'required' => false, // if you want to required need to set true 'visible' => true, 'position' => 500, // position of attribute 'system' => false, 'backend' => '' ]); /* Specify which place you want to display customer attribute */ $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'mobile') ->addData(['used_in_forms' => [ 'adminhtml_customer', 'adminhtml_checkout', 'customer_account_create', 'customer_account_edit' ] ]); $attribute->save(); } }
Now Run the upgrade command to install our module.
You need to run command using SSh from the root of your Magento instance,
php bin/magento setup:upgrade php bin/magento setup:static-content:deploy -f php bin/magento indexer:reindex php bin/magento cache:flush
Now you can check your new mobile number attribute at admin panel.
Login with admin credential,
Go To, Customers -> All Customers -> Edit any Customer.