3

In Magento 2 CE we have to add new attributes with Coding? It's not provided by default?

I have looked in to http://www.extensions.sashas.org/blog/magento-2-make-customer-attribute.html

I would like to add custom customer attribute called "passport" in Magento 2 CE.

I also like to apply validation of that on both side client & server. Through jQuery regexp we can achieve this. But the main concern is where to add/in which file so it will not affect other things

4

Place this code in InstallData Script in custom module.

InstallData.php

namespace Paarth\CustomerAttribute\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{ 

    private $customerSetupFactory;      

    public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
    {
      $this->customerSetupFactory = $customerSetupFactory;
    }

    public function install(ModuleDataSetupInterface
    $setup, ModuleContextInterface $context)
    {
        /** @var CustomerSetup $customerSetup */

        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
        $setup->startSetup();

        $attributeCode = "passport";

        $customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, $attributeCode);          

        $customerSetup->addAttribute('customer',
        'passport', [
        'label' => 'Passport',
        'type' => 'text',
        'frontend_input' => 'text',
        'required' => false,
        'visible' => true,
         'system'=> 0,
        'position' => 105,
        ]);

        $loyaltyAttribute = $customerSetup->getEavConfig()->getAttribute('customer', 'passport');
        $loyaltyAttribute->setData('used_in_forms',['adminhtml_customer']);
        $loyaltyAttribute->save();

        $setup->endSetup();
    }
}
  • Added @Paarth. Not working. Not showing in DB as well :(. Internal messages between users not allowed. – Ankit Shah Aug 10 '16 at 6:13
  • Did you change namespace ? – Sourav Aug 11 '16 at 4:19
  • Nope. I think tutorial only missing registration.php file. So i have added that & it works well. Thank you for help & effort at well. Appreciated :) – Ankit Shah Aug 11 '16 at 4:23
  • Thanks Mike. can you send me skype Id or email address. – Sourav Aug 11 '16 at 4:24
  • There is no internal messaging system. How to do it? – Ankit Shah Aug 11 '16 at 5:44
0

Use $customerSetup (solution by @Paarth) and add 'adminhtml_only' => true as a part of attribute configuration instead of

$loyaltyAttribute = $customerSetup->getEavConfig()->getAttribute('customer', 'passport');   
$loyaltyAttribute->setData('used_in_forms',['adminhtml_customer']);
$loyaltyAttribute->save();

You can also add 'is_used_in_grid' => true if you want it to be available in grid

-1

First of all create a sample module called Mageplaza_HelloWorld and the InstallDataclass.

then,create InstallData.php file

on 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.

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
            ]
        );
}

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 command line to install the module: php magento setup:upgrade. Then check the customer page to see the new attribute.

I hope it helps you !!

  • this also didn't helped – Ashwani Shukla Oct 30 '18 at 10:34
  • This answer was blatantly plagiarised from the top post on google and is in essence the same method as an earlier, neater answer. – clockworkgeek Sep 12 at 15:39

protected by Community Jan 10 at 7:14

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.