0

I want to display mobile number in create an account page in Magento 2.

Iam editing below page with following code but it include address also in my create an account page, I just want telephone number attribute.

/vendor/magento/module-customer/view/frontend/layout/customer_account_create.xml

<referenceBlock name="customer_form_register">
    <action method="setShowAddressFields">
        <argument name="show.address.fields" xsi:type="boolean">true</argument>
    </action>
</referenceBlock>
1
  • did you enable Stores -> Configuration Customer -> Customer Configuration -> Name and Adress options -> Show Telephone Jan 7, 2019 at 11:53

1 Answer 1

1

1/ You need to create a mobilePhone attribute

app/code/Vendor/Module/Setup/InstallData.php

<?php
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);

        $customerSetup->addAttribute(Customer::ENTITY, 'mobile_phone', [
            'type' => 'varchar',
            'label' => 'Mobile phone',
            '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_phone')
            ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                /*use this mobile phone attribute in the all bellow forms*/
                'used_in_forms' => ['adminhtml_customer', 'customer_account_create', 'customer_account_edit', 'adminhtml_checkout', 'checkout_register'],

            ]);

        $attribute->save();
    }
}

2/

app/design/frontend/{Vendor}/{themename}/Magento_Customer/templates/form/register.phtml

Then add your custom attribute as something like this:

<div class="field phone required">
    <label for="telephone" class="label"><span><?= $block->escapeHtml(__('Mobile')) ?></span></label>
    <div class="control">
        <input type="telephone" name="mobile_phone" id="mobile_phone" value="<?= $block->escapeHtmlAttr($block->getFormData()->getMobilePhone()) ?>" title="<?= $block->escapeHtmlAttr(__('Mobile Phone')) ?>" class="input-text" data-validate="{required:true, 'validate-mobilePhoneNumber':true}">
     </div>
 </div>

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.