Thursday, June 25, 2020

How to Add Custom Field in Magento 2 Registration Form

Today, we are going to guide you on how to add custom field in the Magento2 registration form. In our last blog, we discussed how to save custom attribute value after customer register in magento2.
 Let see how to add custom field in the Magento2 registration form step by step:-
Step-1 Create additional.phtml file

 Create view file additional.phtml app/code/Webiators/CustomerAttribute/view/frontend/templates/additional.phtml for show field on customer registration form.

  1. <div class="field magento_username required">
  2. <label class="label" for="magento_username">
  3. <span><?= $block->escapeHtml(__('Magento Username')) ?></span>
  4. </label>
  5. <div class="control">
  6. <input type="text" name="magento_username" id="magento_username" value="" title="<?= $block->escapeHtmlAttr(__('Magento Username')) ?>" class="input-text" data-validate="{required:true}">
  7. </div>
  8. </div>
Step-2 Create customer_account_create.xml file

 Create layout file customer_account_create.xml app/code/Webiators/CustomerAttribute/view/frontend/layout/customer_account_create.xml 

  1. <?xml version="1.0"?>
  2. <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
  3. <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
  4. <body>
  5. <referenceContainer name="form.additional.info">
  6. <block class="Magento\Framework\View\Element\Template" name="form_additional_info_customer" template="Webiators_CustomerAttribute::additional.phtml"/>
  7. </referenceContainer>
  8. </body>
  9. </page>

Let me know if you have any questions or faced any issue while following this method for add custom field in the Magento2 registration form in the Comments section below. 
Thank you.

No comments: