This article is all about creating custom customer attribute so here, I am going to explain this. Magento 2 custom attributes provide you the options to add additional required fields for the customer. So by adding this custom field in customer you can collect information that is required to support the order, fulfillment, and customer management processes.
Do you want to provide the best service for your customers in your business? so, you might need some additional fields to collect information.
By installing this custom customers attributes module of Wishusucess you can add custom attributes to the Account Registration of the customer, Address Book, and Billing Information.
How to Add Custom Attribute in Customer Account
If you want to add extra fields in the customer registration page, then for that you have to create a file named setup install data and in that, you have to set the field type keeping in mind the requirements of your attribute.
Wishusucess/CustomerAttribute/registration.php
Wishusucess/CustomerAttribute/etc/module.xml
Wishusucess/CustomerAttribute/Setup/InstallData.php
As soon as you create these three files your model will be ready and after that, you have to run the command to install the Magento model.
For example, you want to add a mobile number or phone number or some other required field to the customer account page. So you just have to follow the following code to create a field for custom attributes.
Let’s follow the below steps to Create a Custom Attribute in Magento 2 :
You may also like this :
How to Override Block, Controller, and Model in Magento 2
Step 1: Create Customer Attribute Registration File
app/code/Wishusucess/CustomerAttribute/registration.php
<?php /** * Category: Wishusucess_CustomerAttribute', * Developer: Hemant Singh Magento 2x Developer * Website: http://wwww.wishusucess.com */ \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Wishusucess_CustomerAttribute', __DIR__ );
Step 2: Create Module XML File
app/code/Wishusucess/CustomerAttribute/etc/module.xml
<?xml version="1.0"?> <!-- /** * Category: Wishusucess_CustomerAttribute', * Developer: Hemant Singh Magento 2x Developer * Website: http://wwww.wishusucess.com */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Wishusucess_CustomerAttribute" setup_version="1.0.7" /> </config>
Step 3: InstallData.php File For Customer Custom Attribute
Here, you can decide your custom customer attribute identifier name, label, and others properties types.
So you have to enter an attribute code to identify the Magento 2 attribute within the system.
There is some default rule that needs to follow while creating your InstallData PHP file.
While adding custom attributes your attribute code must begin with a letter and can include any combination of lowercase letters (a-z) and numbers (0-9).
The custom attribute code can not be greater than thirty characters in length, and also you have to keep in mind that you cannot include any special characters or spaces.
The underscore character (_) we can use while defining the name.
app/code/Wishusucess/CustomerAttribute/Setup/InstallData.php
<?php /** * Category: Wishusucess_CustomerAttribute * Developer: Hemant Singh Magento 2x Developer * Website: http://wwww.wishusucess.com */ namespace Wishusucess\CustomerAttribute\Setup; use Magento\Customer\Setup\CustomerSetupFactory; use Magento\Customer\Model\Customer; use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet; use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; /** * Install data * @codeCoverageIgnore */ class InstallData implements InstallDataInterface { /** * CustomerSetupFactory * @var CustomerSetupFactory */ protected $customerSetupFactory; /** * $attributeSetFactory * @var AttributeSetFactory */ private $attributeSetFactory; /** * initiate object * @param CustomerSetupFactory $customerSetupFactory * @param AttributeSetFactory $attributeSetFactory */ public function __construct( CustomerSetupFactory $customerSetupFactory, AttributeSetFactory $attributeSetFactory ) { $this->customerSetupFactory = $customerSetupFactory; $this->attributeSetFactory = $attributeSetFactory; } /** * install data method * @param ModuleDataSetupInterface $setup * @param ModuleContextInterface $context */ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { /** @var CustomerSetup $customerSetup */ $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer'); $attributeSetId = $customerEntity->getDefaultAttributeSetId(); /** @var $attributeSet AttributeSet */ $attributeSet = $this->attributeSetFactory->create(); $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); /** * customer registration form default field mobile number */ $customerSetup->addAttribute(Customer::ENTITY, 'wishusucess_customerattribute', [ 'type' => 'varchar', 'label' => 'Wishusucess Customer Attribute', 'input' => 'text', 'required' => false, 'visible' => true, 'user_defined' => true, 'sort_order' => 1000, 'position' => 1000, 'system' => 0, ]); //add attribute to attribute set $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'wishusucess_customerattribute') ->addData([ 'attribute_set_id' => $attributeSetId, 'attribute_group_id' => $attributeGroupId, 'used_in_forms' => ['adminhtml_customer', 'customer_account_create'], ]); $attribute->save(); } }
Now, you need to run the below Magento 2 command to get install your custom attribute module.
php bin/magento s:up php bin/magento s:s:d -f php bin/magento c:c
Now you can see the result in
Admin > All Customer > Customer Name
Suggested Post:
Cart Images: Get Product Image in Customer Cart Magento 2 REST API