Question

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.

Was it helpful?

Solution

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>
                </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;

class Link extends \Magento\Framework\View\Element\Html\Link\Current
{
    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;
    }
}

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top