Jump to content

Tip: how to hide category in the menu bar horizontal


Recommended Posts

The catalog of my e-commerce contains hundreds of categories and subcategories. For obvious reasons, if you put all the categories and subcategories in the horizontal bar of the menu, it would become chaotic and unmanageable. For this reason I thought it would be appropriate to "hide" from view in the bar a few categories. I would also like that the categories are not displayed in the horizontal bar were visible in the list of the page when you click on a category. 

 

How can I do?

Link to comment
Share on other sites

Hi,

 

For the horizontal bar I did it by going to module Top horizontal bar in modules and then remove the category from the right pane where what you want to show goes, and create on same module a "add menu top link" linking (url) to the root of the category you want to have on the bar :)

Regarding the left menu there is an option to select level of expand categories under the categories module.

 

Hope this helps

Link to comment
Share on other sites

Thanks for the reply. I can not remove the module from the category that I want to hide because it is a sub-category and therefore is not visible in the management of the module.

I replied to the pm you sent me. You need to remove the whole category if there already, if not added by you there's nothing to remove, just create a link to the category at the level you want and add it to bar. 

Link to comment
Share on other sites

to hide subcategory from block top menu it's necessary to modify the module php file

there is no other way to achieve this, it's because whole menu (also categories and subcategories) are generated in .php file - not in .tpl file as it is in other modules.

Link to comment
Share on other sites

to hide subcategory from block top menu it's necessary to modify the module php file

there is no other way to achieve this, it's because whole menu (also categories and subcategories) are generated in .php file - not in .tpl file as it is in other modules.

thanks for the reply. What should I modify the php file?

Link to comment
Share on other sites

blocktopmenu.php

 

there is a function:
 

	private function getCategory($id_category, $id_lang = false, $id_shop = false)
	{
		$id_lang = $id_lang ? (int)$id_lang : (int)Context::getContext()->language->id;
		$category = new Category((int)$id_category, (int)$id_lang);

		if ($category->level_depth > 1)
			$category_link = $category->getLink();
		else
			$category_link = $this->context->link->getPageLink('index');

		if (is_null($category->id))
			return;

		$children = Category::getChildren((int)$id_category, (int)$id_lang, true, (int)$id_shop);
		$selected = ($this->page_name == 'category' && ((int)Tools::getValue('id_category') == $id_category)) ? ' class="sfHoverForce"' : '';

		$is_intersected = array_intersect($category->getGroups(), $this->user_groups);
		// filter the categories that the user is allowed to see and browse
		if (!empty($is_intersected))
		{
			$this->_menu .= '<li '.$selected.'>';
			$this->_menu .= '<a href="'.Tools::HtmlEntitiesUTF8($category_link).'">'.$category->name.'</a>';

			if (count($children))
			{
				$this->_menu .= '<ul>';

				foreach ($children as $child)
					$this->getCategory((int)$child['id_category'], (int)$id_lang, (int)$child['id_shop']);

				$this->_menu .= '</ul>';
			}
			$this->_menu .= '</li>';
		}
	}

you have to add there if condition to exclude category you don't want to display.

add it here:

		if (!empty($is_intersected))
		{
			$this->_menu .= '<li '.$selected.'>';
			$this->_menu .= '<a href="'.Tools::HtmlEntitiesUTF8($category_link).'">'.$category->name.'</a>';

			if (count($children))
			{
				$this->_menu .= '<ul>';

				foreach ($children as $child)
					$this->getCategory((int)$child['id_category'], (int)$id_lang, (int)$child['id_shop']);

				$this->_menu .= '</ul>';
			}
			$this->_menu .= '</li>';
		}
Link to comment
Share on other sites

if parent category id number is, for example, 51 use this code:

if ($category->id !=51){

        if (!empty($is_intersected))
        {

            $this->_menu .= '<li '.$selected.'>';
            $this->_menu .= '<a href="'.Tools::HtmlEntitiesUTF8($category_link).'">'.$category->name.'</a>';

            if (count($children))
            {
                $this->_menu .= '<ul>';

                foreach ($children as $child)
                    $this->getCategory((int)$child['id_category'], (int)$id_lang, (int)$child['id_shop']);

                $this->_menu .= '</ul>';
            }
            $this->_menu .= '</li>';
        }

}

Link to comment
Share on other sites

if parent category id number is, for example, 51 use this code:

if ($category->id !=51){

        if (!empty($is_intersected))

        {

            $this->_menu .= '<li '.$selected.'>';

            $this->_menu .= '<a href="'.Tools::HtmlEntitiesUTF8($category_link).'">'.$category->name.'</a>';

 

            if (count($children))

            {

                $this->_menu .= '<ul>';

 

                foreach ($children as $child)

                    $this->getCategory((int)$child['id_category'], (int)$id_lang, (int)$child['id_shop']);

 

                $this->_menu .= '</ul>';

            }

            $this->_menu .= '</li>';

        }

}

 

 

so I have to repeat the given string for each category that I want to "hide"?

Link to comment
Share on other sites

  • 1 year later...

I try this solution: https://www.prestashop.com/forums/topic/334071-how-to-hide-empty-categories-in-top-menu/ inside the file classes/Category.php and it works!!

I edit it for multistore ecommerce site in this way:

 

    public static function getNestedCategories($root_category = null, $id_lang = false, $active = true, $groups = null,
        $use_shop_restriction = true, $sql_filter = '', $sql_sort = '', $sql_limit = '', Context $context = null)
    {
        if (!$context) {
            $context = Context::getContext();
        }
        
        if (isset($root_category) && !Validate::isInt($root_category)) {
            die(Tools::displayError());
        }

        if (!Validate::isBool($active)) {
            die(Tools::displayError());
        }

        if (isset($groups) && Group::isFeatureActive() && !is_array($groups)) {
            $groups = (array)$groups;
        }

        $cache_id = 'Category::getNestedCategories_'.md5((int)$root_category.(int)$id_lang.(int)$active.(int)$use_shop_restriction
            .(isset($groups) && Group::isFeatureActive() ? implode('', $groups) : ''));

        if (!Cache::isStored($cache_id)) {
            
            $result = Db::getInstance()->executeS('SELECT c.*, cl.*
                FROM `'._DB_PREFIX_.'category` c
                '.($use_shop_restriction ? Shop::addSqlAssociation('category', 'c') : '').'
                LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON c.`id_category` = cl.`id_category`'.Shop::addSqlRestrictionOnLang('cl').'
                                LEFT JOIN `'._DB_PREFIX_.'category_product` cp on cp.`id_category` = c.`id_category`
                                LEFT JOIN `'._DB_PREFIX_.'product_shop` ps on ps.`id_product` = cp.`id_product`
                '.(isset($groups) && Group::isFeatureActive() ? 'LEFT JOIN `'._DB_PREFIX_.'category_group` cg ON c.`id_category` = cg.`id_category`' : '').'
                '.(isset($root_category) ? 'RIGHT JOIN `'._DB_PREFIX_.'category` c2 ON c2.`id_category` = '.(int)$root_category.' AND c.`nleft` >= c2.`nleft` AND c.`nright` <= c2.`nright`' : '').'
                WHERE 1 '.$sql_filter.' '.($id_lang ? 'AND `id_lang` = '.(int)$id_lang : '').'
AND ps.active = 1 and ps.available_for_order = 1 and ps.id_shop = '.(int)$context->shop->id.'
                '.($active ? ' AND c.`active` = 1' : '').'
                '.(isset($groups) && Group::isFeatureActive() ? ' AND cg.`id_group` IN ('.implode(',', $groups).')' : '').'
                '.(!$id_lang || (isset($groups) && Group::isFeatureActive()) ? ' GROUP BY c.`id_category`' : '').'
                '.($sql_sort != '' ? $sql_sort : ' ORDER BY c.`level_depth` ASC').'
                '.($sql_sort == '' && $use_shop_restriction ? ', category_shop.`position` ASC' : '').'
                '.($sql_limit != '' ? $sql_limit : ''));
 

If you want to see the result immediately disable the cache in the BO on "advance parameters"-"performance".

This solution works in the 1.6.1 version too.

Link to comment
Share on other sites

  • 4 months later...

 

blocktopmenu.php

 

there is a function:

 

	private function getCategory($id_category, $id_lang = false, $id_shop = false)
	{
		$id_lang = $id_lang ? (int)$id_lang : (int)Context::getContext()->language->id;
		$category = new Category((int)$id_category, (int)$id_lang);

		if ($category->level_depth > 1)
			$category_link = $category->getLink();
		else
			$category_link = $this->context->link->getPageLink('index');

		if (is_null($category->id))
			return;

		$children = Category::getChildren((int)$id_category, (int)$id_lang, true, (int)$id_shop);
		$selected = ($this->page_name == 'category' && ((int)Tools::getValue('id_category') == $id_category)) ? ' class="sfHoverForce"' : '';

		$is_intersected = array_intersect($category->getGroups(), $this->user_groups);
		// filter the categories that the user is allowed to see and browse
		if (!empty($is_intersected))
		{
			$this->_menu .= '<li '.$selected.'>';
			$this->_menu .= '<a href="'.Tools::HtmlEntitiesUTF8($category_link).'">'.$category->name.'</a>';

			if (count($children))
			{
				$this->_menu .= '<ul>';

				foreach ($children as $child)
					$this->getCategory((int)$child['id_category'], (int)$id_lang, (int)$child['id_shop']);

				$this->_menu .= '</ul>';
			}
			$this->_menu .= '</li>';
		}
	}

you have to add there if condition to exclude category you don't want to display.

add it here:

		if (!empty($is_intersected))
		{
			$this->_menu .= '<li '.$selected.'>';
			$this->_menu .= '<a href="'.Tools::HtmlEntitiesUTF8($category_link).'">'.$category->name.'</a>';

			if (count($children))
			{
				$this->_menu .= '<ul>';

				foreach ($children as $child)
					$this->getCategory((int)$child['id_category'], (int)$id_lang, (int)$child['id_shop']);

				$this->_menu .= '</ul>';
			}
			$this->_menu .= '</li>';
		}

Hi,

 

thank you for your reply to this post, this is really getting me closer to delete those unwanted subcategories from the Top Horizontal Bar.

 

Please know that our domain name is https://usapartsandmore.com

one of the sub-categories we are trying delete is Control Boards, under Kitchen-Refrigerator & Freezer Parts-Control Boards

The Category id is 34. We are also trying to delete all of the subcategories so do we have to repeat the following each time? 

Ex: if ($category->id !=34){

     if ($category->id !=33){   (this would be another subcategory "Ice Makers")

 

We are including this to our blocktopmenu.php file: (Please let us know if we are doing it right

 

 

private function getCategory($id_category, $id_lang = false, $id_shop = false)

    {

        $id_lang = $id_lang ? (int)$id_lang : (int)Context::getContext()->language->id;

        $category = new Category((int)$id_category, (int)$id_lang);

 

        if ($category->level_depth > 1)

            $category_link = $category->getLink();

        else

            $category_link = $this->context->link->getPageLink('index');

 

        if (is_null($category->id))

            return;

 

        $children = Category::getChildren((int)$id_category, (int)$id_lang, true, (int)$id_shop);

        $selected = ($this->page_name == 'category' && ((int)Tools::getValue('id_category') == $id_category)) ? ' class="sfHoverForce"' : '';

 

        $is_intersected = array_intersect($category->getGroups(), $this->user_groups);

        // filter the categories that the user is allowed to see and browse

        if (!empty($is_intersected))

        {

            $this->_menu .= '<li '.$selected.'>';

            $this->_menu .= '<a href="'.Tools::HtmlEntitiesUTF8($category_link).'">'.$category->name.'</a>';

 

            if (count($children))

            {

                $this->_menu .= '<ul>';

 

                foreach ($children as $child)

                    $this->getCategory((int)$child['id_category'], (int)$id_lang, (int)$child['id_shop']);

 

$this->_menu .= '</ul>';

            }

            $this->_menu .= '</li>';

        }

    }

 
if ($category->id !=34){

 

if (!empty($is_intersected))

        {

            $this->_menu .= '<li '.$selected.'>';

            $this->_menu .= '<a href="'.Tools::HtmlEntitiesUTF8($category_link).'">'.$category->name.'</a>';

 

            if (count($children))

            {

                $this->_menu .= '<ul>';

 

                foreach ($children as $child)

                    $this->getCategory((int)$child['id_category'], (int)$id_lang, (int)$child['id_shop']);

 

                $this->_menu .= '</ul>';

            }

            $this->_menu .= '</li>';

        }

 

 

 

 

Please know that when we edited our blocktopmenu.php file we included all of this at the very end of the file. Also, when we uploaded the edited version and i tried to refresh my domain, it will leave me a blank page. So we had to delete everything because we need to have a working website.

 

Please help!

We love how you help thousands or millions of people

Link to comment
Share on other sites

to hide subcategory from block top menu it's necessary to modify the module php file

there is no other way to achieve this, it's because whole menu (also categories and subcategories) are generated in .php file - not in .tpl file as it is in other modules.

 Please Vekia read our last message we really need your help :)

Link to comment
Share on other sites

×
×
  • Create New...