4

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

3
  • Not sure if this applies in your case but private content should be handled client-side, see the official docs for more info. Since private content is specific to individual users, it’s reasonable to handle it on the client (i.e., web browser).
    – Ben Crook
    Commented Jan 26, 2018 at 15:17
  • 1
    Refer this answer: magento.stackexchange.com/a/190842/35758 Commented Jan 26, 2018 at 18:34
  • this doesn't work in Magento 2.4, Any help?
    – Carlyle
    Commented Jul 15 at 2:20

1 Answer 1

7

You can use toHtml() method

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

You can replace if condition with some valid condition

2
  • thanks, I think return null is better though?
    – Holly
    Commented Jan 28, 2018 at 17:06
  • this doesn't work in Magento 2.3, Any help? Commented Mar 22, 2019 at 21:57

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.