1

How can i restrict customer dashboard links for specific customer group in magento2 frontend

enter image description here

2
  • Customer desh board link means customer account all pages?
    – Amit Bera
    Oct 13, 2017 at 7:16
  • Yes, i want to hide links in customer account all pages Oct 13, 2017 at 9:34

1 Answer 1

2

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;
    }
}
4
  • i have added screenshot above, please check Oct 13, 2017 at 11:23
  • You need add another event controller_action_predispatch_{fullActionname}
    – Amit Bera
    Oct 13, 2017 at 11:25
  • i want to hide the frontend customer dashboard links for particular users Oct 13, 2017 at 11:29
  • Yes, possible..,If you customer object then you can do
    – Amit Bera
    Oct 13, 2017 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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