cancel
Showing results for 
Search instead for 
Did you mean: 

Redirected automatically to a store view based on a user group - M2

Redirected automatically to a store view based on a user group - M2

I have created two store views, one for my normal customers (group) and one for my wholesale customers (group). I need that when a Wholesale customer login will be redirected automatically to the view created for their user group. But for normal customers and unregistered customers can use the normal store view. How can I do this? Can someone help me solve this please?

Thanks!

11 REPLIES 11

Re: Redirected automatically to a store view based on a user group - M2

Hi @ManuelSissl 

 

I would say for this you will require to create a custom module with plugin !

 

Where in you will require to create di.xml file.

 

<config>
    <type name="\Magento\Customer\Controller\Account\LoginPost">
        <plugin name="yourplugin_customerlogin_loginpostplugin" type="\VendorName\Customerlogin\Plugin\LoginPostPlugin" sortOrder="1" />
    </type>
</config>

app/code/VendorName/Customerlogin/Plugin/LoginPostPlugin.php

 

<?php
namespace VendorName\Customerlogin\Plugin;
class LoginPostPlugin
{
    public function afterExecute(
        \Magento\Customer\Controller\Account\LoginPost $subject,
        $result)
    {
        //-- check group is retail customer or not
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerSession = $objectManager->create('Magento\Customer\Model\Session');
        if ($customerSession->isLoggedIn()){
            $groupId = $customerSession->getCustomerGroupId();
            if ($groupId == 3){
                $result->setPath('your store view URL');
            }
        }
        return $result;
    }
}

Now in between set Path you can get your store view path or URL and add over here !

 

Then run the necessary commands to install the module and it will works.

 

Hope it helps !

if issue solved,Click Kudos & Accept as Solution

Re: Redirected automatically to a store view based on a user group - M2

Thank you very much for your response. This will be the first plugin that I will create and I have some questions:

 

   - Where do I have to create the di.xml file for this plugin?

   - The LoginPostPlugin.php file I have to create where you told me (adapting the names to the names of my folders), and I do not have to do anything else?

 

Can you help me solve these doubts please?

Re: Redirected automatically to a store view based on a user group - M2

Hi @ManuelSissl 

 

RE : Where do I have to create the di.xml file for this plugin?

-  You need to create Di.xml file in this location - app/code/{namespace}/{module}/etc/di.xml.

 

RE : The LoginPostPlugin.php file I have to create where you told me (adapting the names to the names of my folders), and I do not have to do anything else?

 

- Yes you need use your names for the folders and no then you don't need do anything else - you will require to load your store view as mentioned on the above post.

 

I would say refer below links to know more details for plugin creations : 

 

https://www.mageplaza.com/magento-2-module-development/magento-2-plugin-interceptor.html

 

https://www.emiprotechnologies.com/technical_notes/magento-technical-notes-60/post/how-to-create-and...

 

 

 

Hope it helps !

 

if issue solved,Click Kudos & Accept as Solution

Re: Redirected automatically to a store view based on a user group - M2

Hi @Manthan Dave,

 

I created these files in these path:

 

/app/code/MyPlugin/CustomerLogin/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="\Magento\Customer\Controller\Account\LoginPost">
        <plugin name="myPluginCustomerloginLoginpostplugin" type="\MyPlugin\CustomerLogin\Plugin\LoginPostPlugin" sortOrder="1" />
    </type>
</config>

 

/app/code/MyPlugin/CustomerLogin/Plugin/LoginPostPlugin.php

<?php

namespace MyPlugin\CustomerLogin\Plugin;

class LoginPostPlugin
{
    public function afterExecute(
        \Magento\Customer\Controller\Account\LoginPost $subject,
        $result)
    {
        //-- check group is retail customer or not
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerSession = $objectManager->create('Magento\Customer\Model\Session');
        if ($customerSession->isLoggedIn()){
            $groupId = $customerSession->getCustomerGroupId();
            //if ($groupId == 3){
                //$result->setPath('your store view URL');
                echo '<script>alert ($groupId);</script>';
            //}
        }
        return $result;
    }
}

I used these commands:

php bin/magento cache:flush
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f

 

But it does not work the alert when I log in, could you help me understand what I did wrong or what I'm missing please?

Re: Redirected automatically to a store view based on a user group - M2

Hello @ManuelSissl 

 

have you created other custom module required files ? like module.xml file then registration.php file ?

 

Also can you please check your custom module is installed or not in your database ? setup_module table !

 

using only this two files your module will not create or installed this two file is for your custom requirement logic but rest of the required files you need to create.

 

Hope it helps !

 

if issue solved,Click Kudos & Accept as Solution

Re: Redirected automatically to a store view based on a user group - M2

No, I did not know that I had to create more files. So, I have to create only those two files (module.xml and registration.php) or some more?

Re: Redirected automatically to a store view based on a user group - M2

Hi @ManuelSissl 

 

Yes looking at your current code you only require two files.

 

if you haven't created any basic custom module yet - then i would suggest you to create a first basic custom module in magento 2 

 

Refer this link for the same - https://www.mageplaza.com/magento-2-module-development/

 

then jump to the module which you are trying to create so you will get idea which file is necessary and required etc.

 

Hope it helps !

if issue solved,Click Kudos & Accept as Solution

Re: Redirected automatically to a store view based on a user group - M2

Hi @Manthan Dave 

 

In the code that you told me:

$result->setPath('your store view URL');

How do I know the URL of my view?

Re: Redirected automatically to a store view based on a user group - M2

Hi @ManuelSissl 

 

For that you will require to get the Store Information and bind the same with base URL.

 

to get the store view code or store related information i would say refer this link for the same - https://www.mageplaza.com/devdocs/how-get-store-information-magento-2.html

 

Hope it helps !

if issue solved,Click Kudos & Accept as Solution