Create Link Dynamically By Customer Group in Customer Account Navigation - Magento 2

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












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







share|improve this question


























    up vote
    3
    down vote

    favorite












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







    share|improve this question






















      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











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







      share|improve this 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!









      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 17 at 5:00









      Nick Piro

      255




      255




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          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="VendorModuleBlockCustomergroupLink" 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="VendorModuleBlockCustomergroupLink" 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="VendorModuleBlockCustomergroupLink" 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="VendorModuleBlockCustomergroupLink" 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 VendorModuleBlockCustomergroup;

          class Link extends MagentoFrameworkViewElementHtmlLinkCurrent

          protected $_customerSession;

          public function __construct(
          MagentoFrameworkViewElementTemplateContext $context,
          MagentoFrameworkAppDefaultPathInterface $defaultPath,
          MagentoCustomerModelSession $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!






          share|improve this answer






















          • 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
            Aug 17 at 8:25










          • No need to add comma separated. Check my Updated answer.
            – Sukumar Gorai
            Aug 17 at 8:26










          • Thanks! So, if I wanted these same links on two groups I would still create a different block class?
            – Nick Piro
            Aug 17 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.
            – Sukumar Gorai
            Aug 17 at 8:31










          • If its working for you then click the tick button near my answer so that it will help someone in future.
            – Sukumar Gorai
            Aug 17 at 8:51










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "479"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f238665%2fcreate-link-dynamically-by-customer-group-in-customer-account-navigation-magen%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          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="VendorModuleBlockCustomergroupLink" 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="VendorModuleBlockCustomergroupLink" 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="VendorModuleBlockCustomergroupLink" 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="VendorModuleBlockCustomergroupLink" 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 VendorModuleBlockCustomergroup;

          class Link extends MagentoFrameworkViewElementHtmlLinkCurrent

          protected $_customerSession;

          public function __construct(
          MagentoFrameworkViewElementTemplateContext $context,
          MagentoFrameworkAppDefaultPathInterface $defaultPath,
          MagentoCustomerModelSession $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!






          share|improve this answer






















          • 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
            Aug 17 at 8:25










          • No need to add comma separated. Check my Updated answer.
            – Sukumar Gorai
            Aug 17 at 8:26










          • Thanks! So, if I wanted these same links on two groups I would still create a different block class?
            – Nick Piro
            Aug 17 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.
            – Sukumar Gorai
            Aug 17 at 8:31










          • If its working for you then click the tick button near my answer so that it will help someone in future.
            – Sukumar Gorai
            Aug 17 at 8:51














          up vote
          2
          down vote



          accepted










          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="VendorModuleBlockCustomergroupLink" 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="VendorModuleBlockCustomergroupLink" 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="VendorModuleBlockCustomergroupLink" 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="VendorModuleBlockCustomergroupLink" 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 VendorModuleBlockCustomergroup;

          class Link extends MagentoFrameworkViewElementHtmlLinkCurrent

          protected $_customerSession;

          public function __construct(
          MagentoFrameworkViewElementTemplateContext $context,
          MagentoFrameworkAppDefaultPathInterface $defaultPath,
          MagentoCustomerModelSession $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!






          share|improve this answer






















          • 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
            Aug 17 at 8:25










          • No need to add comma separated. Check my Updated answer.
            – Sukumar Gorai
            Aug 17 at 8:26










          • Thanks! So, if I wanted these same links on two groups I would still create a different block class?
            – Nick Piro
            Aug 17 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.
            – Sukumar Gorai
            Aug 17 at 8:31










          • If its working for you then click the tick button near my answer so that it will help someone in future.
            – Sukumar Gorai
            Aug 17 at 8:51












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          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="VendorModuleBlockCustomergroupLink" 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="VendorModuleBlockCustomergroupLink" 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="VendorModuleBlockCustomergroupLink" 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="VendorModuleBlockCustomergroupLink" 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 VendorModuleBlockCustomergroup;

          class Link extends MagentoFrameworkViewElementHtmlLinkCurrent

          protected $_customerSession;

          public function __construct(
          MagentoFrameworkViewElementTemplateContext $context,
          MagentoFrameworkAppDefaultPathInterface $defaultPath,
          MagentoCustomerModelSession $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!






          share|improve this answer














          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="VendorModuleBlockCustomergroupLink" 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="VendorModuleBlockCustomergroupLink" 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="VendorModuleBlockCustomergroupLink" 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="VendorModuleBlockCustomergroupLink" 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 VendorModuleBlockCustomergroup;

          class Link extends MagentoFrameworkViewElementHtmlLinkCurrent

          protected $_customerSession;

          public function __construct(
          MagentoFrameworkViewElementTemplateContext $context,
          MagentoFrameworkAppDefaultPathInterface $defaultPath,
          MagentoCustomerModelSession $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!







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 17 at 8:29

























          answered Aug 17 at 7:57









          Sukumar Gorai

          3,8021424




          3,8021424











          • 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
            Aug 17 at 8:25










          • No need to add comma separated. Check my Updated answer.
            – Sukumar Gorai
            Aug 17 at 8:26










          • Thanks! So, if I wanted these same links on two groups I would still create a different block class?
            – Nick Piro
            Aug 17 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.
            – Sukumar Gorai
            Aug 17 at 8:31










          • If its working for you then click the tick button near my answer so that it will help someone in future.
            – Sukumar Gorai
            Aug 17 at 8:51
















          • 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
            Aug 17 at 8:25










          • No need to add comma separated. Check my Updated answer.
            – Sukumar Gorai
            Aug 17 at 8:26










          • Thanks! So, if I wanted these same links on two groups I would still create a different block class?
            – Nick Piro
            Aug 17 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.
            – Sukumar Gorai
            Aug 17 at 8:31










          • If its working for you then click the tick button near my answer so that it will help someone in future.
            – Sukumar Gorai
            Aug 17 at 8:51















          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
          Aug 17 at 8:25




          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
          Aug 17 at 8:25












          No need to add comma separated. Check my Updated answer.
          – Sukumar Gorai
          Aug 17 at 8:26




          No need to add comma separated. Check my Updated answer.
          – Sukumar Gorai
          Aug 17 at 8:26












          Thanks! So, if I wanted these same links on two groups I would still create a different block class?
          – Nick Piro
          Aug 17 at 8:30




          Thanks! So, if I wanted these same links on two groups I would still create a different block class?
          – Nick Piro
          Aug 17 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.
          – Sukumar Gorai
          Aug 17 at 8:31




          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.
          – Sukumar Gorai
          Aug 17 at 8:31












          If its working for you then click the tick button near my answer so that it will help someone in future.
          – Sukumar Gorai
          Aug 17 at 8:51




          If its working for you then click the tick button near my answer so that it will help someone in future.
          – Sukumar Gorai
          Aug 17 at 8:51

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f238665%2fcreate-link-dynamically-by-customer-group-in-customer-account-navigation-magen%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          What does second last employer means? [closed]

          List of Gilmore Girls characters

          Confectionery