2

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

  • 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 Jan 26 '18 at 15:17
  • 1
    Refer this answer: magento.stackexchange.com/a/190842/35758 – Prince Patel Jan 26 '18 at 18:34
3

You can use toHtml() method

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

You can replace if condition with some valid condition

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

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.