cancel
Showing results for 
Search instead for 
Did you mean: 

Add new customer attribute in MAGENTO2

Add new customer attribute in MAGENTO2

Hi,

 

I am facing problem while adding custom attribute to registration form.

 

Below is the code:

 

app/code/Sashas/CustomerAttribute/etc/module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Sashas_CustomerAttribute" setup_version="1.0.0">
<sequence>
<module name="Magento_Customer"/>
</sequence>
</module>
</config>

 

 

 

app/code/Sashas/CustomerAttribute/Setup/InstallData.php

 

<?php


namespace Sashas\CustomerAttribute\Setup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{

/**
* @var CustomerSetupFactory
*/
protected $customerSetupFactory;

/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;

/**
* @param CustomerSetupFactory $customerSetupFactory
* @param AttributeSetFactory $attributeSetFactory
*/
public function __construct(
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}


/**
* {@inheritdoc}
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{

/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();

/** @var $attributeSet AttributeSet */
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

$customerSetup->addAttribute(Customer::ENTITY, 'magento_username', [
'type' => 'varchar',
'label' => 'Magento Username',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]);

$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'magento_username')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer'],
]);

$attribute->save();


}
}

 

In my local machine i have placed all folders then launched magento upgrade and clean cache. 

php bin/magento  setup:upgrade

 php bin/magento  cache:flush –all

 

But still i dont get any filed shown in the additional information in admin area.

 I checked in the "Customer_entity_varchar" table after the upgrade, but i dont see any column created in it.

Please let me know if anything i am missing.

 

Thanks in advance 

 

 

 

 

6 REPLIES 6

Re: Add new customer attribute in MAGENTO2

Hello 

I have the same requirement if have created installData.php in my setup folder of custom module. Hope this will help you.

 

  

<?php
/**
 * Copyright © 2015 Ipragmatech. All rights reserved.
 */
namespace Ipragmatech\Registration\Setup;

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

class InstallData implements InstallDataInterface
{

    /**
     * Customer setup factory
     *
     * @var \Magento\Customer\Setup\CustomerSetupFactory
     */
    private $_customerSetupFactory;

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

    /**
     * Installs DB schema for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $configContext =  $context->getConfig();
        $setup->startSetup();

        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->_customerSetupFactory->create([
            'setup' => $setup
        ]);

        $customerSetup->addAttribute(Customer::ENTITY, 'mobilenumber', [
            "type"     => "varchar",
            "backend"  => "",
            "label"    => "Mobile Number",
            "input"    => "text",
            "source"   => "",
            "visible"  => true,
            "required" => true,
            "default"  => "",
            "frontend" => "",
            "unique"   => false,
            "note"     => ""
        ]);

        // add attribute to form
        /** @var  $attribute */
        $attribute = $customerSetup->getEavConfig()->getAttribute('customer',
            'mobilenumber');

        $usedinform[] = "adminhtml_customer";
        $usedinform[] = "checkout_register";
        $usedinform[] = "customer_account_create";
        $usedinform[] = "customer_account_edit";
        $usedinform[] = "adminhtml_checkout";
        $attribute->setData("used_in_forms", $usedinform)
            ->setData("is_used_for_customer_segment", true)
            ->setData("is_system", 0)
            ->setData("is_user_defined", 1)
            ->setData("is_visible", 1)
            ->setData("sort_order", 100);

        $attribute->save();

        $setup->endSetup();
    }
}

Re: Add new customer attribute in MAGENTO2

Take help of this tutorial to add new customer attribute programetically.. 

https://magearray.blogspot.in/2017/11/how-to-add-custom-attribute-to-customer.html

 

Cheers

-
Magento Programmer | Was my answer helpful? You can accept it as a solution.

Re: Add new customer attribute in MAGENTO2

There is a bug in magento to see the customer field in admin you must also add:

at least:
'adminhtml_customer' AND 'customer_account_edit' to used_in_forms

'used_in_forms' => ['adminhtml_customer','customer_account_edit','customer_account_create']

 Also to find the attribute in the database look at the last row in table 'eav_attribute'

Re: Add new customer attribute in MAGENTO2

See this tutorial, I hope it will save your time

How to add a new customer attribute in magento 2

Re: Add new customer attribute in MAGENTO2

Step 1: Create InstallData.php file
Step 2: Modify data input for each kind of attribute
Step 3: [Optional] Customize layout
Step 4: Enable your module and see the result
You can read this tutorial : How to create Customer Attribute in Magento2

Re: Add new customer attribute in MAGENTO2

 

did anyone have a newer solution instead the deprecated save() method in ver. 2.3.2?