0

I have used below code in my setup folder to create custom customer attributes,

 use Magento\Eav\Setup\EavSetup;
 use Magento\Eav\Setup\EavSetupFactory;
 use Magento\Framework\Setup\UpgradeDataInterface;
 use Magento\Framework\Setup\ModuleContextInterface;
 use Magento\Framework\Setup\ModuleDataSetupInterface;

 class UpgradeData implements UpgradeDataInterface
 {
   private $eavSetupFactory;
   public function __construct(EavSetupFactory $eavSetupFactory)
   {
     $this->eavSetupFactory = $eavSetupFactory;
   }

public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    /** @var EavSetup $eavSetup */
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

    if (version_compare($context->getVersion(), '2.2.4') < 0){

            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $customerSetup = $objectManager->create('[Vendor]\[Module]\Setup\CustomerSetup');
            $customerSetup->installAttributes($customerSetup);



      }
  }

}

And below code used inside CustomerSetup.php

use Magento\Eav\Model\Config;
use Magento\Eav\Model\Entity\Setup\Context;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;

class CustomerSetup extends EavSetup {

protected $eavConfig;

public function __construct(
    ModuleDataSetupInterface $setup,
    Context $context,
    CacheInterface $cache,
    CollectionFactory $attrGroupCollectionFactory,
    Config $eavConfig
    ) {
    $this -> eavConfig = $eavConfig;
    parent :: __construct($setup, $context, $cache, $attrGroupCollectionFactory);
} 

public function installAttributes($customerSetup) {
    $this -> installCustomerAttributes($customerSetup);
} 

public function installCustomerAttributes($customerSetup) {

    $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY,
        'org_name',
        [
            'label' => 'Organisation Name',
            'system' => 0,
            'position' => 100,
            'sort_order' =>100,
            'visible' =>  true,
            'note' => '',
            'type' => 'varchar',
            'input' => 'text'               
        ]
    );
   $customerSetup->getEavConfig()->getAttribute('customer', 'org_name')->setData('is_user_defined',1)->setData('is_required',1)->setData('default_value','')->setData('used_in_forms', ['adminhtml_customer', 'customer_account_create'])-> save();
}
 public function getEavConfig() {
    return $this -> eavConfig;
 } 
} 

This has worked perfectly, and the attribute is created, Now i need to add another attribute.

How that can be done, I need to edit the same file for new attribute? What changes i need to do suppose i like to add new attribute called customer_example?

1 Answer 1

1

There is no need to create a new file, you can edit the same file and add code to include the new attribute.
You just need to update the setup_version in your module.xml file.
The second thing that you need to take care of is not adding the attribute that has already been there.
There are multiple ways to check this thing.
You can go through any Magento module's setup upgrade file for having a look at how Magento does this.
you can check code in file vendor/magento/module-customer/Setup/UpgradeData.php for better understanding.

UPDATE

if (version_compare($context->getVersion(), '2.2.4') < 0) {
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $customerSetup = $objectManager->create('[Vendor]\[Module]\Setup\CustomerSetup');
    $customerSetup->installAttributes($customerSetup);
}
if (version_compare($context->getVersion(), '2.2.5') < 0) {
    //code to add new attribute
}

Something like above will work.

NOTE: It is not recommended and not a good practice to use ObjectManager directly in your code, Magento highly recommends to include classes/objects using constructor dependency injection methodology.

6
  • hi, yes i will update my version in module.xml file, that i got it. second thing take care for is not adding the attribute that has already been there, for this one i need to delete all the other code written to create attributes previously? Commented Aug 10, 2018 at 10:48
  • I am not clear about this point, what should i do? can you pls update me the code i have written in question. Commented Aug 10, 2018 at 10:48
  • check the updated answer, you will get what I am trying to say. Commented Aug 10, 2018 at 11:10
  • Yes, i understood what you are saying now, but i checked my module.xml file. I seen the version is 2.0.14, why the code is used 2.2.4 i am not getting. Actually someone written this, i am not written code before Commented Aug 10, 2018 at 11:18
  • 2.2.4 was just an example of how to use version numbers. If in your case it is 2.0.14, then change it to 2.0.15 Commented Aug 10, 2018 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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