Adding custom attribute to Customer
Adding custom attribute to Customer
Create InstallData.php file at app/code/{Vendor}/{Module Name}/Setup/InstallData.php
<?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 ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address'] $mobileAttribute->setData( 'used_in_forms', ['adminhtml_customer']
); $mobileAttribute->save(); }}
If like efforts, Please share, comment and subscribe for future posts and inspire more.
Comments
Post a Comment