Adding links to top menu in magento

In magento frontend, top menu is fact for customer need. When you add some menu without category then is a fact to add menu. Here I add a menu programmatically with top menu. Following step can add a menu.

Suppose Namespace is “Exercise” and module name is “FrontendMenu” and CodePool is “local”

Step 1: Create a module config file into app/etc/modules/Exercise_FrontendMenu.xml

<config>
    <modules>
        <Exercise_FrontendMenu>
            <active>true</active>
            <codePool>local</codePool>
        </Exercise_FrontendMenu>
    </modules>
</config>

Step 2: Create a config file under app/code/local/Exercise/FrontendMenu/etc/config.xml

<config>
    <modules>
        <Exercise_FrontendMenu>
            <version>0.1.0</version>
        </Exercise_FrontendMenu>
    </modules>
    <global>
        <blocks>
            <frontendmenu>
                <class>Exercise_FrontendMenu_Block</class>
            </frontendmenu>
        </blocks>
        <models>
            <frontendmenu>
                <class>Exercise_FrontendMenu_Model</class>
            </frontendmenu>
        </models>
    </global>
    <frontend>
        <events>
            <page_block_html_topmenu_gethtml_before>
                <observers>
                    <frontendmenu>
                        <class>frontendmenu/observer</class>
                        <method>addToTopmenu</method>
                    </frontendmenu>
                </observers>
            </page_block_html_topmenu_gethtml_before>
        </events>
    </frontend>
</config>

Step 3 : Create a observer into app/code/local/Exercise/FrontendMenu/Model/Observer.php

class Exercise_FrontendMenu_Model_Observer
{
    public function addToTopmenu(Varien_Event_Observer $observer)
    {
        $menu = $observer->getMenu();
        $tree = $menu->getTree();
        // create a menu tree node
        $node = new Varien_Data_Tree_Node(array(
            'name'   => 'Products',
            'id'     => 'products',
            'url'    => Mage::getUrl('products') // Link url
        ), 'id', $tree, $menu);
        // add node to menu
        $menu->addChild($node);
    }
}

That’s it! Magento is always beautiful 🙂

One thought on “Adding links to top menu in magento

  1. This is very good hook to add link in top menu, but it’s works only if magento version 1.7+, if I want to add it lower version than this code is not work, could you please help to how should add link in top menu in lower version?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s