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

Nikunj Vadariya
  • 4,037
  • 1
  • 20
  • 30
Navin Bista
  • 1,093
  • 4
  • 14
  • 30

1 Answers1

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
        }
    }
}
Bilal Usean
  • 9,977
  • 14
  • 75
  • 122
  • 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 – Navin Bista Oct 24 '16 at 04: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 05: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 – Navin Bista Oct 24 '16 at 05: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 05: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. – Navin Bista Nov 17 '16 at 12:39
  • it is happen suddenly or from the beginning? refer this http://magento.stackexchange.com/a/87632/36463 – Bilal Usean Nov 17 '16 at 12:43
  • also refer this for how to get layout object http://magento.stackexchange.com/a/88847/36463 – Bilal Usean Nov 17 '16 at 12:48