In this blog post, we learn how to add custom attributes in the edit form in Magento2. In last blog we discussed how to add custom field in magento 2 registration form.
You can use the below code if you want to add a custom attribute in the edit form.
Step-1 Create customer_account_edit.xml file.
file: app/code/Webiators/CustomerAttribute/view/frontend/layout/customer_account_edit.xml
1 2 3 4 5 6 7 8 9 10 11 |
<?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"> <update handle="customer_account"/> <body> <referenceContainer name="form.additional.info"> <block class="Magento\Customer\Block\Form\Edit" name="magento_username" template="Webiators_CustomerAttribute::form/edit.phtml"/> </referenceContainer> </body> </page> |
Step-2 Create edit.phtml file.
file: app/code/Webiators/CustomerAttribute/view/frontend/templates/form/edit.phtml
1 2 3 4 5 6 7 |
<div class="field magento_username"> <label class="label" for="magento_username"><span><?php /* @escapeNotVerified */ echo __('Magento Username') ?></span></label> <div class="control"> <input type="text" id="magento_username" name="magento_username" value="<?php echo $block->escapeHtml($block->getCustomer()->getCustomAttribute('magento_username')->getValue()) ?>" title="<?php /* @escapeNotVerified */ echo __('Magento Username') ?>" class="input-text" autocomplete="off" /> </div> </div> |
That’s it.
Any doubts on the topic can be mentioned in the Comments section below.
Hit 5 Stars if you find this post helpful
0 (based on 0 Reviews)