How to add customer attribute via Patch in Magento 2
We are creating here a Boolean attribute which have value YES and NO
You need to create a file at path : Magesan\Extension\Setup\Patch\Data\CustomerAttribute.php
CustomerAttribute.php Have the below code
<?php
namespace Magesan\Extension\Setup\Patch\Data;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Eav\Model\Config;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Customer\Model\Customer;
class CustomerAttribute implements DataPatchInterface
{
const YESNO_ATTRIBUTE = "yesno_attribute";
const CUSTOMER_SOURCE = 'customer_source';
/**
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* @var Config
*/
private $eavConfig;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
/**
* __construct
*
* @param EavSetupFactory $eavSetupFactory
* @param Config $eavConfig
* @param AttributeSetFactory $attributeSetFactory
*/
public function __construct(
EavSetupFactory $eavSetupFactory,
Config $eavConfig,
AttributeSetFactory $attributeSetFactory
) {
$this->eavSetupFactory = $eavSetupFactory;
$this->eavConfig = $eavConfig;
$this->attributeSetFactory = $attributeSetFactory;
}
/**
* {@inheritdoc}
*/
public function apply()
{
$eavSetup = $this->eavSetupFactory->create();
// For YES And No Attribute.
if (!$eavSetup->getAttribute(Customer::ENTITY, self::YESNO_ATTRIBUTE)) {
$this->addYesNoAttribute($eavSetup);
}
// For Text Attribute.
if (!$eavSetup->getAttribute(Customer::ENTITY, self::CUSTOMER_SOURCE)) {
$this->addCustomerSourceCustomerAttribute($eavSetup);
}
}
public function addYesNoAttribute($eavSetup)
{
$eavSetup->addAttribute(
Customer::ENTITY,
self::YESNO_ATTRIBUTE,
[
"type" => "int",
"label" => "YES NO Attribute",
"input" => "boolean",
"source" => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
"user_defined" => true,
"required" => false,
"visible" => true,
"position" => 334,
"system" => false,
"backend" => "",
"unique" => false,
'default' => 0,
"is_used_in_grid" => true,
"is_searchable_in_grid" => true,
"is_visible_in_grid" => true,
"is_filterable_in_grid" => true,
]
);
$customerEntity = $this->eavConfig->getEntityType("customer");
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$yesNo = $this->eavConfig
->getAttribute("customer", self::YESNO_ATTRIBUTE)
->addData([
"used_in_forms" => [
"adminhtml_customer",
"customer_account_edit",
],
"attribute_set_id" => $attributeSetId,
"attribute_group_id" => $attributeGroupId,
]);
$yesNo->save();
}
public function addCustomerSourceCustomerAttribute($eavSetup)
{
$eavSetup->addAttribute(Customer::ENTITY, self::CUSTOMER_SOURCE, [
'type' => 'varchar',
'label' => 'Customer Source',
'input' => 'text',
'user_defined' => true,
'required' => false,
'visible' => true,
'position' => 334,
'system' => false,
'backend' => '',
'unique' => false,
'default' => null,
'is_used_in_grid' => true,
'is_searchable_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
]);
$customerEntity = $this->eavConfig->getEntityType(Customer::ENTITY);
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSource = $this->eavConfig
->getAttribute(Customer::ENTITY, self::CUSTOMER_SOURCE)
->addData([
'used_in_forms' => [
'adminhtml_customer',
'customer_account_edit',
],
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
]);
$customerSource->save();
}
/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [];
}
/**
* {@inheritdoc}
*/
public function getAliases()
{
return [];
}
}
Happy Coding…