0

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

enter image description here

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

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

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.