Magento Basic Setting - How to add a “Home” link to the top menu?


How to add a “Home” link to the top menu?

Magento 2 is awesome, now you can learn and practice every thing about Magento 2 with

Magestore Ultimate Magento 2 Tutorial

More than 150 tips, guides and tutorial. Everything is Free forever.

50 000 Magento developers have joined, explore Magento 2 in the easiest way today!

This is a handy script to add a home link to the top menu.

In magento 1.7, the menu is now created via an *event* (page_block_html_topmenu_gethtml_before)

The catalog observer hooks into this and adds menu items as Varien_Data_Tree_Node objects.

There are some samples here to show you how to add a link at the beginning of this menu.

config.xml portion.

Copy the code below:

===============================
<pre>
<config>
….
<frontend>
<events>
<page_block_html_topmenu_gethtml_before>
<observers>
<catalog_add_topmenu_items>
<class>cp_layout/observer</class>
<method>addTopMenuItems</method>
</catalog_add_topmenu_items>
</observers>
</page_block_html_topmenu_gethtml_before>
</events> 
</frontend>
</config>
</pre>
observer portion:
=================================
<pre>
<?php
Class Cp_Layout_Model_Observer
{
/**
* Adds additional links to the top menu
*
* @param Varien_Event_Observer $observer
*/
public function addTopMenuItems(Varien_Event_Observer $observer)
{
$nodeId = “home”;
$menu = $observer->getMenu();
$tree = $observer->getMenu()->getTree();

$data = array(
“name” => “Home”,
“id” => $nodeId,
“url” => Mage::getBaseUrl(),
“is_active” => Mage::getSingleton(‘cms/page’)->getIdentifier() == ‘home’ && Mage::app()->getFrontController()->getRequest()->getRouteName() == ‘cms’ 
);
$homeNode = new Varien_Data_Tree_Node($data, ‘id’, $tree, $menu);
$this->_prependNode($homeNode, $menu);

}
protected function _prependNode($node, $menu)
{
$menu->addChild($node);
$nodeId = $node->getId();
$readded = array();
foreach($menu->getChildren()->getNodes() as $n)
{
if ($n->getId() != $nodeId)
{
$readded[] = $n;
$menu->getChildren()->delete($n); 
}
}
foreach($readded as $r)
{
$menu->getChildren()->add($r); 

}
}
</pre>


Other Answers in the Category "Magento Basic Setting"

Top