Magento 2 create a custom attribute and save data
Create a custom module and follow the below step
Create InstallData.php file in VenderModulenameSetup
<?php
namespace VenderModulenameSetup;
use MagentoFrameworkModuleSetupMigration;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
class InstallData implements InstallDataInterface {
private $customerSetupFactory;
public function __construct(MagentoCustomerSetupCustomerSetupFactory $customerSetupFactory) {
$this->customerSetupFactory = $customerSetupFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
$installer = $setup;
$installer->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$entityTypeId = $customerSetup->getEntityTypeId(MagentoCustomerModelCustomer::ENTITY);
$customerSetup->addAttribute(MagentoCustomerModelCustomer::ENTITY, "custom_attribute", array(
"type" => "int",
"backend" => "",
"label" => "Custom Attribute",
"input" => "select",
"visible" => true,
'source' => 'MagentoEavModelEntityAttributeSourceTable',
"required" => false,
"default" => "",
"frontend" => "",
"unique" => true,
"note" => "",
));
$employee = $customerSetup->getAttribute(MagentoCustomerModelCustomer::ENTITY, "custom_attribute");
$employee = $customerSetup->getEavConfig()->getAttribute(MagentoCustomerModelCustomer::ENTITY, 'custom_attribute');
$used_in_forms[] = "adminhtml_customer";
$used_in_forms[] = "checkout_register";
$used_in_forms[] = "customer_account_create";
$used_in_forms[] = "customer_account_edit";
$used_in_forms[] = "adminhtml_checkout";
$employee->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("is_used_in_grid", 1)
->setData("is_visible_in_grid", 1)
->setData("is_filterable_in_grid", 1)
->setData("is_searchable_in_grid", 1)
->setData("sort_order", 100);
$employee->save();
$installer->endSetup();
}
}

