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?