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. I'm creating a plugin to do this. 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 == 2){
$result->setPath('?___store=Wholesale&___from_store=Wholesale');
}
}
return $result;
}
}
And the view changes to the selected view (wholesale), but when I open a product or a category, the view changes to the predefined view, the change does not become effective. Any other idea of being able to make the fixed exchange?
This is the line that I do not know what to put in order that the scope of view remains:
$result->setPath('?___store=Wholesale&___from_store=Wholesale');
Any ideas to solve this problem? I need to make a change of store view and keep the change.