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.
[crayon-63d3e571bf932717703776/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.