4

I am trying to add the custom link on customer dashboard in magento 2.

I have added

<?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">
            <block class="Magento\Framework\View\Element\Html\Link\Current" name="demo-link">
                <arguments>
                    <argument name="path" xsi:type="string">module/controller/action</argument>
                    <argument name="label" xsi:type="string">Demo Link</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

on my module.Actually i have followed http://webkul.com/blog/magento2-add-link-in-my-account-navigation-panel/ . I changed the path to my module controller.Till here it is working good.

But there are two things that i couldn't achieve:

one is When i click on that tab it is redirecting to me to the entirely new page.The page that was defined on my layout's xml file. so I added the code of updating the handle of customer account. see my xml file.

<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="customer_account"/>
    <body>
        <referenceContainer name="content">         
            <block class="Custom\Addprofile\Block\Index\Index" name="index.index" template="Custom_Addprofile::index/index.phtml"/>
        </referenceContainer>
    </body>
</page>

This is still not working as expected. The template should load aside of the left navigation links on the account dashboard.

Am I missing some thing here?

  • Also Another thing i was trying achieve is i want to display that custom tab to particular customer group only. Is there any easy way of achieving that?
1

change layout="1column" to layout="2columns-left" in your custom layout file.

it should be

<page layout="2columns-left" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="customer_account"/>
    <body>
        <referenceContainer name="content">         
            <block class="Custom\Addprofile\Block\Index\Index" name="index.index" template="Custom_Addprofile::index/index.phtml"/>
        </referenceContainer>
    </body>
</page>

Look for Reference: app/code/Magento/Customer/view/frontend/layout/customer_account.xml

update

protected $_customersession;
public function __construct(\Magento\Customer\Model\Session $customersession)
{

        $this->_customersession=$customersession
}

public function yourMethod()
{
    if($this->_customersession->isLoggedIn())
    {
        echo   $customerSession->getCustomer()->getGroupId();  // get Customer Group Id
    }
}

you can add the above logic in block or helper and make decision to show or not by overriding corresponding update_handle phtml.

  • Thanks bibal it worked. have you got any idea of showing that tab to customer of particular customer group only? – aton1004 Oct 21 '16 at 11:02
  • see my updated answer. if you can't get, can you please raise new question with your stuff I will elaborate. – Bilal Usean Oct 21 '16 at 11:20
  • sure bilal. here is my question magento.stackexchange.com/questions/142032/… – aton1004 Oct 21 '16 at 11:32

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.