I have created two customer attribute one "abc" and second is "xyz" this two attribute display properly in customer admin. also attributes are created in database table "eav_attribute". But while save new customer with this field data are not save this into database. And also can not able to get this custom attribute value in frontend header side. plz check file.
app/code/Test/CustomerAttribute/Setup/InstallData.php
<?php
namespace Test\CustomerAttribute\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface {
private $customerSetupFactory;
public function __construct (
\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup'=> $setup]);
$setup->startSetup();
$customerSetup->addAttribute('customer', 'account_id', [
'label' => 'Account Id',
'type' => 'varchar',
'input' => 'text',
'required' => false,
'visible' => true,
'position' => 105,
"unique" => false,
'user_defined' => true,
'system' => 0,
'visible_on_front' => true,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
'is_searchable_in_grid' => true,
]);
$loyaltyAttribute = $customerSetup->getEavConfig()->getAttribute('customer', 'account_id');
$loyaltyAttribute->setData('used_in_forms',['adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address','adminhtml_checkout']);
$loyaltyAttribute->save();
$customerSetup->addAttribute('customer', 'amount_spend', [
'label' => 'Amount Spend',
'type' => 'varchar',
'input' => 'text',
'required' => false,
'visible' => true,
'position' => 106,
"unique" => false,
'user_defined' => true,
'system' => 0,
'visible_on_front' => true,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
'is_searchable_in_grid' => true,
]);
$amount_spend = $customerSetup->getEavConfig()->getAttribute('customer', 'amount_spend');
$amount_spend->setData('used_in_forms',['adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address','adminhtml_checkout']);
$amount_spend->save();
$setup->endSetup();
}
}
If you have any idea plz share it
thanks