1

How to change the actions of top links in Magento 2

My Account --> customer/account to My Account --> customer/dashboard

1 Answer 1

2

You can achieve this by create a plugin of class Magento\Customer\Block\Account\Link.

First create a plugin for Magento\Customer\Block\Account\Link of function getHref(). Then on afterGetHref changes the dir of backup.

Define plugin in di.xml

Define plugin at a app/code/{Vendor}/{Modulename}/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\Block\Account\Link">
        <plugin name="change_toplink_url" type="{VendorName}\{Modulename}\Plugin\AccountLinkPlugin" sortOrder="1" />
    </type>
</config>

Plugin Class:

<?php
namespace {VendorName}\{Modulename}\Plugin;
use Magento\Framework\UrlInterface; 
class AccountLinkPlugin
{  
 /**
 * @var UrlInterface
 */
protected $urlBuilder;

public function __construct(
    UrlInterface $urlBuilder
)
{
    $this->urlBuilder = $urlBuilder;
}
    public function afterGetHref(
        \Magento\Customer\Block\Account\Link $subject,
         $result
    )
    {
        return $this->urlBuilder->getUrl('customer/account/dashboard');

    }

}

You need to do di compile and cache flush in to active this plugin.

5
  • Please elaborate i am new in magento Apr 9, 2018 at 13:39
  • Fatal error: Uncaught TypeError: Argument 2 passed to Xudot\ReferralCredit\Plugin\AccountLinkPlugin::afterGetHref() must be of the type array, string given Apr 9, 2018 at 13:46
  • Have done di complie?
    – Amit Bera
    Apr 9, 2018 at 13:50
  • I just remove {array $result} from class and it works Apr 9, 2018 at 13:53
  • ooh, my mistake.Yes it should be string not array
    – Amit Bera
    Apr 9, 2018 at 13:58

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.