Last active
April 2, 2025 18:54
-
-
Save mrkhoa99/f4582535d6124709885521be185894c4 to your computer and use it in GitHub Desktop.
Add new static type customer attribute Magento 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#File app/code/Vendor/CustomerDemo/Setup/Patch/Data/CustomerAgeAttribute.php | |
<?php | |
namespace Vendor\CustomerDemo\Setup\Patch\Data; | |
use Magento\Customer\Model\Customer; | |
use Magento\Customer\Setup\CustomerSetup; | |
use Magento\Customer\Setup\CustomerSetupFactory; | |
use Magento\Framework\Setup\Patch\DataPatchInterface; | |
use Magento\Framework\Setup\ModuleDataSetupInterface; | |
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet; | |
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory; | |
class CustomerAgeAttribute implements DataPatchInterface | |
{ | |
/** | |
* @var ModuleDataSetupInterface | |
*/ | |
private $moduleDataSetup; | |
/** | |
* @var CustomerSetupFactory | |
*/ | |
private $customerSetupFactory; | |
/** | |
* @var AttributeSetFactory | |
*/ | |
private $attributeSetFactory; | |
/** | |
* CustomerAgeAttribute constructor. | |
* @param ModuleDataSetupInterface $moduleDataSetup | |
* @param CustomerSetupFactory $eavSetupFactory | |
*/ | |
public function __construct( | |
ModuleDataSetupInterface $moduleDataSetup, | |
CustomerSetupFactory $eavSetupFactory, | |
AttributeSetFactory $attributeSetFactory | |
) { | |
$this->moduleDataSetup = $moduleDataSetup; | |
$this->customerSetupFactory = $eavSetupFactory; | |
$this->attributeSetFactory = $attributeSetFactory; | |
} | |
public static function getDependencies() | |
{ | |
return []; | |
} | |
public function getAliases() | |
{ | |
return []; | |
} | |
/** | |
* @return DataPatchInterface|void | |
* @throws \Magento\Framework\Exception\LocalizedException | |
*/ | |
public function apply() | |
{ | |
/** @var CustomerSetup $eavSetup */ | |
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]); | |
$customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY); | |
$attributeSetId = $customerEntity->getDefaultAttributeSetId(); | |
/** @var $attributeSet AttributeSet */ | |
$attributeSet = $this->attributeSetFactory->create(); | |
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); | |
$customerSetup->addAttribute( | |
Customer::ENTITY, | |
'age', | |
[ | |
'type' => 'static', | |
'label' => 'Age', | |
'input' => 'text', | |
//'backend' => 'Vendor\CacheDemo\Model\Customer\Source\Attribute\Backend\Age', | |
'required' => false, | |
'sort_order' => 130, | |
'visible' => true, | |
'position' => 130, | |
'user_defined' => true, | |
'system' => false | |
] | |
); | |
$ageAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'age'); | |
$ageAttribute->addData([ | |
'attribute_set_id' => $attributeSetId, | |
'attribute_group_id' => $attributeGroupId, | |
'used_in_forms' => ['adminhtml_customer', 'customer_account_create', 'customer_account_edit'], | |
]); | |
$ageAttribute->save(); | |
} | |
} | |
#File app/code/Vendor/CustomerDemo/Setup/Patch/Schema/CustomerAgeAttribute.php | |
<?php | |
namespace Vendor\CustomerDemo\Setup\Patch\Schema; | |
use Magento\Framework\DB\Ddl\Table; | |
use Magento\Framework\Setup\Patch\PatchInterface; | |
use Magento\Framework\Setup\Patch\SchemaPatchInterface; | |
use Magento\Framework\Setup\ModuleDataSetupInterface; | |
class CustomerAgeAttribute implements SchemaPatchInterface | |
{ | |
/** | |
* @var ModuleDataSetupInterface | |
*/ | |
private $moduleDataSetup; | |
/** | |
* CustomerAgeAttribute constructor. | |
* @param ModuleDataSetupInterface $moduleDataSetup | |
*/ | |
public function __construct( | |
ModuleDataSetupInterface $moduleDataSetup | |
) { | |
$this->moduleDataSetup = $moduleDataSetup; | |
} | |
public static function getDependencies() | |
{ | |
return []; | |
} | |
public function getAliases() | |
{ | |
return []; | |
} | |
public function apply() | |
{ | |
$this->moduleDataSetup->startSetup(); | |
$this->moduleDataSetup->getConnection()->addColumn( | |
$this->moduleDataSetup->getTable('customer_entity'), | |
'age', | |
[ | |
'type' => Table::TYPE_INTEGER, | |
'nullable' => true, | |
'comment' => 'Age', | |
] | |
); | |
$this->moduleDataSetup->endSetup(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment