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.

Prince Patel
  • 22,708
  • 10
  • 97
  • 119
Jay Kapoor
  • 603
  • 2
  • 12
  • 30

1 Answers1

11

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-&gt;_customerSession = $customerSession;
     parent::__construct($context, $defaultPath, $data);
 }

protected function _toHtml()
{    
    $responseHtml = null; //  need to return at-least null
    if($this-&gt;_customerSession-&gt;isLoggedIn()) {

        $customerGroup = $this-&gt;_customerSession-&gt;getCustomer()-&gt;getGroupId(); //Current customer groupID

        //Your Logic Here
        if($customerGroup == '1') {
            $responseHtml = parent::_toHtml(); //Return link html
        } 
    }
    return $responseHtml;
}

public function getSortOrder()
{
    return $this-&gt;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.

Prince Patel
  • 22,708
  • 10
  • 97
  • 119