Customer attribute use to filter customer based on value.
Create InstallData.php file at app/code/{Vendor}/{Module Name}/Setup/InstallData.php
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 |
<?php namespace Hello\CustomerAttribute\Setup; use Magento\Eav\Setup\EavSetup; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Eav\Model\Config; use Magento\Customer\Model\Customer; class InstallData implements InstallDataInterface { private $eavSetupFactory; public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig) { $this->eavSetupFactory = $eavSetupFactory; $this->eavConfig = $eavConfig; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $eavSetup->addAttribute( \Magento\Customer\Model\Customer::ENTITY, 'mobile_number', [ 'type' => 'varchar', 'label' => 'Mobile Number', 'input' => 'text', 'required' => false, 'visible' => true, 'user_defined' => true, 'position' => 999, 'system' => 0, ] ); $mobileAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'mobile_number'); // define in which forms need to use this attribute used_in_forms ['customer_account_create','checkout_register','adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address'] $mobileAttribute->setData( 'used_in_forms', ['adminhtml_customer','customer_account_create','customer_account_edit'] ); $mobileAttribute->save(); } } |
We this works for fresh module. If you need to add to already existed module, you need to use UpdateData.php instead of InstallData.php
In UpgradeData and UpgradeSchema need to add conditions of version of module.
Module version need to define at etc/module.xml
Let’s check that out too.
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 |
<?php namespace Hello\CustomerAttribute\Setup; use Magento\Eav\Setup\EavSetup; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Eav\Model\Config; use Magento\Customer\Model\Customer; class UpgradeData implements \Magento\Framework\Setup\UpgradeDataInterface { private $eavSetupFactory; public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig) { $this->eavSetupFactory = $eavSetupFactory; $this->eavConfig = $eavConfig; } public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { if (version_compare($context->getVersion(), '1.0.1', '<')) { $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $eavSetup->addAttribute( \Magento\Customer\Model\Customer::ENTITY, 'mobile_number', [ 'type' => 'varchar', 'label' => 'Mobile Number', 'input' => 'text', 'required' => false, 'visible' => true, 'user_defined' => true, 'position' => 999, 'system' => 0, ] ); $mobileAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'mobile_number'); // define in which forms need to use this attribute used_in_forms ['customer_account_create','checkout_register','adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address'] $mobileAttribute->setData( 'used_in_forms', ['adminhtml_customer','customer_account_create','customer_account_edit'] ); $mobileAttribute->save(); } } } |
If like efforts, Please share, comment and subscribe for future posts and inspire more.