1

I am trying to add refer a friend link in the customer account page. I followed these links.

Add new link to customer account page

Magento 2 Custom Tab on Customer Account

It doesnt work for me

customer_account.xml

 <referenceBlock name="customer_account_navigation">
    <block class="Magento\Framework\View\Element\Html\Link\Current" name="refer-a-friend">
      <arguments>
       <argument name="path" xsi:type="string">customer/refer</argument>
       <argument name="label" xsi:type="string">Refer a Friend</argument>
      </arguments>
    </block>
 </referenceBlock>

routes.xml in app/code/YX/Customer/etc/frontend/routes.xml

<?xml version="1.0" ?>
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
   <router id="standard">
     <route frontName="yx_customer" id="yx_customer">
         <module name="YX_Customer"/>
     </route>
  </router>
</config>

customer_refer_index.xml in app/code/Vendor/Module/view/frontend/layout/customer_refer_index.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">
  <update handle="customer_account"/>
   <body>
     <referenceContainer name="content">
         <block class="YX\Customer\Block\Friend"
               name="referFriend"
              template="Magento_Customer::account/refer_a_friend.phtml"
        />
    </referenceContainer>
 </body>

Block file in app/code/YX/Customer/Block/Friend

class Friend extends Template
 {

   public function __construct(Context $context)
    {
      parent::__construct($context);
    }
 }

Controller app/code/YX/Customer/Controller/Refer/Index.php

class Index extends \Magento\Framework\App\Action\Action
 {

   protected $resultPageFactory;


   public function __construct(
      \Magento\Framework\App\Action\Context $context,
      \Magento\Framework\View\Result\PageFactory $resultPageFactory
  ) {
      $this->resultPageFactory = $resultPageFactory;
      parent::__construct($context);
   }


   public function execute()
   {
      return $this->resultPageFactory->create();
   }
 }

refer-a-friend.phtml

<div > Hello this is my template. I can do anything inside this file.</div>

I have no idea what I am doing wrong.

2

Your path url and your layout file name are incorrect. Your frontName in routers.xml is yx_customer and not customer.

customer_account.xml

 <referenceBlock name="customer_account_navigation">
<block class="Magento\Framework\View\Element\Html\Link\Current" name="refer-a-friend">
  <arguments>
   <argument name="path" xsi:type="string">yx_customer/refer</argument>
   <argument name="label" xsi:type="string">Refer a Friend</argument>
  </arguments>
</block>

And your layout file should be named yx_customer_refer_index.xml

  • I had a similar problem, issue was with layout file name and missing route.xml, took sometime to figure this out. – cnu May 16 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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