Question

I am attempting to add links to certain customer groups that will show on Customer Dashboard. I'm having trouble figuring this out! Help!

Was it helpful?

Solution

Create a module and add create customer_account.xml like below:

app/code/Vendor/Module/view/frontend/layout/customer_account.xml

<?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\Customergroup\Link" name="customer-account-navigation-new-link1" after="-">
                <arguments>
                    <argument name="label" xsi:type="string">Link1</argument>
                    <argument name="path" xsi:type="string">customer/link1/index</argument>
                </arguments>
            </block>
            <block class="Vendor\Module\Block\Customergroup\Link" name="customer-account-navigation-new-link2" after="customer-account-navigation-new-link1">
                <arguments>
                    <argument name="label" xsi:type="string">Link2</argument>
                    <argument name="path" xsi:type="string">customer/link2/index</argument>
                </arguments>
            </block>
            <block class="Vendor\Module\Block\Customergroup\Link" name="customer-account-navigation-new-link3" after="customer-account-navigation-new-link2">
                <arguments>
                    <argument name="label" xsi:type="string">Link3</argument>
                    <argument name="path" xsi:type="string">customer/link3/index</argument>
                </arguments>
            </block>
            <block class="Vendor\Module\Block\Customergroup\Link" name="customer-account-navigation-new-link4" after="customer-account-navigation-new-link3">
                <arguments>
                    <argument name="label" xsi:type="string">Link4</argument>
                    <argument name="path" xsi:type="string">customer/link4/index</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

I have added 4 links here, you can add or remove more.

Now create one Block class with same name mentioned in your xml to the below path:

app/code/Vendor/Module/Block/Customergroup/Link.php

Content for Block class will be:

<?php
namespace Vendor\Module\Block\Customergroup;

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;
        if($this->_customerSession->isLoggedIn()) {

            $customerGroup = $this->_customerSession->getCustomer()->getGroupId();

            if($customerGroup == '2') {
                $responseHtml = parent::_toHtml();
            } 
        }
        return $responseHtml;
    }
}

Here I put logic that if customer group id is 2 then it will show the links I have added to the xml file. You can change the logic according to your requirement.

Also if you want to add different links for different customer groups then you can create different block classes for different customer groups and call accordingly in your customer_account.xml

UPDATED SECTION: If you want to check more customer groups then you can use below code:

$customerGroups = array(2,3,4);

if(in_array($customerGroup,$customerGroups)) {
    $responseHtml = parent::_toHtml();
} 

Hope this helps!

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