SEO Ultimate package released! Grab 20% off - SEO20 Explore

Magento 2 Add Customer Attribute Programmatically

This article will guide you how to create a add customer attribute in Magento 2 programatically. Please follow our previous article to create a simple module which we will use to demo coding for this lesson and how to create the setup script classes. In this article, we will use the sample module Mageplaza_HelloWorld and the InstallDataclass.

Overview of Adding Customer Attribute Programmatically

  • Step 1: Create setup file InstallData.php
  • Step 2: Define the install() method
  • Step 3: Create custom attribute

Step 1: Create setup file InstallData.php

Firstly, we will create the InstallData.php file:

File: app/code/Mageplaza/Example/Setup/InstallData.php

<?php
namespace Mageplaza\Example\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;

class InstallData implements InstallDataInterface
{
	private $eavSetupFactory;

	public function __construct(EavSetupFactory $eavSetupFactory)
	{
		$this->eavSetupFactory = $eavSetupFactory;
	}

}

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

Step 2: Define the install() method

After that, we have to define the install() method and create eav setup model:

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
	{
		$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
	}
	Next, we will use eavSetup object to add attribute:
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
	{
		$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
		$eavSetup->addAttribute(
			\Magento\Customer\Model\Customer::ENTITY,
			'sample_attribute',
			[
				'type' => 'int',
				'label' => 'Sample Attribute',
				'input' => 'select',
				'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
				'required' => true,
				'default' => '0',
				'sort_order' => 100,
				'system' => false,
				'position' => 100
			]
		);
}

Step 3: Create custom attribute

Finally, we need to set the forms in which the attributes will be used. In this step, we need define the eavConfig object which allow us to call the attribute back and set the data for it. And the full code to create customer attribute is:

File: app/code/Mageplaza/Example/Setup/InstallData.php

<?php
namespace Mageplaza\Example\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;

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' => 'int',
				'label' => 'Sample Attribute',
				'input' => 'select',
				'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
				'required' => true,
				'default' => '0',
				'sort_order' => 100,
				'system' => false,
				'position' => 100
			]
		);
		$sampleAttribute = $this->eavConfig->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'sample_attribute');
		$sampleAttribute->setData(
			'used_in_forms',
			['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
		);
		$sampleAttribute->save();
	}
}

Now, let run comman line to install the module: php magento setup:upgrade. Then checking the customer page to see the new attribute.



Next tutorial:

Module Development Series



Enjoyed the tutorial? Spread it to your friends!

magento-2-module-development
hello-world
admin
customer
attribute

Module Development

Comments for Magento 2 Add Customer Attribute Programmatically

Please leave comments if you have any questions, feedbacks.

You also may like these Magento 2 Extensions

One Step Checkout

$199
10 reviews

Layered Navigation

$99
12 reviews

SEO

FREE
no review

Shop By Brand

$99
3 reviews

People also searched for:

  • Magento 2 Add Customer Attribute
  • Magento 2 Add Customer
  • Magento 2 Add Customer Attribute Programmatically
  • magento 2 add customer attributes extension
  • magento 2 add customer attribute
  • magento 2 add custom attribute checkout
  • magento 2 how to add customer attribute
  • magento 2 add custom image attribute to product
  • add customer attribute in magento 2
  • magento 2 add custom attribute to module
  • magento 2 add multi select attribute
  • magento 2 add attribute module
  • magento 2 customer add new attribute
  • magento 2 add custom attribute to product programmatically