Skip to content

Instantly share code, notes, and snippets.

@tinchodev
Created July 20, 2015 19:46
Show Gist options
  • Save tinchodev/475e32bc5d78f5a0044b to your computer and use it in GitHub Desktop.
Save tinchodev/475e32bc5d78f5a0044b to your computer and use it in GitHub Desktop.
/*
<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>
*/
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment