Magento 2 Create Customer Attribute


Magento 2 create Customer Attribute Using Installer Script – It is very simple to create customer custom attribute in Magento 2. Here in this tutorial we are going to create one customer attribute called customer_discount in Magento 2 using installer script.
In this article we will use sample module Tutorialsplane_HelloWorld.


Magento 2 create Customer Attribute Programmatically Example

You need to keep the following points in mind while creating customer attribute-

  • 1.Create extension_attributes.xml file-
  • 2.Create InstallData.php File-

The directory structure of sample module Tutorialsplane_HelloWorld contains the follwing folder structure –

Magento 2 Create Customer Attribute Programmatically

Now let us create the required files to create customer attribute –

Step 1 Create extension_attributes.xml file

Create extension_attributes.xml in etc folder and add the following content-

Magento 2 Customer Attribute extension_attributes.xml File Example:

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
	<extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
		<attribute code="customer_discount" type="string"/>
	</extension_attributes>
</config>

Step 2 Create InstallData.php file

Create InstallData.php in Setup folder and add the following code –

Customer Attribute Create InstallData Example:

<?php


namespace Tutorialsplane\HelloWorld\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;

class InstallData implements InstallDataInterface
{

    private $customerSetupFactory;

    /**
     * Constructor
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->addAttribute('customer', 'customer_discount', [
            'type' => 'varchar',
            'label' => 'Customer Discount',
            'input' => 'text',
            'source' => '',
            'required' => false,
            'visible' => true,
            'position' => 333,
            'system' => false,
            'backend' => ''
        ]);

        
        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'customer_discount')
            ->addData(['used_in_forms' => [
                'adminhtml_customer',
                'adminhtml_checkout'
            ]]);
        $attribute->save();
    }
}

As you the above file contains the install() which is resposible to create the customer attribute. In the above example “Customer Discount” attribute with attribute code customer_discount. You can define the other parameters as per your requirement. In this example we have shown this attribute on admin customer form and admin checkout page.

Download Sample Code

If you want to download the sample module you can download simply from here – Magento 2 Create Customer Attribute Sample Module


Advertisements

Add Comment

📖 Read More