Create Magento Customer Attribute Boolean type Programmatically with the help of the Data patch feature.
Let’s create a customer attribute called ‘email_marketing‘ with the type boolean using programmatically and the bool type has only two possible values Yes/No.
You need to create a basic Magento module to create custom attributes for the customer.
We are going with all the required steps to finalize your customer attribute readymade once you follow all the steps given.
Create a registration.php file to register our module. File Path, app/code/Rbj/CustomerAttribute/registration.php
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Rbj_EmailMarketing',
__DIR__
);
Create a module.xml file to define our module.

Hi Rakesh,
Why we need to create extension attribute in this case?
Using extension_attribute, You can get above customer attribute easily in API call Using an extension_attributes array. extension_attribute is easily syncing with third-party application data sync using API.
How to create Customer attribute programmatically using checkbox in magento 2?
Thank you Sir for useful solution.
But I have one question, if we want to add select field () as custom attributes. and list custom module data(dynamic value) in that select field.
Then what I have to do.
(I just want to select field with dynamic value instead of text here).
Attribute is not being created in magento ce 2.2.5
Attribute is not being created in magento ce 2.2.5
Thank you Sir for useful solution.
But I have one question, if we want to add select field () as custom attributes. and list custom module data(dynamic value) in that select field.
Then what I have to do.
(I just want to select field with dynamic value instead of text here).
Hi Rakesh,
Why we need to create extension attribute in this case?
Using extension_attribute, You can get above customer attribute easily in API call Using an extension_attributes array. extension_attribute is easily syncing with third-party application data sync using API.
How to create Customer attribute programmatically using checkbox in magento 2?
to avoid using deprecated attribute->save()
use the following code.
use MagentoCustomerModelResourceModelAttribute as AttributeResourceModel;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
then
public function apply()
{
$customerSetup = $this->customerSetupFactory->create([‘setup’ => $this->moduleDataSetup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY);
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(
Customer::ENTITY,
static::STATUS_CODE_FIELD_NAME,
[
‘type’ => ‘varchar’,
‘label’ => ‘Customer Status’,
‘input’ => ‘text’,
‘required’ => false,
‘sort_order’ => 999,
‘position’ => 999,
‘visible’ => true,
‘default’ => ”,
‘user_defined’ => true,
‘system’ => 0,
‘unique’ => true,
]
);
$statusCodeAttribute = $customerSetup->getEavConfig()
->getAttribute(Customer::ENTITY, static::STATUS_CODE_FIELD_NAME)
->addData([
‘attribute_set_id’ => $attributeSetId,
‘attribute_group_id’ => $attributeGroupId,
‘used_in_forms’ => [‘adminhtml_customer’],
]);
$this->attributeResourceModel->save($statusCodeAttribute);