0

I want to remove customer_account_navigation-reports block from customer account on the frontend for specific customer group i.e. Distributor

How can I do this?

Ankit8
  • 177
  • 1
  • 2
  • 10
  • This seems similar requirement - https://magento.stackexchange.com/questions/211137/only-show-link-in-account-navigation-to-specific-customer-groups – Gagan Aug 08 '18 at 10:30
  • Thanks @Evince. can you please tell me how to implement it? – Ankit8 Aug 08 '18 at 10:41

1 Answers1

1

I have found the soution.

  1. Create a custom module Abweb_Customer.

  2. Create registration.php in Abweb/Customer and module.xml in Abweb/Customer/etc folder.

  3. Create di.xml in Abweb/Customer/etc folder with following code.

    <?xml version="1.0" ?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Customer\Controller\Account\Index" type="Abweb\Customer\Controller\Account\Index" />
    </config>
    
  4. Create Index.phpin Abweb\Customer\Controller\Account folder with following code.

    <?php
    namespace Abweb\Customer\Controller\Account;
    use Magento\Framework\App\Action\Context;
    use Magento\Framework\View\Result\PageFactory;
    
    class Index extends \Magento\Customer\Controller\Account\Index
    {
    /**
    * @var PageFactory
    */
    protected $resultPageFactory;
    protected $customersession;
    
    public function __construct(
    Context $context,
    \Magento\Customer\Model\Session $customersession,       
    PageFactory $resultPageFactory
    ) {
    parent::__construct($context,$resultPageFactory);
    $this->customersession=$customersession;
    $this->resultPageFactory = $resultPageFactory;
    }
    public function execute()
    {
    if($this->customersession->isLoggedIn())
    {
        $cgid = $this->customersession->getCustomer()->getGroupId();  // get Customer Group Id
        $resultPage = $this->resultPageFactory->create();
        $layout = $resultPage->getLayout();
    
        if($cgid!="10") // Distributor Customer Group
        {
            $block = $layout->getBlock('customer_account_navigation-reports'); // block name
            $layout->unsetElement('customer_account_navigation-reports'); //remove block
            return $resultPage;
        } else {
            return $resultPage;
        }
    
    }
    }
    }
    
  5. Done. Now all you have to do is change the customer-group code and block name that you want to remove.

Ankit8
  • 177
  • 1
  • 2
  • 10