1

I have added two links to customer dashboard "Proposals" and "Documentaion". I have also added a customer group "xyz". I want to only show the mentioned group. Similarly, I want to hide these two links for other groups.

2

I would suggest below reference code. There are two ways to do this

  1. Create a new layout that will work filter based on the customer group. Forex customer_group_general.xml. In this file, you can hide/remove the left navigation link which you have added using the XML file. Please follow the steps to create a new layout:
  • Create a new module
  • Add di.xml file in the etc/frontend folder
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\View\Result\Page">
        <plugin name="[pluginName]" type="[Vendor]\[ModuleName]\Plugin\[ClassName]" sortOrder="10"/>
    </type>
</config>
  • Create plugin php file in your module in app/code/[codePool]/[Vendor]/[ModuleName]/Plugin/[ClassName].php
<?php

namespace [Vendor]\[ModuleName]\Plugin;

use Magento\Customer\Model\Session;
use Magento\Framework\View\Result\Page;
use Magento\Store\Model\GroupFactory;

class [ClassName]
{
    protected $customerSession;

    protected $groupFactory;

    public function __construct(
        Session $customerSession,
        GroupFactory $groupFactory
    ) {
        $this->customerSession = $customerSession;
        $this->groupFactory = $groupFactory;
    }

    public function afterAddPageLayoutHandles(Page $subject)
    {
        $group = $this->groupFactory->create()
            ->load($this->customerSession->getCustomerGroupId());
        if ($group->getId() >= 0) {
            $handle = sprintf("customer_group_%s", strtolower(preg_replace("/[^A-Za-z0-9]/", '_', $group->getName())));
            $subject->addHandle($handle);
        }

        return $subject;
    }
}

Now you can create a customer_group_default.xml and update the link based on your requirement. Please review the reference link for more details.

https://www.smartiehastheanswer.co.uk/magento2/adding-custom-dynamic-layout-handles-globally-in-magento2.html

  1. Create a custom link that will come from the phtml file. In phtml file, you can add your conditions and it will update in the left navigation.
<block class="Custom\Sales\Block\Link" name="downloads-new" template="Custom_Sales::link.phtml">
    <arguments>
        <argument name="sortOrder" xsi:type="number">210</argument>
    </arguments>
</block>

link.phtml

<?php if($block->hidelink()):?>
<li class="custom link" > 
<a href="<?= /* @noEscape */
$block->getUrl('test/index/index')?>" class="custom-link">
   <?php echo __("My Link")?>
</a> 
</li>
<?php }?>

Link.php

<?php
namespace Custom\Sales\Block;

use Magento\Customer\Block\Account\SortLinkInterface;

/**
 * Class Link
 *
 * @api
 * @SuppressWarnings(PHPMD.DepthOfInheritance)
 * @since 100.0.2
 */
class Link extends \Magento\Framework\View\Element\Html\Link implements SortLinkInterface
{
    /**
     * {@inheritdoc}
     * @since 101.0.0
     */
    public function getSortOrder()
    {
        return $this->getData(self::SORT_ORDER);
    }

    public function hideLink() {
        //Add condition
        return false;
    }

}

In block, you can load current logged in customer and sent true or false flag in the link.phtml

I hope this will help you.

Thanks. If you need any help, please let me know.

6
  • Let me try this
    – Tanmay
    Oct 12 '20 at 18:42
  • This will only hide custom links what I want to achive is that when the particular customer belongs to group it will display only custom links other wise it will hide it.
    – Tanmay
    Oct 12 '20 at 18:49
  • It means you also want to hide the Magento default customer links as well when particular customer login from a specific group, is it?
    – Nits
    Oct 13 '20 at 4:35
  • Yes I want to hide default links as well
    – Tanmay
    Oct 13 '20 at 7:55
  • then you have to create a module as I have mentioned in the above comment and create a catalog_group_groupname.xml file and hide links from the XML file. Else you can add css (display:none) for particular links based on the current customer login and call that phtml file on all the customer_account pages(I would not prefer this solution).
    – Nits
    Oct 13 '20 at 9:13

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.