For example, when we create a mobile number customer attribute in Magento 2, we need to create a module to add new customer attribute in Magento2.
Create a registration.php file to the following path
app/code/Magemonkeys/CustomerAttribute/registration.php
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Magemonkeys_CustomerAttribute', __DIR__ ); |
Create module.xml file to the following path app/code/Magemonkeys/CustomerAttribute/etc/module.xml which define our module setup version to setup_module table.
1 2 3 4 5 6 7 8 |
<?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="Magemonkeys_CustomerAttribute" setup_version="1.0.0"> <sequence> <module name="Magento_Customer"/> </sequence> </module> </config> |
Then, we need to create an extension_attributes.xml file to the following path app/code/Magemonkeys/CustomerAttribute/etc/extension_attributes.xml
which defines our mobile number customer attribute.
Pass Classname in extension_attributes.xml as Magento\Customer\Api\Data\CustomerInterface because its a base Customer interface to create customer attributes in Magento2.
Set attribute code as your customer attribute code and define type as a string.
1 2 3 4 5 6 |
<?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 attribute we need to define InstallData.php file to the following path app/code/Magemonkeys/CustomerAttribute/Setup/InstallData.php which adds an entry to our database for custom mobile number attribute.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
<?php namespace Magemonkeys\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 using SSh from the root of your Magento instance.
1 2 3 4 |
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 the admin panel.
Go To, Customers -> All Customers -> Edit any Customer.
If you want get query string params in controller file,...
Create di.xml and add the below code Magemonkey/Redirect/etc/frontend/di.xml [crayon-62846ad54ea2a360410882/] Create...
You can try below code to change local date to...
Step 1: First you need to add registration.php file in...
Step1 : Override message.js in current theme file on the...