Create your custom module first.
Add below code to setup folder create CustomerSetup.php file at app/code/VendorName/Modulename/Setup/CustomerSetup.php
<?php
namespace VendorName\Modulename\Setup;
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;
/**
* @codeCoverageIgnore
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class CustomerSetup extends EavSetup {
/**
* EAV configuration
*
* @var Config
*/
protected $eavConfig;
/**
* Init
*
* @param ModuleDataSetupInterface $setup
* @param Context $context
* @param CacheInterface $cache
* @param CollectionFactory $attrGroupCollectionFactory
* @param Config $eavConfig
*/
public function __construct(
ModuleDataSetupInterface $setup,
Context $context,
CacheInterface $cache,
CollectionFactory $attrGroupCollectionFactory,
Config $eavConfig
) {
$this -> eavConfig = $eavConfig;
parent :: __construct($setup, $context, $cache, $attrGroupCollectionFactory);
}
/**
* Gets EAV configuration
*
* @return Config
*/
public function getEavConfig() {
return $this -> eavConfig;
}
}
Create InstallData.php file at app/code/VendorName/Modulename/Setup/InstallData.php add below code to it
<?php
namespace VendorName\Modulename\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;
/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{
/**
* EAV setup factory
*
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* Customer setup factory
*
* @var CustomerSetupFactory
*/
private $customerSetupFactory;
/**
* Init
*
* @param EavSetupFactory $eavSetupFactory
* @param CustomerSetupFactory $customerSetupFactory
*/
public function __construct(EavSetupFactory $eavSetupFactory, CustomerSetupFactory $customerSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
$this->customerSetupFactory = $customerSetupFactory;
}
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$customerEntity = $this->eavConfig->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
/** @var $attributeSet AttributeSet */
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY,
'suspend_account_customer',
[
'label' => 'User Note',
'system' => 0,
'position' => 100,
'sort_order' =>100,
'visible' => true,
'note' => '',
'type' => 'text',
'input' => 'textarea',
]
);
$attribute = $customerSetup->getEavConfig()
->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'suspend_account_customer')
->addData(
[
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms'=> ['adminhtml_customer', 'customer_account_edit']
]
);
$attribute->save();
}
}
Run below command after adding this code to your custom module.
php bin/magento setup:upgrade
Note: If your test module is already exists, please remove entry from setup_module table and then run command