3

Hi i have created account navigation link, and i have two customer groups in magento admin panel, and i only want to display that links to specific group rather than displaying all the time.

here is my xml code for displaying links.

    <?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="customer-account-navigation-new-product-link" after="-">
                <arguments>
                    <argument name="label" xsi:type="string">Some link</argument>
                    <argument name="path" xsi:type="string">customer/somelink/index</argument>
                </arguments>
            </block>
        </referenceBlock>
        <referenceBlock name="customer_account_navigation">
            <block class="Magento\Framework\View\Element\Html\Link\Current" name="customer-account-navigation-new-some-link" after="-">
                <arguments>
                    <argument name="label" xsi:type="string">new link here</argument>
                    <argument name="path" xsi:type="string">customer/somelinkhere/index</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

please suggest a way for this.

2
  • Let me know if you have any issue Aug 28, 2017 at 10:55
  • 1
    Works like a charm..!
    – Jay Kapoor
    Aug 28, 2017 at 11:14

1 Answer 1

10

Check this for more info: blog.mageprince.com

You need to create block for dynamic link

1) Define Your block class {vendor}\{Module}\Block\Customer\Link in navigation link

<?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="{vendor}\{Module}\Block\Customer\Link" name="customer-account-navigation-new-product-link" after="-">
                <arguments>
                    <argument name="label" xsi:type="string">Some link</argument>
                    <argument name="path" xsi:type="string">customer/somelink/index</argument>
                    <argument name="sortOrder" xsi:type="number">250</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

2) Now create Link.php at app/code/{Vendor}/{Module}/Block/Customer/Link.php

<?php

namespace Vendor\Module\Block\Customer;

use Magento\Customer\Block\Account\SortLinkInterface;
use Magento\Framework\View\Element\Html\Link\Current;

class Link extends Current implements SortLinkInterface
{
    protected $_customerSession;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\DefaultPathInterface $defaultPath,
        \Magento\Customer\Model\Session $customerSession,
        array $data = []
     ) {
         $this->_customerSession = $customerSession;
         parent::__construct($context, $defaultPath, $data);
     }

    protected function _toHtml()
    {    
        $responseHtml = null; //  need to return at-least null
        if($this->_customerSession->isLoggedIn()) {
            
            $customerGroup = $this->_customerSession->getCustomer()->getGroupId(); //Current customer groupID
            
            //Your Logic Here
            if($customerGroup == '1') {
                $responseHtml = parent::_toHtml(); //Return link html
            } 
        }
        return $responseHtml;
    }

    public function getSortOrder()
    {
        return $this->getData(self::SORT_ORDER);
    }
}

Here you make the dynamic link as per your logic. Here I check current customer groupId and return HTML of link if customer group = 1 otherwise return null.

This same way you can make your second link dynamic.

If you want only navigation link dynamic then use helper

1) Define helper class {vendor}\{Module}\Helper\Data in navigation link

<?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\Customer\Block\Account\SortLinkInterface" name="customer-account-navigation-new-product-link" after="-">
                <arguments>
                    <argument name="label" xsi:type="string">Some link</argument>
                    <argument name="path" xsi:type="helper" helper="{Vendor}\{Module}\Helper\Data::getLink"></argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

2) Now create Data.php at app/code/{Vendor}/{Module}/Helper/Data.php

<?php

namespace Vendor\Module\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    public function getLink()
    {
        //Do your logic and return link
        $link = "sales/order/mylink";

        return $link;
    }
}

Note :

In Magento 2.1.x remove var/generation and flush cache.

In Magento 2.2.x remove generated/code and generated/metadata and flush cache.

3
  • I have tried the same way as you suggested for displaying dynamic link but I'm not getting the output.
    – Siv
    Nov 5, 2019 at 12:38
  • Below is my XML code: <referenceBlock name="customer_account_navigation"> <block class="MageHelper\HideSidebarTabs\Block\Account\OrderLink" name="customer-account-navigation-orders-link"> <arguments> <argument name="path" xsi:type="string">sales/order/history</argument> <argument name="label" xsi:type="string" translate="true">My Orders</argument> <argument name="sortOrder" xsi:type="number">230</argument> </arguments> </block> </referenceBlock>
    – Siv
    Nov 5, 2019 at 12:38
  • Could you please help me out this?
    – Siv
    Nov 5, 2019 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.