In online store , there are pool of customers buying from your store, it is very necessary to understand your customer’s selection and choice or the type of post they are referring to. In order to draw in more customers and bringing organized format to your store, better understanding of your customers is needed so that similar type of content is shown
and displayed to them.
For designing the store for specific customers’ requirements, needs to make the Magento store a customized one. Thereby, originating the idea of using the option of adding “Custom Customer Attribute” in Magento 2. Since Magento is a user-friendly platform, it allows creating custom attributes.
A description given to an entity is called attribute, which sees the entity from all angles and defines it.
Some examples of custom customer attributes are:
1.Customers interests
2.Hobbies
3. Area/ region
4. Gender
5. Mobile Number
6. Age of customer
Method to Create Custom Customer Attribute in Magento 2:
eavSetupFactory = $eavSetupFactory; $this->eavConfig = $eavConfig; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $eavSetup->addAttribute( Customer::ENTITY, 'sample_attribute', [ 'type' => 'varchar', 'label' => 'Sample Attribute', 'input' => 'text', 'required' => 0, 'visible' => 1, 'user_defined' => 1, 'position' => 100, 'system' => 0, ] ); $customAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'sample_attribute'); /** * you can below used_in_forms types to set attribute in forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address'] */ $customAttribute->setData( 'used_in_forms', ['adminhtml_customer'] ); $customAttribute->save(); } } Now, run the below commands: php bin/magento setup:upgrade php bin/magento setup:static-content:deploy -f php bin/magento cache:flush php bin/magento cache:clean
That’s it.
Any doubts about the solution can be solved by us.
We would be pleased to help you out.
Thank you!