Posted on

Add custom link in customer account navigation

I want to add custom link in customer’s navigation.I have developed below code but getting 404 – not found issue while click on the tab.

<?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=”MagentoFrameworkViewElementHtmlLinkCurrent” name=”module”>
<arguments>
<argument name=”path” xsi:type=”string”>module/customer/list</argument>
<argument name=”label” xsi:type=”string” translate=”true”>My Data</argument>
</arguments>
</block>
</referenceBlock>
</body>
</page>

Then I create a controller file name : List.php at modulename/controller/customer

<?php
namespace namespacemodulenameControllerCustomer;
use MagentoSalesControllerOrderInterface;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkViewResultPageFactory;

class List extends MagentoFrameworkAppActionAction
{
/**
* @var PageFactory
*/
protected $resultPageFactory;

/**
* @param Context $context
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}

/**
* Customer order history
*
* @return MagentoFrameworkViewResultPage
*/
public function execute()
{
$resultPage = $this->resultPageFactory->create();
$resultPage->getConfig()->getTitle()->set(__(‘My Data’));

$block = $resultPage->getLayout()->getBlock(‘customer.account.link.back’);
if ($block) {
$block->setRefererUrl($this->_redirect->getRefererUrl());
}
return $resultPage;
}
}

Block file at namespace/module/block/customer/test/list.php

<?php

namespace namespacemodulenameControllerCustomer;

use MagentoSalesControllerOrderInterface;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkViewResultPageFactory;

class List extends MagentoFrameworkAppActionAction
{
/**
* @var PageFactory
*/
protected $resultPageFactory;

/**
* @param Context $context
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}

/**
* Customer order history
*
* @return MagentoFrameworkViewResultPage
*/
public function execute()
{
$resultPage = $this->resultPageFactory->create();
$resultPage->getConfig()->getTitle()->set(__(‘My Orders’));

$block = $resultPage->getLayout()->getBlock(‘customer.account.link.back’);
if ($block) {
$block->setRefererUrl($this->_redirect->getRefererUrl());
}
return $resultPage;
}
}

and add xml file : modulename_test_list.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=”MagentoFrameworkViewElementHtmlLinkCurrent” name=”modulename”>
<arguments>
<argument name=”path” xsi:type=”string”>modulename/customer/list</argument>
<argument name=”label” xsi:type=”string” translate=”true”>My Data</argument>
</arguments>
</block>
</referenceBlock>
</body>

Leave a Reply

Your email address will not be published. Required fields are marked *