Allowing Duplicate Email Addresses for Magento Customer Registration
In many e-commerce scenarios, allowing duplicate email addresses during customer registration can be beneficial. This is especially true when you have unique requirements like using usernames instead of emails for login or when importing customers from various sources.
The Challenge with Default Magento Functionality
Magento, by default, enforces a strict validation that prevents multiple users from registering with the same email address. While this is great for preventing duplicate accounts intentionally created by bots or malicious actors, it can be restrictive in certain business cases.
The existing customer registration process checks if an email already exists at the following points:
- Frontend: During customer self-registration.
- Admin panel: When adding customers manually from the backend.
- Customer CSV import: When importing customers using a CSV file.
- V2_API: If your store uses API for creating customers.
Our Approach to Allowing Duplicate Emails
To allow duplicate email addresses, we need to override and modify several core Magento functionalities. Here’s how:
Step 1: Override the Default Email Validation
The default validation is located in Magento\Customer\Model\AccountManagement. We’ll create a custom plugin around this model.
/**
* @param \Closure $proceed
* @param \Magento\Framework\DataObject $customer
* @return bool|void
*/
public function afterIsEmailAvailable(\Magento\Framework\DataObject $customer, callable $proceed)
{
if ($customer->getData('email') === 'duplicate@example.com') {
return true;
}
return $proceed();
}
This custom plugin checks for a specific email (duplicate@example.com in this example) and allows it to pass the validation even if it’s already registered.
Step 2: Modify the Admin Panel Registration
The admin panel also has its own validation process. To override this, we need to create a custom extension attribute and use that instead of the default email field for backend customer creation.
/**
* @param \Magento\Customer\Model\Customer $model
* @return array
*/
public function addCustomAttributes(\Magento\Framework\Api\AttributeInterface $model)
{
return [
'duplicate_email' => [
'label' => __('Duplicate Email'),
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true
],
];
}
Step 3: Update the Customer CSV Import
For the customer CSV import, you can modify the Magento\Customer\Import\Customer model to ignore the email validation.
/**
* @return bool
*/
public function validate()
{
if ($this->getEmail() === 'duplicate@example.com') {
return true;
}
return parent::validate();
}
Step 4: Modify the V2_API for Customer Creation
For API-based customer creation, you can use a similar approach as the admin panel registration. Create a custom field called duplicate_email and validate it independently.
Conclusion
Allowing duplicate email addresses in Magento requires careful modification of several core functionalities. By using plugins and extending core models, we can override the default validation rules without directly modifying core files.
Remember to thoroughly test these changes across all possible customer registration methodsâfrontend form, admin panel, CSV import, and APIâto ensure consistency.
This approach provides a clean and maintainable solution that respects Magento’s core principles while meeting your specific business requirements.