Removing links from the top level in the PrestaShop top Menu

PrestaShop’s top Menu usually lets you click on the first level categories, as well as their children in the mega-dropdown. In this quick tip, we will disable the top level links and let them be triggers for the drop down menus only.

Overriding the Top Horizontal Menu Module

To make our modification scalable and have it preserve in future upgrades, of not only PrestaShop, but the module itself, we will create an override of it. Please notice overrides are only available for version 1.6.0.11 and newer. If you are using an older PrestaShop version, you will have to amend the core files directly.

Create a new folder named blocktopmenu inside override/modules. Within it, create a new file and name it blocktopmenu.php. Open it up in your favorite code editor and add some generic override code inside php tags:

[php]

class BlockTopMenuOverride extends BlockTopMenu
{

}
[/php]

We want to override a method named generateCategoriesMenu. It’s a protected function so we can easily extend it. Make sure you copy and paste the original one from your module here, as mine might differ even slightly:

[php]

protected function generateCategoriesMenu($categories, $is_children = 0)
{
$html = ”;

foreach ($categories as $key => $category)
{
if ($category[‘level_depth’] > 1)
{
$cat = new Category($category[‘id_category’]);
$link = Tools::HtmlEntitiesUTF8($cat->getLink());
}
else
$link = $this->context->link->getPageLink(‘index’);

$html .= ‘<li’.(($this->page_name == ‘category’
&& (int)Tools::getValue(‘id_category’) == (int)$category[‘id_category’]) ? ‘ class="sfHoverForce"’ : ”).’>’;
$html .= ‘<a href="’.$link.’" title="’.$category[‘name’].’">’.$category[‘name’].'</a>’;

if ($category[‘level_depth’] < 4 && isset($category[‘children’]) && !empty($category[‘children’]))
{
$html .= ‘<ul>’;
$html .= $this->generateCategoriesMenu($category[‘children’], 1);

if ((int)$category[‘level_depth’] > 1 && !$is_children)
{
$files = scandir(_PS_CAT_IMG_DIR_);

if (count($files) > 0)
{
$html .= ‘<li class="category-thumbnail">’;

foreach ($files as $file)
if (preg_match(‘/^’.$category[‘id_category’].’-([0-9])?_thumb.jpg/i’, $file) === 1)
$html .= ‘<div><img src="’.$this->context->link->getMediaLink(_THEME_CAT_DIR_.$file)
.’" alt="’.Tools::SafeOutput($category[‘name’]).’" title="’
.Tools::SafeOutput($category[‘name’]).’" class="imgm" /></div>’;

$html .= ‘</li>’;
}
}

$html .= ‘</ul>’;
}

$html .= ‘</li>’;
}

return $html;
}
[/php]

We are interested in the following line:

[php]
$html .= ‘<a href="’.$link.’" title="’.$category[‘name’].’">’.$category[‘name’].'</a>’;
[/php]

Which is the one responsible for displaying the link to each category. However, if we were to remove the anchor as it is, it would get rid of all the links, and we want to keep the ones inside mega menus. We need to add a condition that first checks we are dealing with our top level. How can we do it?
The most logical way would be to check the “level_depth” property of the current category. This might work, in case your top menu categories all belong to the same depth level.

However, we can use another, more reliable variable: $is_children. It’s passed to each iteration of the loop used to generate the category tree, and checks whether we are in the top level or not, so it’s ideal for our purpose. Knowing that, we can replace the previous line with some logic:

[php]
if ($is_children == 0)
$html .= ‘<a>’.$category[‘name’].'</a>’;
else
$html .= ‘<a href="’.$link.’" title="’.$category[‘name’].’">’.$category[‘name’].'</a>’;
[/php]

Please notice it needs an anchor wrapper in any case, to retain its style. Save the override, then reach the cache folder and get rid of class_index.php so that the override takes place. Depending on your configuration, you might as well need to go to Advanced Parameters, Performance, and clear cache.

Need PrestaShop Modules? Have a look at my Prestashop Addons Store!

Looking for quality PrestaShop web hosting? Look no further than Arvixe Web Hosting.

Tags: , , , | Posted under PrestaShop | RSS 2.0

Author Spotlight

Fabio Porta

Fabio has been involved in web development and design since 2005, when launched his first website at the age of 16. He’s now highly skilled in both client and server side development, along with design, and since August 2012 runs a successful website about PrestaShop tutorials and Prestashop Modules called Nemo’s Post Scriptum, at http://nemops.com

Leave a Reply

Your email address will not be published. Required fields are marked *