I have added a field “Favorite Color” to the Customer entity and added it to the registration form. The field properly stores data, as I can see when logging in as Admin and checking in Customers -> All Customers -> Edit -> Account Information. However, when the Customer herself logs in that field is empty in his personal Account Information section. The module.xml file does of course list a dependency on Magento_Customer.
I’ve added the field with this UpgradeData:
public function upgrade(ModuleDataSetupInterface $setup)
{
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$params = [
'label' => 'Favorite Color',
'type' => 'varchar',
'input' => 'text',file, image, multilevel, price, weight
'required' => false,
'visible' => true,
'user_defined' => true,
'position' =>999,
'system' => 0,
];
$customerSetup->addAttribute('customer_address', 'favorite_color', $params);
$customAttribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'favorite_color');
$customAttribute->setData('used_in_forms', ['adminhtml_checkout', 'adminhtml_customer', 'adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']);
$customAttribute->save();
}
I use the following layout to add a template to the account creation form:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="form.additional.info">
<block class="MagentoFrameworkViewElementTemplate" name="dotan_customer_form_additional_info" template="Dotan_Customer::form/customer_account_create_additional_info.phtml">
</block>
</referenceContainer>
</body>
</page>
And a near-identical layout to add a template to the account edit form:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="form.additional.info">
<block class="MagentoFrameworkViewElementTemplate" name="dotan_customer_form_edit_additional_info" template="Dotan_Customer::form/customer_account_edit_additional_info.phtml">
</block>
</referenceContainer>
</body>
</page>
Here is the template added to the account creation form:
<div class="field favorite_color">
<label for="favorite_color" class="label"><span><?php /* @escapeNotVerified */ echo __('Favorite Color') ?></span></label>
<div class="control">
<input type="text" name="favorite_color" id="favorite_color" title="Favorite Color" value="" class="input-text" type="text" />
</div>
</div>
And the identical template added to the account edit form:
<div class="field favorite_color">
<label for="favorite_color" class="label"><span><?php /* @escapeNotVerified */ echo __('Favorite Color') ?></span></label>
<div class="control">
<input type="text" name="favorite_color" id="favorite_color" title="Favorite Color" value="" class="input-text" type="text" />
</div>
</div>
I suspect that I need to add the fields’ content to the account edit form template. But does that need to be done manually, via e.g. a block that fetches the vales from the database? I’ve looked at many tutorials online but none of them mention the need for such a block. And the information is being provided in the Adminhtml area’s Customer -> Account Information, so I suspect that there is some built-in functionality to provide that information to the Customer’s own Account Information page as well.