1

I have added the custom tab on the customer dashboard using the code:

<?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">
            <!-- <update handle="customer_account_navigation"/> -->

            <block class="Magento\Framework\View\Element\Html\Link\Current" name="demo-link">
                <arguments>
                    <argument name="path" xsi:type="string">addprofile/index</argument>
                    <argument name="label" xsi:type="string">Demo Link</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

Now what I want is I want to show this tab to only a particular customer group. As the link was added through the XML file and when I checked the navigation.phtml it is rendering all the links using

<?php echo $block->getChildHtml();?>

So I wasn't able to add some condition there.

can anyone help?

Thanks

1

I hope you get an idea from this, I have not tested. I have tried like remove that block programatically when customer group id is not match with our expectation, .

protected $_customersession;
public function __construct(\Magento\Customer\Model\Session $customersession)
{
  $this->_customersession=$customersession
}

public function yourMethod()
{
    if($this->_customersession->isLoggedIn())
    {
        $cgid=$customerSession->getCustomer()->getGroupId();  // get Customer Group Id
        if($cgid!="yourExpectedId")
        {
            $layout = $this->getLayout();
            $block = $layout->getBlock('demo-link'); // block name
            $layout->unsetElement('demo-link'); //remove block
        }
    }
}
  • Hi Bilal thanks for the answer but i am confused on implementing it.where should i add this code as the tab is added from layout. Also i didn't find the template for customer_account – aton1004 Oct 24 '16 at 4:58
  • in layout you can't find customer group for add tab, as I know the only possibilities to check from controller so you should need to apply this logic in your custom controller execute method. – Bilal Usean Oct 24 '16 at 5:04
  • How is that going to work coz i want to hide this tab for particular customer group but controller of my module is only executed when someone click this link – aton1004 Oct 24 '16 at 5:10
  • oh you right! in your case you need to override and try this logic in app/code/Magento/Customer/Controller/Account/Index.php – Bilal Usean Oct 24 '16 at 5:12
  • Hi bilal i tried it above code. The line with $layout = $this->getLayout(); is giving the error of unable to handle the request. can you suggest what could possibly go wrong. – aton1004 Nov 17 '16 at 12:39

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.