Magento: Removing links in the navigation block by name or by label

Posted by Stuart Forster on 11th May 2012

Sometimes in Magento you may want to remove links from the customer navigation block. What you'll notice is it's actually quite hard to do so because there is no 'remove' method.

In this tutorial I will show you had to extend Mage_Customer_Block_Account_Navigation to include an extra two methods, one to remove links by name and the other by label.

Step 1

First, we want to create our module.

app/etc/modules/Gravitywell_All.xml

<?xml version="1.0"?>
<config>
    <modules>      
        <Gravitywell_Customer>
            <active>true</active>
            <codePool>local</codePool>
        </Gravitywell_Customer>
    </modules>
</config>

Step 2

Next we want to create our configuration for our module. Inside this configuration we want to rewrite the Mage_Customer_Block_Account_Navigation class to our new, Gravitywell_Customer_Block_Account_Navigation class which will extend Mage_Customer_Block_Account_Navigation.

app/code/local/Gravitywell/Customer/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Gravitywell_Customer>
            <version>0.1.0</version>
        </Gravitywell_Customer>
    </modules>
    <global>
        <blocks>
            <customer>
                <rewrite>
                    <account_navigation>Gravitywell_Customer_Block_Account_Navigation</account_navigation>
                </rewrite>
            </customer>
        </blocks>
    </global>
</config>

Step 3

We are now able to create the actual block. Go ahead and create the following file and copy the code contents into it.

app/code/local/Gravitywell/Customer/Block/Account/Navigation.php

<?php

class Gravitywell_Customer_Block_Account_Navigation extends Mage_Customer_Block_Account_Navigation
{
    /**
     * Removes a link by name
     *
     * @param $name string
     * @return Gravitywell_Customer_Block_Account_Navigation
     */

    public function removeLinkByName($name)
    {
        foreach($this->_links as $k => $v) {
            if($v->getName() == $name) {
                unset($this->_links[$k]);
            }
        }

        return $this;
    }


    /**
     * Removes a link by label
     *
     * This is useful because sometimes, the links aren't named.
     *
     * @param $label string
     * @return Gravitywell_Customer_Block_Account_Navigation
     */

    public function removeLinkByLabel($label)
    {
        foreach($this->_links as $k => $v) {
            if($v->getLabel() == $label) {
                unset($this->_links[$k]);
            }
        }

        return $this;
    }
}

Step 4

Finally now I will be able to demonstrate its usage.

Find your themes local.xml and open it, if you don't have one or are not sure where to find this you can follow my theme tutorial here which should bring you up to date.

The contents of my local.xml might look like the following, notice we now have two new methods available to us. These are the methods from Gravitywell_Customer_Block_Account_Navigation above.

app/design/frontend/gravitywell/default/layout/local.xml

<layout>

    <customer_account translate="label">

        <reference name="customer_account_navigation">
            <action method="removeLinkByName"><name>recurring_profiles</name></action>
            <action method="removeLinkByName"><name>billing_agreements</name></action>
            <action method="removeLinkByName"><name>downloadable_products</name></action>
            <action method="removeLinkByLabel"><name>My Tags</name></action>
        </reference>

    </customer_account>

</layout>

Step 5

Your done! All you need to do now is clean your cache and the navigation links should be removed.