Magento 2 Add Customer Attribute Programmatically

As we all know Magento uses the EAV model structure for the customer attributes. So, in this article, I am going to explain to you how to create customer attributes programmatically in Magento 2.

First of all, you need to create one module. If you already have a module then go with that. If you are new to Magento then check our another article How To Create a Magento 2 Module.

Let's get started!!

Now I assume you already have a Magento 2 Module.

As we all know, now Magento 2 introduce a new way to modify data using Data Patches in Magento2. So, in this article, we'll create customer attributes using Data Patch in our Magento 2 Extension

Step 1: Create AddCustomerCustomAttribute.php file at app/code/Vendor/CustomerAttribute/Setup/Patch/Data and paste the below code.

saveCopyzoom_out_map
<?php namespace Vendor\CustomerAttribute\Setup\Patch\Data; use Magento\Customer\Model\Customer; use Magento\Customer\Setup\CustomerSetup; use Magento\Customer\Setup\CustomerSetupFactory; use Magento\Eav\Model\Entity\Attribute\Set; use Magento\Eav\Model\Entity\Attribute\SetFactory; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Framework\Setup\Patch\PatchRevertableInterface; class AddCustomerCustomAttribute implements DataPatchInterface, PatchRevertableInterface { /** * @var ModuleDataSetupInterface */ private $moduleDataSetup; /** * @var CustomerSetup */ private $customerSetupFactory; /** * @var SetFactory */ private $attributeSetFactory; /** * Constructor * * @param ModuleDataSetupInterface $moduleDataSetup * @param CustomerSetupFactory $customerSetupFactory * @param SetFactory $attributeSetFactory */ public function __construct( ModuleDataSetupInterface $moduleDataSetup, CustomerSetupFactory $customerSetupFactory, SetFactory $attributeSetFactory ) { $this->moduleDataSetup = $moduleDataSetup; $this->customerSetupFactory = $customerSetupFactory; $this->attributeSetFactory = $attributeSetFactory; } /** * {@inheritdoc} */ public function apply() { $this->moduleDataSetup->getConnection()->startSetup(); /** @var CustomerSetup $customerSetup */ $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]); $customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY); $attributeSetId = $customerEntity->getDefaultAttributeSetId(); /** @var $attributeSet Set */ $attributeSet = $this->attributeSetFactory->create(); $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); $customerSetup->addAttribute( Customer::ENTITY, 'custom_new_attribute', [ 'label' => 'Custom New Attribute', 'input' => 'text', 'type' => 'varchar', 'source' => '', 'required' => false, 'position' => 40, 'visible' => true, 'system' => false, 'is_used_in_grid' => true, 'is_visible_in_grid' => true, 'is_filterable_in_grid' => true, 'is_searchable_in_grid' => false, 'backend' => '' ] ); $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'custom_new_attribute'); $attribute->addData([ 'used_in_forms' => [ 'adminhtml_customer', 'customer_account_create', 'customer_account_edit' ] ]); $attribute->addData([ 'attribute_set_id' => $attributeSetId, 'attribute_group_id' => $attributeGroupId ]); $attribute->save(); $this->moduleDataSetup->getConnection()->endSetup(); } public function revert() { $this->moduleDataSetup->getConnection()->startSetup(); /** @var CustomerSetup $customerSetup */ $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]); $customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'custom_new_attribute'); $this->moduleDataSetup->getConnection()->endSetup(); } /** * {@inheritdoc} */ public function getAliases() { return []; } /** * {@inheritdoc} */ public static function getDependencies() { return []; } }

In the above code, we have created a text type attribute. Replace, custom_new_attribute with your attribute code and change the label and another attribute settings as per your need.

Step 2: Open Command line in folder root of magento and run commands for create new customer attribute programmatically.

saveCopyzoom_out_map
php bin/magento setup:upgrade php bin/magento cache:flush php bin/magento indexer:reindex

Bingo!! we are done, now go to the Admin > Customer > Edit Customer > Account Information > And find your attribute in our case it's Custom New Attribute. Newly created attribute will display like the below screenshot.

Magento 2 Add Customer Attribute Programmatically

That's it!!

If you want to Create or Remove any Product attribute programmatically in Magento then check our another article.

Also, refer Magento 2 Add Product Attribute with Options Programmatically article, if you want to add select type product attribute programmatically.

I hope this article helped you to find what you were looking for.

Bookmark it for your future reference. Do comment below if you have any other questions on that.

P.S. Do share this article with your team.

Review other articles maybe it'll help you too.




Recent Articles
Tags
Newsletter
Chrome Extension
Copyright © 2024 devhooks.in All rights reserved.
Ads OFF toggle_off
wifi_off