Quantcast
Channel: Excellence Technologies Magento Blog | Magento Tutorials | Magento Developer
Viewing all articles
Browse latest Browse all 69

Creating Customer Attribute Programmatically in Magento 2

$
0
0

As we know, in Enterprise edition of Magneto, a customer attribute can be created from Admin Panel itself. But, no such option is available for Community Edition.

But no worries, a customer attribute can be created programmatically in both of the editions. A custom module needs to be created for this one. The setup script of the module (InstallData.php) will be used to perform this task. Follow these steps:

1. Create setup script (InstallData.php)

Create file app/code/Excellence/Hello/Setup/InstallData.php

Add following code to the file:

<?php

namespace Excellence\Hello\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Config;
use Magento\Customer\Model\Customer;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;
    
    public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig)
    {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig       = $eavConfig;
    }
}

In this class, we define the EAV setup model which will be used to interact with Magento 2 attribute.

2. Defining The Install() method

In this step, we will be creating Install() method and create EAV setup model which we will do the job

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    $eavSetup->addAttribute(
        \Magento\Customer\Model\Customer::ENTITY,
        'sample_attribute',
        [
            'type'         => 'varchar',
            'label'        => 'Sample Attribute',
            'input'        => 'text',
            'required'     => false,
            'visible'      => true,
            'user_defined' => true,
            'position'     => 999,
            'system'       => 0,
        ]
    );
    $sampleAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'sample_attribute');

    // more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
    $sampleAttribute->setData(
        'used_in_forms',
        ['adminhtml_customer']

    );
    $sampleAttribute->save();
}

Here, the used_in_forms is used to show the attribute in different kind of forms such as customer registration page, account information page etc.

Complete Install Script:

Here is the complete Install script which is to be used in InstallData.php:

<?php

namespace Excellence\Hello\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Config;
use Magento\Customer\Model\Customer;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;
    
    public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig)
    {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig       = $eavConfig;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'sample_attribute',
            [
                'type'         => 'varchar',
                'label'        => 'Sample Attribute',
                'input'        => 'text',
                'required'     => false,
                'visible'      => true,
                'user_defined' => true,
                'position'     => 999,
                'system'       => 0,
            ]
        );
        $sampleAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'sample_attribute');

        // more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
        $sampleAttribute->setData(
            'used_in_forms',
            ['adminhtml_customer']

        );
        $sampleAttribute->save();
    }
}

After that run setup:upgrade and setup:static-content:deploy to see the changes. Also, you might need to set permissions of directories like pub, vargenerated etc.

The post Creating Customer Attribute Programmatically in Magento 2 appeared first on Excellence Technologies Magento Blog | Magento Tutorials | Magento Developer.


Viewing all articles
Browse latest Browse all 69

Trending Articles