How To

How to Change My Account Navigation Links Title Dynamically in Magento 2

Hello, Magento Folks,

I am back with another article on Magento development. Last time, We have learned how you can add dynamic row multiselect in Magento 2 system configuration. Today, I am going to explain to you how you can change My Account navigation links title dynamically in Magento2.

Whenever you create my account dashboard navigation link in default Magento, you need to create a customer_account.xml file, and in this file, you need to add title as static. But sometimes you may need to change this title dynamically. And you are not able to do that by hook or cook in the Magento system configuration.

But don’t worry, we have a solution for that, and to make the title link dynamically, you need to have a code that can make the work simple for you. It would help if you had a bit of coding knowledge to achieve this in your Magento 2 store. If you do, then add this following code in your custom extension.

1] First, we need to add customer_account.xml file at the following path,

app\code\Vendor\Extension\view\frontend\layout\customer_account.xml

Now, add the below code to this file.

<?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="Magento\Framework\View\Element\Html\Link\Current" name="customer-account-navigation-mycard">
                <arguments>
                    <argument name="path" xsi:type="string">router
/controller/action </argument>
                    <argument name="label" xsi:type="string">Next Level</argument> 
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

2] Now you need to create plugin add di.xml file at the following path,

app\code\Vendor\Extension\etc\di.xml

Add the below code to this file.

<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\Navigation">
    <plugin name="account_tab_plugin" type="Vendor \Extension\Plugin\Changetabtitle" sortOrder="10" disabled="false"  />
</type>
</config>

3] Now, you need to create Changetabtitle.php file at the following path,

app\code\Vendor\Extension\Plugin\Changetabtitle.php

Add the below code to this file.

<?php

namespace Vendor\Extension\Plugin;

class Changetabtitle
{


 public function afterGetLinks(\Magento\Customer\Block\Account\Navigation $subject, $result)
    {

        foreach($result as $item) {
            $tarray = $item->getData();
            if ($tarray['type'] != 'Magento\\Customer\\Block\\Account\\Delimiter') {
                if ($tarray['label'] == "Next Level") {
                    $item->setLabel("Add title dynamic"); //You add title dynamically here.
                }
            }
        }

        return $result;

    }

}

So, this was it. Simple, isn’t it?

With the help of these codes, you will be able to create dynamic My Account navigation title links. If you had any problem while implementing these codes, then contact us at our support desk. We will be happy to help you.

Lastly, if you like this article, then let us know in the comments section. Also, do share this with your Magento colleagues and friends. If you faced issues or suggestions about code implement we love to hear from you! You may contact our support team here.

Happy Reading?

 

 

Click to rate this post!
[Total: 7 Average: 4.6]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

3 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

4 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

5 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

7 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago