I need to add mobile number after email in the form of customer registration in magento 2.
Magento – How to i add a field mobile number at the time of customer registration form in magento 2
customer-accountmagento2registration
Related Solutions
First override Customer Resource model app\code\core\Mage\Customer\Model\Resource\Customer.php
in local
After replace code from
protected function _beforeSave(Varien_Object $customer)
{
parent::_beforeSave($customer);
if (!$customer->getEmail()) {
throw Mage::exception('Mage_Customer', Mage::helper('customer')->__('Customer email is required'));
}
$adapter = $this->_getWriteAdapter();
$bind = array('email' => $customer->getEmail());
$select = $adapter->select()
->from($this->getEntityTable(), array($this->getEntityIdField()))
->where('email = :email');
if ($customer->getSharingConfig()->isWebsiteScope()) {
$bind['website_id'] = (int)$customer->getWebsiteId();
$select->where('website_id = :website_id');
}
if ($customer->getId()) {
$bind['entity_id'] = (int)$customer->getId();
$select->where('entity_id != :entity_id');
}
$result = $adapter->fetchOne($select, $bind);
if ($result) {
throw Mage::exception(
'Mage_Customer', Mage::helper('customer')->__('This customer email already exists'),
Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS
);
}
// set confirmation key logic
if ($customer->getForceConfirmed()) {
$customer->setConfirmation(null);
} elseif (!$customer->getId() && $customer->isConfirmationRequired()) {
$customer->setConfirmation($customer->getRandomConfirmationKey());
}
// remove customer confirmation key from database, if empty
if (!$customer->getConfirmation()) {
$customer->setConfirmation(null);
}
return $this;
}
TO
protected function _beforeSave(Varien_Object $customer)
{
parent::_beforeSave($customer);
if (!$customer->getEmail()) {
throw Mage::exception('Mage_Customer', Mage::helper('customer')->__('Customer email is required'));
}
$adapter = $this->_getWriteAdapter();
$bind = array('email' => $customer->getEmail());
$select = $adapter->select()
->from($this->getEntityTable(), array($this->getEntityIdField()))
->where('email = :email');
if ($customer->getSharingConfig()->isWebsiteScope()) {
$bind['website_id'] = (int)$customer->getWebsiteId();
$select->where('website_id = :website_id');
}
if ($customer->getId()) {
$bind['entity_id'] = (int)$customer->getId();
$select->where('entity_id != :entity_id');
}
$result = $adapter->fetchOne($select, $bind);
if ($result) {
throw Mage::exception(
'Mage_Customer', Mage::helper('customer')->__('This customer email already exists'),
Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS
);
}
if($customer->getMobile()) {
$bind2 = array('mobile' => $customer->getMobile());
$select2 = $adapter->select()
->from($this->getEntityTable(), array($this->getEntityIdField()))
->where('mobile = :mobile');
if ($customer->getSharingConfig()->isWebsiteScope()) {
$bind2['website_id'] = (int)$customer->getWebsiteId();
$select2->where('website_id = :website_id');
}
if ($customer->getId()) {
$bind['entity_id'] = (int)$customer->getId();
$select2 ->where('entity_id != :entity_id');
}
$result = $adapter->fetchOne($select2, $bind2);
if ($result) {
throw Mage::exception(
'Mage_Customer', Mage::helper('customer')->__('This customer mobile already exists'),
Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS
);
}
}
// set confirmation key logic
if ($customer->getForceConfirmed()) {
$customer->setConfirmation(null);
} elseif (!$customer->getId() && $customer->isConfirmationRequired()) {
$customer->setConfirmation($customer->getRandomConfirmationKey());
}
// remove customer confirmation key from database, if empty
if (!$customer->getConfirmation()) {
$customer->setConfirmation(null);
}
return $this;
}
1- You have to create the attribute phone_number
and then display it for the customer in register.phtml
and edit.phtml
, you have to follow this tuto
2- You have to Rewritte loginPostAction()
from AccountController.php
to your local codepool, you can follow this tuto and then you add this piece of code in loginPostAction()
:
if (strpos($login['username'], '@') === false){ //the user has entred a phone number
$telUser = $login['username'];
$customer = Mage::getModel('customer/customer')->getCollection()
->addAttributeToFilter('phone_number', $telUser)
->getFirstItem();
if($telUser == $customer->getPhoneNumber()){
$login['username'] = $customer->getEmail();
}
}
Best Answer
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
You'll also need to register your module with etc/module.xml
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
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
Also create view/frontend/templates/add_company_name_to_client.phtml (In your case you can name it whatever you want).
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();