0

I want to add default telephone field in our website Create and Account form.

I enable Telephone field under Store >> Configuration >> Customer >> Customer Configuration >> Name & Address Option >> Show Telephone (Required). But not show Telephone field in create customer account page. How can i show for it?

http://devmag.visibleone.io/phom236/customer/account/create/

2
  • You have to create a custom module for that
    – Arun Kumar
    Mar 19 at 5:57
  • just change to default theme (luma) and check if you can see it...
    – Pawan
    Mar 19 at 6:04
0

First of all Create a simple module or if you are having custom module you can also use that

Create a file

Setup/Patch/Data/AddPhoneAttribute.php

Enter your own Vendor\ModuleName in namespace

<?php
namespace VendorName\ModuleName\Setup\Patch\Data;

use Magento\Catalog\Ui\DataProvider\Product\ProductCollectionFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Config;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
use Psr\Log\LoggerInterface;

class AddPhoneAttribute implements DataPatchInterface, PatchRevertableInterface
{
   private $moduleDataSetup;
   
   private $eavSetupFactory;
  
   private $productCollectionFactory;
  
   private $logger;
   
   private $eavConfig;

   private $attributeResource;

   public function __construct(
       EavSetupFactory $eavSetupFactory,
       Config $eavConfig,
       LoggerInterface $logger,
       \Magento\Customer\Model\ResourceModel\Attribute $attributeResource,
       \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
   ) {
       $this->eavSetupFactory = $eavSetupFactory;
       $this->eavConfig = $eavConfig;
       $this->logger = $logger;
       $this->attributeResource = $attributeResource;
       $this->moduleDataSetup = $moduleDataSetup;
   }

   public function apply()
   {
       $this->moduleDataSetup->getConnection()->startSetup();
       $this->addPhoneAttribute();
       $this->moduleDataSetup->getConnection()->endSetup();
   }

   public function addPhoneAttribute()
   {
       $eavSetup = $this->eavSetupFactory->create();


       $eavSetup->addAttribute(
           \Magento\Customer\Model\Customer::ENTITY,
           'phone_number',
           [
               'type' => 'varchar',
               'label' => 'Phone Number',
               'input' => 'text',
               'required' => 1,
               'visible' => 1,
               'user_defined' => 1,
               'sort_order' => 999,
               'position' => 999,
               'system' => 0
           ]
       );

       $attributeSetId = $eavSetup->getDefaultAttributeSetId(Customer::ENTITY);
       $attributeGroupId = $eavSetup->getDefaultAttributeGroupId(Customer::ENTITY);

       $attribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'phone_number');
       $attribute->setData('attribute_set_id', $attributeSetId);
       $attribute->setData('attribute_group_id', $attributeGroupId);

       $attribute->setData('used_in_forms', [
           'adminhtml_customer',
       ]);

       $this->attributeResource->save($attribute);
   }

   public static function getDependencies()
   {
       return [];
   }

   public function revert()
   {
   }
       public function getAliases()
   {
       return [];
   }
}

Create another file view/frontend/layout/customer_account_create.xml Change Vendorname_Modulename with your own Vendor_Module

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     layout="1column"
     xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
   <body>
       <referenceContainer name="form.additional.info">
           <block class="Magento\Framework\View\Element\Template"
                  name="phone_number"
                  template="Vendorname_Modulename::extra_field.phtml"/>
       </referenceContainer>
   </body>
</page>

Create another file view/frontend/templates/extra_field.phtml

<div class="field phone_number required">
   <label class="label" for="phone_number">
       <span><?= $block->escapeHtml(__('Phone number')) ?></span>
   </label>
   <div class="control">
       <input type="text" name="phone_number" id="phone_number" value="" title="<?= $block->escapeHtmlAttr(__('Phone number')) ?>" class="input-text" data-validate="{required:true}">
   </div>
</div>

Now run and field will added to your registration form

php bin/magento setup:upgrade && php bin/magento setup:di:compile && php bin/magento s:s:d -f && php bin/magento cache:flush

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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