Home » Best Magento Extensions » Magento Main Menu Adding Links Mega Menu Extension

Magento Main Menu Adding Links Mega Menu Extension

One of the things that may sound easy to set is the top menu items. However, we’re talking about Magento here and it isn’t as easy as it seems. A specific way of creating menu items out of categories makes this fairly easy task a nightmare. Fortunately for us, Magento has a secret up its sleeve. Magento’s logic allows only categories to appear in the top navigation. Magento already explained how to add a custom link by creating a fake category and redirecting it to a specific page, but they also gave us an event to hook on to do more versatile things with the top menu.

main menu

Config

First at first. A hook has to be defined by adding the code below to the config.xml. We’re simply adding an event that will be fired by a dispatch event where a hook, model and method names are required.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<config>
    <frontend>
        <events>
            <page_block_html_topmenu_gethtml_before>
                <observers>
                    <my_module>
                        <class>my_module/observer</class>
                        <method>addToTopmenu</method>
                    </my_module>
                </observers>
            </page_block_html_topmenu_gethtml_before>
        </events>
   </frontend>
</config>

Observer

Now, inside the observer file which is defined in the config.xml, a method named addToTopmenu has to be placed. The code below is just an example that adds a top menu item named “Categories” together with its children menu items, category names. That’s right, with this we’re able to create nested menu items!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public function addToTopmenu(Varien_Event_Observer $observer)
{
    $menu = $observer->getMenu();
    $tree = $menu->getTree();
    $node = new Varien_Data_Tree_Node(array(
            'name'   => 'Categories',
            'id'     => 'categories',
            'url'    => Mage::getUrl(), // point somewhere
    ), 'id', $tree, $menu);
    $menu->addChild($node);
    // Children menu items
    $collection = Mage::getResourceModel('catalog/category_collection')
            ->setStore(Mage::app()->getStore())
            ->addIsActiveFilter()
            ->addNameToResult()
            ->setPageSize(3);
    foreach ($collection as $category) {
        $tree = $node->getTree();
        $data = array(
            'name'   => $category->getName(),
            'id'     => 'category-node-'.$category->getId(),
            'url'    => $category->getUrl(),
        );
        $subNode = new Varien_Data_Tree_Node($data, 'id', $tree, $node);
        $node->addChild($subNode);
    }
}

The result is visible in the image below:

categories-top-menu-600x206

 

This way of adding new top menu links is easy to implement into Magento and gives us a greater flexibility as opposed to other methods. No fake categories are made and no templates are edited. This method requires some code, but with this programmatic way we’re able to create more than just static menu links. We’re able to create nested menu items, using collections to generate menu items.

Magento Mega Menu Extensions

Menu Customization

You easily hide or show sub-categories on menu. You can hide or show image thumbnail for each category on menu. Also, you can decide how many columns of sub-categories will be displayed when a shopper hover the mother category on menu. Moreover, you can select numbers of products shown in categories.

Custom Block

You can select where a block is placed. It can be on top, left, right or bottom. You can also set block width when it is on right or left. There are options to hide or show blocks

Menu Effects

You can put Home section on menu. Besides, you easily select background and color for each category.

FRONT END DEMO

BACK END DEMO

 (Username: cmsmart_demo, Password: cmsmart2014)

Check Also

What makes Magento extensions be one of the best online store development platforms

What makes Magento extensions be one of the best online store development platforms?

Life has been changing and the way we shopping is now totally different from the …

CLOSE
CLOSE