In this article, we’ll see what’re customer attributes and how we add them programmatically in Magento 2.
To create customer custom attribute you need to one custom module. once the module is created you need to create
Step 1: Create setup file InstallData.php
Firstly, we will create the InstallData.php file:File: Magesam/Gstnumber/Setup/InstallData.php
<?php
namespace
Magesam\Gstnumber
\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;
use
Magento\Customer\Api\CustomerMetadataInterface;
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,
'gst_number'
,
[
'type'
=>
'varchar'
,
'label'
=>
'GST Number'
,
'input'
=>
'text'
,
'required'
=> false,
'visible'
=> true,
'user_defined'
=> true,
'position'
=> 999,
'system'
=> 0,
]
);
$attribute
=
$this
->eavConfig
->getAttribute(Customer::ENTITY,
'gst_number'
);
$attribute
->setData(
'used_in_forms'
,
[
'adminhtml_customer'
,
'customer_account_edit',
'customer_account_create'
]
);
$attribute
->save();
$eavSetup
->
addAttributeToSet(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,null,
'gst_number'
);
}
}
Step 4: Show Customer Attribute in Register form
For showing this customer attribute on the registration page, we need to do the following things :
We will add our phtml files to ‘form.additional.info’ reference name using Magesam/Gstnumber/view/frontend/layout/customer_account_create.xml
<body>
<referenceContainer name=
"form.additional.info"
>
<block
class
=
"Magento\Framework\View\Element\Template"
name=
"gst_number"
template=
"Magesame_Gstnumber::extra_field.phtml"
/>
</referenceContainer>
</body>
</page>
Now, in the extra_field.phtml
file, we will write the code to show the additional textbox:
<fieldset
class
=
"fieldset create account"
data-hasrequired=
"<?php /* @escapeNotVerified */echo __('* Required Fields') ?>"
>
<legend
class
=
"legend"
><span><?php
echo __(
'Additional Information'
) ?></span></legend><br>
<div
class
=
"field gst_number required"
>
<label
class
=
"label"
for
=
"email"
><span><?= $block->escapeHtml(__(
'GST Number'
)) ?></span>
</label>
<div
class
=
"control"
>
<input type=
"text"
name=
"gst_number"
id=
"gst_number"
title=
"<?php /* @escapeNotVerified */echo __('My Attribute') ?>"
class
=
"input-text"
data-validate=
"{required:true}"
autocomplete=
"off"
>
</div>
</div>
</fieldset>