3

I want 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

I'm trying to make it work, but it's not working. I create events.xml in **app\code\Sieu\Extension\etc** with this code:

<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="Sieu\Extension\Observer\Customer\RegisterSuccessObserver" />
    </event>
</config>

next I created file RegisterSuccessObserver.php here. **app\code\Sieu\Extension\Observer\Customer** with this code:

<?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;
    }
    $customer->save();
}
0
1

You need to create registration.php file here in your custom module

app/code/Sieu/Extension/registration.php

Content for this file is..

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Sieu_Extension',
    __DIR__
);

Now you need to create module.xml file here in etc directory here

app/code/Sieu/Extension/etc/module.xml

Content for this file is..

<?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="Sieu_Extension" setup_version="1.0.0"/>
</config>

Now create events.xml file here in etc directory

app/code/Sieu/Extension/etc/events.xml

Content for this file is..

<?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="sieu_customer_account_create_post" instance="Sieu\Extension\Observer\Customer\RegisterSuccessObserver" />
    </event>
</config>

Now create one Observer file here

app/code/Sieu/Extension/Observer/Customer/RegisterSuccessObserver.php

Content for this file is..

<?php
namespace Sieu\Extension\Observer\Customer;

use Magento\Framework\Event\ObserverInterface;

class RegisterSuccessObserver implements ObserverInterface
{
    protected $customer;

    public function __construct(
        \Magento\Customer\Model\Customer $customer
    ) {
        $this->customer = $customer;
    }

    public function execute(\Magento\Framework\Event\Observer $observer){
        $customerEmail = $observer->getEvent()->getCustomer()->getEmail();
        $DiscountGroup=4;
        $GuestGroupId=1;
        $customerId = $observer->getEvent()->getCustomer()->getId();
        $customer = $this->customer->load($customerId);
        list($user, $domain) = explode('@',$customerEmail);

        if ($domain == 'soluzioni-internet.eu'):
            /* set Customer group as DiscountedGroup*/
            $customer->setGroupId($DiscountGroup);   
        else:
            $customer->setGroupId($GuestGroupId);   
        endif;
        $customer->save();

        return $observer;
    }  
}

Hope this will work for you!

2
  • 1
    Great Kishan, you solved my question. Thank you for your support. – sm77 Nov 28 '19 at 8:20
  • You're welcome :) – Kishan Savaliya Nov 28 '19 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.