Limiting subcategories in the Prestashop Top Menu

In this quick tip we will see how to limit the number of subcategories the top menu displays in Prestashop.

Watch the Screencast

Text Version

Notice: if you are using a Prestashop version newer than 1.6.0.11, it’s recommended to apply this modification with an override, as explained here: How to override Prestashop modules

Open up modules/blocktopmenu/blocktopmenu.php

Locate the following method:

protected function generateCategoriesMenu($categories, $is_children = 0)

Inside it, you will notice a conditional block

			if (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>';
			}

We can do a number of things here. If we comment it out, all submenus will be removed. Otherwise, we can reduce and limit the subcategory levels:

			if ($category['level_depth'] < 4 && isset($category['children']) && !empty($category['children']))
			{
			...

In this case, levels over 3 will not be displayed!

You like the tuts and want to say "thank you"? Well, you can always feel free to donate:

  • Pascal Camberlin

    Hello Nemo.

    We have a lot of subcat in our prestashop and I wanna know if we can do that with the 1.7 version ?
    I hope you can help
    Best regard

    • NemoPS

      I didn’t check actually, it might not work on 1.7 though, I will have a look and see as it might be a good subject for new tuts

      • Pascal Camberlin

        If you find something, can you share it even if you don’t make a tuts ?
        I try but i haven’t find how to limite the top menu (even on google)

  • http://glexport.eu Ivan

    Hello again, Nemo. I’ve been working on this feature but still no success with the override I actually copy the whole protected function generateCategoriesMenu inside the override file but is still not working. Can you give me a ligth in this subject? Thanks a lot.

  • http://glexport.eu Ivan

    Hello Nemo, I have created the override but is still not working over 1.6.0.14. It would be really helpfull if you mention how the overrides should looks like. I hope you can help me.

Store Top Sales

You like the tuts and want to say "thank you"? Well, you can always feel free to donate: