How can i restrict customer dashboard links for specific customer group in magento2 frontend
1 Answer
In this case, you can use event
controller_action_predispatch_customer
On this event fire an observer:* which will redirect a customer to **home whenever they are loggin and try access customer page
<?php
namespace {Vendor}\{Modulename}\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Model\Session;
class StopAccountAccess implements ObserverInterface
{
protected $_helper;
protected $_actionFlag;
protected $messageManager;
protected $redirect;
protected $session;
public function __construct(
\Magento\Captcha\Helper\Data $helper,
\Magento\Framework\App\ActionFlag $actionFlag,
\Magento\Framework\Message\ManagerInterface $messageManager,
\Magento\Framework\App\Response\RedirectInterface $redirect,
Session $customerSession
) {
$this->_helper = $helper;
$this->_actionFlag = $actionFlag;
$this->messageManager = $messageManager;
$this->redirect = $redirect;
$this->session = $customerSession;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
/** @var \Magento\Framework\App\Action\Action $controller */
$controller = $observer->getControllerAction();
$actionname = $controller->getRequest()->getActionName();
$controllername= $controller->getRequest()->getControllerName();
$disableredirectionAction = array('loginPost','loginPost')
array(
'create',
'login',
'forgotpassword',
'forgotpasswordpost',
'resetpassword',
'resetpasswordpost',
'confirm',
'confirmation',
'loginPost',
'loginpost',
'logoutsuccess',
'logout'
);
$pattern = '/^(' . implode('|', $disableredirectionAction) . ')/i';
if ($this->session->isLoggedIn() && !preg_match($pattern, $action) ) {
$this->messageManager->addError(__('You cannot access account pages'));
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$this->redirect->redirect($controller->getResponse(), '');
}
return $this;
}
}
-
-
You need add another event controller_action_predispatch_{fullActionname}– Amit Bera ♦Commented Oct 13, 2017 at 11:25
-
i want to hide the frontend customer dashboard links for particular users Commented Oct 13, 2017 at 11:29
-
Yes, possible..,If you customer object then you can do– Amit Bera ♦Commented Oct 13, 2017 at 11:30