In this blog, I created customer attribute programmatically in magento 2. Code is supported to php8 and magento 2.4.6  😍

Let’s see, how to create customer attribute programmatically.

We are creating customer attribute name external_id.
First, Create data patch class like AddExternalIdCustomerAttribute.php file at path
 app/code/Vendor/Module/Setup/Patch/Data/AddExternalIdCustomerAttribute.php

PHP
<?php
declare(strict_types=1);

namespace MageDad\Module\Setup\Patch\Data;

use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Customer\Model\ResourceModel\Attribute as AttributeResource;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

/**
 * Creates a customer attribute for managing a customer's external system ID
 */
class AddExternalIdCustomerAttribute implements DataPatchInterface
{
    public function __construct(
        private ModuleDataSetupInterface $moduleDataSetup,
        private CustomerSetupFactory $customerSetupFactory,
        private AttributeResource $attributeResource,
    ) { }

    /**
     * Run code inside patch
     */
    public function apply()
    {
        // Start setup
        $this->moduleDataSetup->getConnection()->startSetup();
        $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);

        $customerSetup->addAttribute(
            CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
            'external_id',
            [
                'type' => 'text',
                'label' => 'External ID',
                'input' => 'text',
                'required' => false,
                'visible' => false,
                'user_defined' => true,
                'position' => 200,
                'system' => false,
                'is_used_in_grid' => true,
                'is_visible_in_grid' => true,
                'is_filterable_in_grid' => true,
                'is_searchable_in_grid' => true,
            ],
        );

        $customerSetup->addAttributeToSet(
            CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
            CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
            $customerSetup->getDefaultAttributeGroupId(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER),
            'external_id'
        );

        // Get the newly created attribute's model
        $attribute = $customerSetup->getEavConfig()
            ->getAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'external_id');

        // Make attribute visible in Admin customer form
        $attribute->setData('used_in_forms', [
            'adminhtml_customer',
            'customer_account_create',
            'customer_account_edit'
        ]);

        // Save attribute using its resource model
        $this->attributeResource->save($attribute);


        // End setup
        $this->moduleDataSetup->getConnection()->endSetup();
    }

    public function getAliases(): array
    {
        return [];
    }

    public static function getDependencies(): array
    {
        return [];
    }
}

After create patch run magento setup upgrade command to create customer attribute.
php bin/magento setup:upgrade

We can see newly created customer attribute in customer edit/add.

External ID is new created customer attribute

I hope this blog is useful for create customer attribute programmatically in magento 2. In case, I missed anything or need to add some more information, Don’t heisted to leave a comment in this blog, I’ll get back with some positive approach.

Keep loving ❤️ Keep inspiring 🤩 Keep liking 👍 No sharing 😄

1 Comment

  1. BWER Company is committed to advancing Iraq’s industrial sector with premium weighbridge systems, tailored designs, and cutting-edge technology to meet the most demanding applications.

Write A Comment