PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 16, 2022

[FIXED] How to add custom field in Customer Registration on Magento 2.2?

 October 16, 2022     custom-fields, magento, magento2.2, php, registration     No comments   

Issue

  1. I want to add Custom field in Customer Registration on Magento 2.2
  2. I 've tried following code but it's not working.
  3. I tried InstallData.php

      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
      {
          protected $customerSetupFactory;
    
          private $attributeSetFactory;
    
          public function __construct(CustomerSetupFactory $customerSetupFactory, AttributeSetFactory $attributeSetFactory)
          {
              $this->customerSetupFactory = $customerSetupFactory;
              $this->attributeSetFactory = $attributeSetFactory;
          }
    
          public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
          {
    
              $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
    
              $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
              $attributeSetId = $customerEntity->getDefaultAttributeSetId();
    
              $attributeSet = $this->attributeSetFactory->create();
              $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
              $customerSetup->addAttribute(Customer::ENTITY, 'intrestedin', [
                  'type' => 'varchar',
                  'label' => 'Custom Field',
                  'input' => 'text',
                  'required' => true,
                  'visible' => true,
                  'user_defined' => true,
                  'sort_order' => 1000,
                  'position' => 1000,
                  'system' => 0,
              ]);
              $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();
          }
      }
    

Solution

Please try to create a module and add customer attribute. you can create module from here- https://mage2gen.com/

After installing the module , you can run setup upgrade and deploy command.



Answered By - Abhinav Kumar Singh
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing