Removing Top.links URL then Re-adding Link with Custom Label via Local.xml
Managing navigation links in Magento is a common task for developers and store administrators. If you’re trying to remove the “My Account” link from top.links, then reintroduce it with custom labels based on customer status (logged in or out), you’ve come to the right place.
The Solution
Here’s how you can achieve this using Magento’s layout XML files. We’ll be editing local.xml located at app/design/frontend/package/theme/layout/local.xml.
Step 1: Remove the “My Account” Link
To remove the “My Account” link for all customers, including guests and logged-in users, add the following to your local.xml:
<reference name="root">
<reference name="top.links">
<action method="removeLinkByUrl"><url helper="customer/getAccountUrl"/><position>10</position></action>
</reference>
</reference>
This code tells Magento to remove the link at position 10 (the default for “My Account”) using the getAccountUrl helper.
Step 2: Conditionally Add Links
Now, let’s add back the links with custom labels based on customer status. Update your local.xml:
<reference name="root">
<!-- Remove My Account link -->
<reference name="top.links">
<action method="removeLinkByUrl"><url helper="customer/getAccountUrl"/><position>10</position></action>
</reference>
<!-- Customer Logged Out: Add Register Link -->
<customer_logged_out>
<reference name="top.links">
<action method="addLink" translate="label title" module="customer">
<label>Register</label>
<url helper="customer/getRegisterUrl"/>
<title>Register</title>
<prepare/>
<urlParams/>
<position>10</position>
</action>
</reference>
</customer_logged_out>
<!-- Customer Logged In: Add My Account and Log Out Links -->
<customer_logged_in>
<reference name="top.links">
<action method="addLink" translate="label title" module="customer">
<label>My Account</label>
<url helper="customer/getAccountUrl"/>
<title>My Account</title>
<prepare/>
<urlParams/>
<position>10</position>
</action>
<!-- Add Log Out Link -->
<action method="addLink" translate="label title" module="customer">
<label>Log Out</label>
<url helper="customer/getLogoutUrl"/>
<title>Log Out</title>
<prepare/>
<urlParams/>
<position>100</position> <!-- Position the Log Out link at 100 to display it last -->
</action>
</reference>
</customer_logged_in>
</reference>
This updated code does several things:
- Removes the “My Account” link using
removeLinkByUrlaction. - Conditionally adds a “Register” link for logged-out customers (guests) using the
customer_logged_outcondition andaddLinkaction. - Adds both “My Account” and “Log Out” links for logged-in customers using the
customer_logged_incondition and multipleaddLinkactions.
Step 3: Flush the Cache
After making these changes, make sure to flush your Magento cache by navigating to System > Cache Management in the admin panel and selecting all cache types before clicking “Flush Cache(s).”
Conclusion
In this blog post, we’ve demonstrated how to remove a specific top.links URL using local.xml, then reintroduce it with custom labels based on customer status. This solution allows you to maintain control over your store’s navigation while keeping the code clean and manageable.
Remember that editing layout XML files directly in the Magento root directory is not recommended for production environments, as it may cause issues during updates or upgrades. Instead, consider creating a custom theme and adding these changes there.
For more information on working with Magento layouts and XML configuration, visit our guide Understanding Magento Layout XML at Laravel Company.
Happy coding!