- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
How to assign a group to a user based on their email domain in Magento 2.3
I need during registration process to assign a different group to users that has a specific domain e-mail:
So user with: abc@domain.com, 123@domain.com, …@domain.com will be assigned to a specific group all others will be assigned to default group.
I searched a lot, but I found only solutions for Magento 1.
Thank you
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to assign a group to a user based on their email domain in Magento 2.3
Hello @SoulMan77
Did you ask to amasty guys? In their extension, they have email in their rule which you can use. I think that's for the same purpose you are looking for.
Ask them once and then purchase.
https://amasty.com/customer-group-auto-assign-for-magento-2.html
We also have that extension but don't have the functionality you are looking for.
So if you don't get anywhere, you can ask us for the customization.
https://magecomp.com/magento-2-auto-customer-group-switching.html
200+ Premium Magento 2 Extensions Need help? Hire Magento Developer
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to assign a group to a user based on their email domain in Magento 2.3
Thanks. I'll let you know.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to assign a group to a user based on their email domain in Magento 2.3
You can create a before plugin for "CreateAccount()".
I have also somewhat same requirement and in that before creating account i was checking the registered information and assigned the group accordingly.
You can take help from the below code.
class CreateAccount { protected $request; public function __construct( RequestInterface $request, Data $helper ) { $this->request = $request; $this->helper = $helper; } public function beforeCreateAccount( AccountManagement $subject, CustomerInterface $customer, $password = null, $redirectUrl = '' ) { $storeOwner = $this->request->getParam('store_owner'); $customerGroupId = $this->helper->getGroupId(); if (isset($storeOwner) && $storeOwner!='') { $customer->setGroupId($customerGroupId); } return [$customer, $password, $redirectUrl]; } }
In this function you can check the email and accordingly assign the group there.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to assign a group to a user based on their email domain in Magento 2.3
You can do this using the Magento event observer as well.
1. Create events.xml file at the following location.
app\code\Vendor\Extension\etc\events.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="customer_register_success"> <observer name="vendor_extension_customer_register_success" instance="Vendor\Extension\Observer\Customer\RegisterSuccessObserver" /> </event> </config>
2. Now create the Observer File RegisterSuccessObserver.php at the following location.
app\code\Vendor\Extension\Observer\Customer\RegisterSuccessObserver.php
<?php namespace Vendor\Extension\Observer\Customer; use Magento\Framework\Event\ObserverInterface; class RegisterSuccessObserver implements ObserverInterface { public function __construct(.......) { } public function execute(\Magento\Framework\Event\Observer $observer) { $customer = $observer->getEvent()->getCustomer(); // DO YOUR LOGIC HERE // GET CUSTOMER EMAIL and do your logic } }
200+ professional extensions for M1 & M2 with free lifetime updates!
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to assign a group to a user based on their email domain in Magento 2.3
So you think that my logic could be like this?
$DiscountGroup=5; $GuestGroupId=2; list($user, $domain) = explode('@',$customer->getEmail()); if ($domain == 'mydomain.com'): /* set Customer group as DiscountedGroup*/ $customer->setData('group_id',$DiscountGroup); else: $customer->setData('group_id',$GuestGroupId); endif;
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to assign a group to a user based on their email domain in Magento 2.3
Yes, you need to change group based on your conditions and save the customer.
200+ professional extensions for M1 & M2 with free lifetime updates!
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to assign a group to a user based on their email domain in Magento 2.3
Hi Dhiren,
could you explain how to save user?
I integrated all your script, but doesn't work.
Here my complete RegisterSuccessObserver.php:
<?php namespace Sieu\Extension\Observer\Customer; use Magento\Framework\Event\ObserverInterface; class RegisterSuccessObserver implements ObserverInterface { public function __construct() { } public function execute(\Magento\Framework\Event\Observer $observer) { $customer = $observer->getEvent()->getCustomer(); $DiscountGroup=4; $GuestGroupId=1; list($user, $domain) = explode('@',$customer->getEmail()); if ($domain == 'mydomain.com'): /* set Customer group as DiscountedGroup*/ $customer->setData('group_id',$DiscountGroup); else: $customer->setData('group_id',$GuestGroupId); endif; } }