How to add a custom field to the frontend registration form.
I already added required feilds in database by following
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
MagentoCustomerModelCustomer::ENTITY,
'sample_attribute',
[
'type' => 'varchar',
'label' => 'Sample Attribute',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'position' => 999,
'system' => 0,
]
);
$sampleAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'sample_attribute');
// more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
$sampleAttribute->setData(
'used_in_forms',
['adminhtml_customer']
);
$sampleAttribute->save();
}
my feild listed in magento admin. I’m unable to display it in frontend
3
Answers
first thing that comes to my eyes is that attributes are created just for the backend, if you set
only, it will display only in backend. You at least have to add
but you should post also the code of your register form in frontend
To add additional field in a customer account
Vendor_Modulelayoutcustomer_account_create.xml
Vendor_Moduleviewfrontendtemplatesadditionalinfocustomer.phtml
Vendor_ModuleSetupInstallData.php
After that run Below Commands:
The path of customer_account_create.xml should be
Vendor_Moduleviewfrontendlayout
.For those who are new to Magento and do not know about all the filename and filepath conventions to be precise is very important to avoid spending time searching for errors.