Adding Custom ‘Mobile Number’ Field in Magento 2 Customer Registration Form
As a developer new to Magento, you’re on the right track by considering creating your own extension for this customization. This approach ensures your changes remain isolated from core files and can be easily updated or removed if needed.
Here’s a step-by-step guide to adding a ‘Mobile Number’ field to the customer registration form without modifying any core files:
Step 1: Create a New Module
First, navigate to your Magento installation directory. Then, create a new folder structure under app/code like this:
app/code
âââ Company
âââ MobileNumberField
âââ etc
â âââ module.xml
âââ setup
â âââ patch
â â âââ data
â â âââ install-data.php
âââ registration.php
Step 2: Define the Module
In Company\MobileNumberField\registration.php, define your new module:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Company_MobileNumberField',
__DIR__
);
Step 3: Configure the Module
In etc/module.xml, configure your module:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Company_MobileNumberField" setup_version="1.0.0">
<sequence>
<module name="Magento_Customer"/>
</sequence>
</module>
</config>
Step 4: Add the Custom Field
In setup/patch/data/install-data.php, add your custom field:
<?php
$installer = $this;
$installer->startSetup();
$field = [
'entity_type_id' => $customerEntityTypeId,
'attribute_code' => 'mobile_number',
'backend_model' => 'Magento\Eav\Model\EntityAttributeBackendString',
'frontend_model' => 'Magento\Customer\Model\Form\Element\Text',
'label' => 'Mobile Number',
'input_filter_class' => '',
'default_value' => null,
'is_global' => 1,
'is_required' => 0,
'is_user_defined' => 1,
'is_visible_in_grid' => 0,
'is_filterable_in_grid' => 0
];
$installer->addAttribute(\Magento\Customer\Model\Customer::ENTITY, $field);
$installer->endSetup();
Step 5: Display the Field in Admin
To display the field in the admin customer listing page, you’ll need to modify a template file or create an observer. For simplicity, let’s modify the vendor/magento/module-customer/view/adminhtml/templates/customer/grid/columns/name.phtml file:
<?php
/** @var \Magento\Customer\Model\Data\Customer $item */
?>
<tr>
<td data-th="Name">
<?= $block->escapeHtml($item->getFirstname()) ?> <?= $block->escapeHtml($item->getLastname()) ?>
<?php if ($block->isAllowed('customer', 'edit')): ?>
<?= /* @noEscape */ $block->getEditUrl('customer_path/edit', ['id' => $item->getId()]) ?>
<?php endif; ?>
</td>
<td data-th="Email">
<?= $block->escapeHtml($item->getEmail()) ?>
</td>
<td data-th="Mobile Number"><?= /* @noEscape */ $item->getData('mobile_number') ?></td> <!-- Add this line -->
</tr>
Step 6: Compile and Enable the Module
Finally, run these commands in your terminal:
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:flush
Now, when customers register on your Magento store, they’ll see a ‘Mobile Number’ field in the form. In the admin backend customer listing page, the mobile number will also be displayed.
Remember to replace placeholders like vendor, customer_path, and edit with actual values based on your store configuration.
For more advanced customizations or if you want to learn about creating an observer, check out our guide on Extending Magento Functionality.