cancel
Showing results for 
Search instead for 
Did you mean: 

Custom Attribute on Customer not showing data in Customer Account Information

SOLVED

Custom Attribute on Customer not showing data in Customer Account Information

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]); $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer'); $attSetId = $customerEntity->getDefaultAttributeSetId(); /** @var $attributeSet AttributeSet */ $attributeSet = $this->attributeSetFactory->create(); $attGroupId = $attributeSet->getDefaultGroupId($attSetId); $customerSetup->addAttribute(Customer::ENTITY, $attribute['code'], [ 'label' => 'Favorite Color', 'type' => 'varchar', 'input' => 'text',file, image, multilevel, price, weight 'required' => false, 'visible' => true, 'user_defined' => true, 'position' =>999, 'system' => 0, ]); $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, $attribute['code'])->addData([ 'attribute_set_id' => $attSetId, 'attribute_group_id' => $attGroupId, 'used_in_forms' => ['adminhtml_checkout', 'adminhtml_customer', 'adminhtml_customer_address', 'customer_account_create', 'customer_account_edit', 'customer_address_edit', 'customer_register_address'], ]); $attribute->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="Magento\Framework\View\Element\Template" 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="Magento\Framework\View\Element\Template" 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.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Custom Attribute on Customer not showing data in Customer Account Information

Hello @dotancohen,

 

It is because you are setting value="" in account edit form but it should be fetched from database.

Change your account edit code with below code : 

 

<?php
$customAttr = $block->getCustomer()->getCustomAttribute('CUSTOM_ATTRIBUTE');
$customerCustomAttr = $customAttr ? $customAttr->getValue() : null;
?>
<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="<?=$block->escapeHtmlAttr($customerCustomAttr);?>" class="input-text" type="text" />
    </div>
</div>

Change CUSTOM_ATTRIBUTE with your custom attribute code.

 

Hope it helps !

Problem Solved ? Click on 'Kudos' & Accept as Solution ! Smiley Happy

View solution in original post

2 REPLIES 2

Re: Custom Attribute on Customer not showing data in Customer Account Information

Hello @dotancohen,

 

It is because you are setting value="" in account edit form but it should be fetched from database.

Change your account edit code with below code : 

 

<?php
$customAttr = $block->getCustomer()->getCustomAttribute('CUSTOM_ATTRIBUTE');
$customerCustomAttr = $customAttr ? $customAttr->getValue() : null;
?>
<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="<?=$block->escapeHtmlAttr($customerCustomAttr);?>" class="input-text" type="text" />
    </div>
</div>

Change CUSTOM_ATTRIBUTE with your custom attribute code.

 

Hope it helps !

Problem Solved ? Click on 'Kudos' & Accept as Solution ! Smiley Happy

Re: Custom Attribute on Customer not showing data in Customer Account Information

Makes perfect sense, thank you.