How to add a new item to the navigation menu in Magento

How to add a new item to the navigation menu in Magento

How to add a new item to the navigation menu in Magento
Listing a new item to the navigation menu is considered to be essential in some occassions while integrating a magento theme So we will add this ability to a custom module. There is no need to modify core Magento files or templates and layouts. We need to rewrite the block that is responsible for menu drawing first ; we will not be using a tag. The reason for this is, if the module is disabled, Magento will break. Instead, will use an observer for it. At global scope, in your config.xml file, add the following code:


Now you can also use other events instead of controller_action_layout_generate_blocks_after. Next, you must create Observer.php in the Model folder using method regenerateMenu as follws :
class NameSpace_ModuleName_Model_Observer
public function newMenu($observer)
  // if module is active
  if (!Mage::getStoreConfig('advanced/modules_disable_output/ NameSpace_ModuleName'))
    $layout = Mage::getSingleton('core/layout');
    // remove all the blocks you don't want
    // load layout updates by specified handles
    // generate xml from collected text updates
    // generate blocks from xml layout

Your Next step is : Should create the block file Navigation.php with the following code:
class NameSpace_ModuleName_Block_Navigation extends Mage_Catalog_Block_Navigation
public function renderCategoriesMenuHtml($level = 0, $outermostItemClass = '', $childrenWrapClass = '') 
  // if route is active
  $active = ($this->getRequest()->getRouteName()=='yourroute' ? 'active' : '');
  // Get navigation menu html
  $html = parent::renderCategoriesMenuHtml($level, $outermostItemClass, $childrenWrapClass);
  // if module is active
  if (Mage::helper('core/data')->isModuleEnabled('NameSpace_ModuleName')) {
    // Adding new menu item. You can also add few items or child elements there
         $html .=  '
  • '. $this->__("New Menu Item") .'
  • '; } return $html; } }
    Now Declare the block in your config.xml file. Then, you need to declare the path for your layout XML in config as :
    Finally, edit content for newmenuitem.xml and thats it . Try :-)

    • Share on Facebook
    • Share on Twitter
    • Share on LinkedIn

    leave a comment