0

I followed several tutorials like https://marketplace.magento.com/ followed the step but no luck for me. how to add 2 extra field in customer or user registration for frontend ,admin and database . any help would be appreciated.

1 Answer 1

0

For Example you need to insert the required field(telephone) into the table. I will always encourage to accomplish this using a custom module, although it is time taking, it is a standard and cleanest way.

  1. Create a standard module with minimal required files.
  2. Create InstallData.php in the following path NameSpace\ModuleName\Setup
namespace NameSpace\ModuleName\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;


  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, 'telephone', [
              'type' => 'varchar',
              'label' => 'Telephone',
              '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, 'telephone')
              ->addData([
                  'attribute_set_id' => $attributeSetId,
                  'attribute_group_id' => $attributeGroupId,
                  'used_in_forms' => ['adminhtml_customer', 'customer_account_create'],
              ]);

          $attribute->save();


      }
  }
  1. Add the field to the frontend by copying module-customer/view/frontend/templates/form/register.phtml to app/design/frontend/namespace/theme/Magento_Customer/templates/form/register.phtml and add
<div class="field required">
    <label for="telephone" class="label"><span><?= __('Telephone') ?></span></label>
    <div class="control">
        <input type="text" name="telephone" id="telephone" value="<?php echo $block->escapeHtml($block->getFormData()->getTelephone()) ?>" title="<?php echo __('Telephone') ?>" class="input-text" data-validate="{required:true, 'validate-phoneStrict':true}">
    </div>
</div>
php bin/magento setup:upgrade
php bin/magento c:c
php bin/magento c:f

FOR MAGENTO 1.x PLEASE REFER TO THE LINK BELOW

How to create additional fields for Customer Registration

6
  • thanks to reply but my question was for magento1.9. can you please do same for Magento 1
    – irshad
    Aug 30, 2019 at 5:34
  • Okay i'll try.... Aug 30, 2019 at 11:19
  • @irshad I'm updating my Question with a reference link, if u'll find it helpfull please accept is as answer. Aug 30, 2019 at 13:15
  • thanks it's helpful
    – irshad
    Aug 31, 2019 at 12:15
  • @irshad Please accept it... Sep 2, 2019 at 10:40

Your Answer

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

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