I need to add mobile number after email in the form of customer registration in magento 2.
2 Answers
In case you want to make a whole new eav attribute. (you can then create any variable you'd desire).
First off create your own module and create Setup/InstallData.php
<?php
namespace NameSpace\NameSpace\Setup;
use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
/**
* Customer setup factory
*
* @var \Magento\Customer\Setup\CustomerSetupFactory
*/
private $customerSetupFactory;
/**
* Init
*
* @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
*/
public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
{
$this->customerSetupFactory = $customerSetupFactory;
}
/**
* Installs DB schema for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$entityTypeId = $customerSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY);
$customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, "reg_code");
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "reg_code", array(
"type" => "varchar",
"backend" => "",
"label" => "Company Reg. code",
"input" => "text",
"source" => "",
"visible" => true,
"required" => false,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => ""
));
$reg_code = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "reg_code");
$reg_code = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'reg_code');
$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
$used_in_forms[]="customer_address";
$used_in_forms[]="customer_address_edit";
$reg_code->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 121);
$reg_code->save();
$installer->endSetup();
}
}
You'll also need to register your module with etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="<Vendor>_<Module>" setup_version="1.0.0" />
</config>
After this your custom attribute (in your case mobile), should appear in admin. (Check under Customer edit). This goes without saying, that you also need to run setup:upgrade and compile.
Then you'll need to display your attribute in frontend. In your case registration.
For that you can see in InstallData.php that I've included
$used_in_forms[]="customer_account_create";
Which allowes it to extend Magento core and inject a fieldset to Magento core registration form.
You'll need to create view/frontend/layout/customer_account_create.xml
<?xml version="1.0"?>
<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="my_form_additional_info_customer" template="<Vendor>_<Module>::add_company_name_to_client.phtml"/>
</referenceContainer>
</body>
</page>
Also create view/frontend/templates/add_company_name_to_client.phtml (In your case you can name it whatever you want).
<fieldset class="fieldset create account" data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>">
<div class="field reg_code">
<label for="reg_code" class="label"><span><?php /* @escapeNotVerified */ echo __('Company registration code') ?></span></label>
<div class="control">
<input type="text" name="reg_code" id="reg_code" title="<?php /* @escapeNotVerified */ echo __('Company Registration Code') ?>" class="input-text" data-validate="{required:false}" autocomplete="off">
</div>
</div>
</fieldset>
This is the gist of it. You will need to modify this code to suit your own needs.
Oh and to answer your original question, if you're going with the way of creating an entirely new attribute, then you can change the sorting order in InstallData.php with ->setData();
-
attribute value will save auto or i will have to write code for save attribute value. Jun 27, 2018 at 8:08
-
For Magento CE addition you need to create a custom customer address attribute with script or by making new module.
check link for creating new attribute in magento2:
https://www.mageplaza.com/magento-2-module-development/magento-2-add-customer-attribute-programmatically.html
After that you need to call it in
/magento/module-customer/view/frontend/templates/form/register.phtml
hope this will help you.