10

How to add a new field for the registration page in Magento 2?

Magento 2 company field added

6 Answers 6

10

If you want add new field, in customer account, you need to override the register.phtml in your custom theme.

Create custom theme, then create register.phtml in following path

app/design/frontend/vendor/theme/Magento_Customer/templates/form/register.phtml

Then, copy codes, from module-customer/view/frontend/templates/form/register.phtml and paste to above created file.

Then, add your custom field:

<div class="field required">
    <label for="custom_field" class="label"><span><?= __('CustomField') ?></span></label>
    <div class="control">
        <input type="text" name="custom_field" id="custom_field" value="<?= $block->escapeHtml($block->getFormData()->getCustomField()) ?>" title="<?= __('CustomField') ?>" class="input-text" data-validate="{required:true, 'validate-phoneStrict':true}">
    </div>
</div>

Before you do that, you need to create customer attribute for your custom_field to store the database.

Create the Customer Attribute:

You need to create a custom module to do that, after create Custom Module

Create InstallData.php in following path Vendor\Module\Setup

InstallData.php

In this below code I have added custom_field attribute.

  <?php
  /**
   * Copyright © 2016 Magento. All rights reserved.
   * See COPYING.txt for license details.
   */
  namespace Vendor\Module\Setup;

  use Magento\Customer\Setup\CustomerSetupFactory;
  use Magento\Customer\Model\Customer;
  use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
  use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
  use Magento\Framework\Setup\InstallDataInterface;
  use Magento\Framework\Setup\ModuleContextInterface;
  use Magento\Framework\Setup\ModuleDataSetupInterface;

  /**
   * Install data
   * @codeCoverageIgnore
   */
  class InstallData implements InstallDataInterface
  {

      /**
       * CustomerSetupFactory
       * @var CustomerSetupFactory
       */
      protected $customerSetupFactory;

      /**
       * $attributeSetFactory
       * @var AttributeSetFactory
       */
      private $attributeSetFactory;

      /**
       * initiate object
       * @param CustomerSetupFactory $customerSetupFactory
       * @param AttributeSetFactory $attributeSetFactory
       */
      public function __construct(
          CustomerSetupFactory $customerSetupFactory,
          AttributeSetFactory $attributeSetFactory
      )
      {
          $this->customerSetupFactory = $customerSetupFactory;
          $this->attributeSetFactory = $attributeSetFactory;
      }

      /**
       * install data method
       * @param ModuleDataSetupInterface $setup
       * @param ModuleContextInterface $context
       */
      public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
      {

          /** @var CustomerSetup $customerSetup */
          $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

          $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
          $attributeSetId = $customerEntity->getDefaultAttributeSetId();

          /** @var $attributeSet AttributeSet */
          $attributeSet = $this->attributeSetFactory->create();
          $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
          /**
           * customer registration form default field mobile number
           */
          $customerSetup->addAttribute(Customer::ENTITY, 'custom_field', [
              'type' => 'varchar',
              'label' => 'Custom Field',
              'input' => 'text',
              'required' => true,
              'visible' => true,
              'user_defined' => true,
              'sort_order' => 1000,
              'position' => 1000,
              'system' => 0,
          ]);
          //add attribute to attribute set
          $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'mobile_number')
              ->addData([
                  'attribute_set_id' => $attributeSetId,
                  'attribute_group_id' => $attributeGroupId,
                  'used_in_forms' => ['adminhtml_customer', 'customer_account_create'],
              ]);

          $attribute->save();


      }
  }

After that run below command:

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy
php bin/magento cache:clean

You will see your custom filed in registration form.

Let me know if you have issue.

1
  • 4
    In which table save data ?
    – Devidas
    Commented Sep 1, 2017 at 9:22
7

You need to create a module and here is installData.php:

namespace Barcode\Unique\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    /**
     * Customer setup factory
     *
     * @var \Magento\Customer\Setup\CustomerSetupFactory
     */
    private $customerSetupFactory;
    /**
     * Init
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
    {
        $this->customerSetupFactory = $customerSetupFactory;
    }
    /**
     * Installs DB schema for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {

        $installer = $setup;
        $installer->startSetup();

        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
        $entityTypeId = $customerSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY);

        $customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, "barcode_unique");

        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "barcode_unique",  array(
            "type"     => "varchar",
            "backend"  => "",
            "label"    => "Barcode",
            "input"    => "text",
            "source"   => "",
            "visible"  => true,
            "required" => false,
            "default" => "",
            "frontend" => "",
            "unique"     => false,
            "note"       => ""

        ));

        $barcode_unique   = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "barcode_unique");

        $barcode_unique = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'barcode_unique');

      $used_in_forms[]="adminhtml_customer";
        $used_in_forms[]="checkout_register";
        $used_in_forms[]="customer_account_create";
        $used_in_forms[]="customer_account_edit";
        $used_in_forms[]="adminhtml_checkout";

        $barcode_unique->setData("used_in_forms", $used_in_forms)
            ->setData("is_used_for_customer_segment", true)
            ->setData("is_system", 0)
            ->setData("is_user_defined", 1)
            ->setData("is_visible", 1)
            ->setData("sort_order", 1002);

        $barcode_unique->save();

        $installer->endSetup();
    }
}

This will create field and you can call in phtml( ie :additionalinfocustomer.phtml) file.

You can help from these 2 URL: IBNAB and SASHAS

I hope it will help you.

0

You can refer below link to create custom fields on registration page in magento2-

https://github.com/jainmegha5395/custom-fields

This custom module will add custom field and attribute on Registration form. The attribute will also show in add or edit customer form in Magento 2 Admin.

0

You can actually let Magento 2 ask the full address on account generation by setting show_address_fields to true in the Register block - then it asks also (besides other's) for the company.

0

https://github.com/jainmegha5395/custom-fields

This link by Megha Jain has open issues and none answered so I wonder if that should be done or not. It does show the additional field "skype ID" on the form but the point is how do I edit, remove or add new ones.

I wonder why Magento has made it so tiresome to add general registration fields.

0

How we can add custom upload field in any like registration and any other custom form magento 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.