3

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

1 Answer 1

4

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!

8
  • this is GREAT. Just curious, if I wanted to show these on more than one customerGroup ID, would I do them comma separated? You're the man!
    – Nick Piro
    Commented Aug 17, 2018 at 8:25
  • No need to add comma separated. Check my Updated answer. Commented Aug 17, 2018 at 8:26
  • Thanks! So, if I wanted these same links on two groups I would still create a different block class?
    – Nick Piro
    Commented Aug 17, 2018 at 8:30
  • No if you want to show the same links for 2 groups then just put the group ids to the array according to my updated section. No need to create different block class. Commented Aug 17, 2018 at 8:31
  • I'm going to post more questions because I currently do not have enough reputation to mark it as the best answer! Will do once I have enough rep. I need 4 more.
    – Nick Piro
    Commented Aug 17, 2018 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.