Magento 2 – Show Link in Account Navigation for Specific Customer Groups

blockslayoutmagento2

I have a link which I've added to the Account Navigation in Magento2. It's called VIP Customer Info.

enter image description here

I would like to be able to hide or disable this link depending on weather the customer is logged in and belongs to the VIP customer group.

Any suggestions on how this could be possible? I have tried the method below but it didn't work 🙁

Maybe I need to manage it from template file?


I thought I could achieve this by using a method from custom block in view/frontend/layout/customer_account.xml

<referenceBlock name="customer_account_navigation">
    <block class="Holy\Vip\Block\Html\Link\Vip" name="customer-account-navigation-vip-info-link">
        <action method="isVipCustomer">
            <arguments>
                <argument name="label" xsi:type="string" translate="true">VIP Customer Info</argument>
                <argument name="path" xsi:type="string">vip/customer/info</argument>
            </arguments>
        </action>
    </block>
</referenceBlock>

But this didn't work, it just stopped the link from being rendered at all.

This is my block class:

<?php

namespace Holy\Vip\Block\Html\Link;


class Vip extends \Magento\Framework\View\Element\Html\Link\Current
{
    public function isVipCustomer()
    {
        return true;
    }
}

Unfortunately, this does not work. It just shows a empty space with a link to the homepage no matter what the return value:

enter image description here

Best Answer

You can use toHtml() method

public function toHtml(){
      if(true){
           return parent::toHtml();
      }
      return null;
}

You can replace if condition with some valid condition

Related Topic