Create Customer Attributes in Magento 2
class InstallData implements InstallDataInterface
{
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(
\Magento\Customer\Model\Customer::ENTITY,
'attribute_name',
[
'type' => 'int',
'label' => 'Attribute Name',
'input' => 'select',
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
'required' => false,
'default' => '0',
'sort_order' => 100,
'system' => false,
'position' => 100
]
);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'attribute_name')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer','customer_account_edit'],
]);
$attribute->save();
}
}