0

How to enable DOB in the checkout page below the first name and last name and phone number field in Registration Page. I have checked so many threads in StackExchange. Still, I didn't get any proper solution.

In the backend, I have enabled DOB and telephone number field. DOB field showing only in registration page not in checkout page.

I have tried to create a custom field that is also not working. Please help me to find out a solution for this.

customer_account_create.xml

<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="form_additional_info_customer" template="Canisys_Register::additional.phtml"/>
    </referenceContainer>
</body>

InstallData.php

<?php
 /**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Canisys\Register\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 \Magento\Framework\Setup\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]);

      $customerSetup->removeAttribute(Customer::ENTITY, 'mobile_number');

      $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, 'mobile_number', [
          'type' => 'varchar',
          'label' => 'Phone Number',
          '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', 'customer_account_edit'],
          ]);

      $attribute->save();

  }
}

additional.phtml

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

I have used this code to add mobile number field in registration page. But its not working.

5
  • easy to add, show here that you have tried
    – Sohel Rana
    Nov 21, 2019 at 15:05
  • Try this.. magento.stackexchange.com/questions/135969/… Nov 21, 2019 at 15:22
  • @SohelRana. I have added the code. Please check. Nov 21, 2019 at 18:00
  • You want to add new mobile number field on registration page ? In default Magento if you'll check there is already one field called 'telephone'. Please review that. Nov 22, 2019 at 3:57
  • I want to show the telephone number filed in the registration page. I have enabled telephone number field in the backend. It is showing only in the address checkout page not in register page. Nov 22, 2019 at 5:08

0

Your Answer

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

Browse other questions tagged or ask your own question.