1

I'm trying to hide account navigation links based on customer groups. In order to hide the links, I have installed a module https://github.com/KishanSavaliya/magehelper-magento2-hide-account-sidebar-tabs by creating different block files for the corresponding links but unfortunately, the links are not hiding.

XML file:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="customer_account_navigation">
            <block class="MageHelper\HideSidebarTabs\Block\Account\WishlistLink" ifconfig="wishlist/general/active" name="customer-account-navigation-wish-list-link">
                <arguments>
                    <argument name="path" xsi:type="string">wishlist</argument>
                    <argument name="label" xsi:type="string" translate="true">My Wish List</argument>
                    <argument name="sortOrder" xsi:type="number">210</argument>
                </arguments>
            </block>
            <referenceBlock name="customer-account-navigation-orders-link" remove="true" />
                <block class="MageHelper\HideSidebarTabs\Block\Account\OrderLink" name="customer-account-navigation-orders-link-new">
                    <arguments>
                        <argument name="path" xsi:type="string">sales/order/history</argument>
                        <argument name="label" xsi:type="string" translate="true">My Orders</argument>
                        <argument name="sortOrder" xsi:type="number">230</argument>
                    </arguments>
                </block>
                <block class="MageHelper\HideSidebarTabs\Block\Account1\AccountLink" name="customer-account-navigation-account-link">
                    <arguments>
                        <argument name="label" xsi:type="string" translate="true">My Account</argument>
                        <argument name="path" xsi:type="string">customer/account</argument>
                        <argument name="sortOrder" xsi:type="number">250</argument>
                    </arguments>
                </block>
        </referenceBlock>
    </body>
</page>

Block file:

<?php

namespace MageHelper\HideSidebarTabs\Block\Account;

class AccountLink extends \Magento\Framework\View\Element\Html\Link\Current
{
    protected $_customerSession;
    protected $customerGroup;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\DefaultPathInterface $defaultPath,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Customer\Model\ResourceModel\Group\Collection $customerGroup,
        array $data = []
     ) {
         $this->_customerSession = $customerSession;
         $this->customerGroup = $customerGroup;
         parent::__construct($context, $defaultPath, $data);
     }

    protected function _toHtml()
    {
        $responseHtml = null; // need to return at-least null
        if ($this->_customerSession->isLoggedIn()) {
            $customerGroup = $this->_customerSession->getCustomer()->getGroupId(); // Current customer groupID
            //Your Logic Here
            if ($customerGroup == '2') {
                $responseHtml = parent::_toHtml(); // Return link html
            }
        }

        return $responseHtml;
    }
}

If you see the logic in the block, I have written a condition to show the link only if the customer group belongs to the group-id 2, if not it should be hidden. In my case, the link displays even in group-ID 1.

I'm not sure what am I missing. I'm trying this to work from a couple of days but not a success.

Could anyone please help me to resolve this issue?

1 Answer 1

2

You can replace your XML file content with below content.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="customer_account_navigation">
            <block class="MageHelper\HideSidebarTabs\Block\Account\WishlistLink" ifconfig="wishlist/general/active" name="customer-account-navigation-wish-list-link">
                <arguments>
                    <argument name="path" xsi:type="string">wishlist</argument>
                    <argument name="label" xsi:type="string" translate="true">My Wish List</argument>
                    <argument name="sortOrder" xsi:type="number">210</argument>
                </arguments>
            </block>
            <block class="MageHelper\HideSidebarTabs\Block\Account\OrderLink" name="customer-account-navigation-orders-link">
                <arguments>
                    <argument name="path" xsi:type="string">sales/order/history</argument>
                    <argument name="label" xsi:type="string" translate="true">My Orders</argument>
                    <argument name="sortOrder" xsi:type="number">230</argument>
                </arguments>
            </block>
            <referenceBlock name="customer-account-navigation-account-link" remove="true" />
            <block class="MageHelper\HideSidebarTabs\Block\Account\AccountLink" name="customer-account-navigation-account-link-custom">
                <arguments>
                    <argument name="label" xsi:type="string" translate="true">My Account</argument>
                    <argument name="path" xsi:type="string">customer/account</argument>
                    <argument name="sortOrder" xsi:type="number">250</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

After changing this please run below commands in CLI once

php bin/magento cache:clean
php bin/magento cache:flush

Hope this will help you!

1
  • 1
    Thank you so much. You saved me!!
    – MagentoDev
    Commented Nov 11, 2019 at 6:34

Your Answer

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

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