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 Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

8 hours ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

3 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

3 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

7 days ago