Jump to content

Display number of products near category name in PS 1.7 - Category tree module


Dema7

Recommended Posts

Hi everyone!

I'm trying to display the number of products near the category name in the left sidebar using the module Category Tree.

Here is an example of what i would like to obtain:

• Category (50)
- - Subcategory (27)
- - Subcategory (3)
- - Subcategory (12)
- - Subcategory (8)

I'm using the code found here https://blog.prestatuts.com/show-number-of-products-next-to-each-category-in-category-tree-menu/

But i'm facing two problems: the main category shows 0 products but I would like to show the sum of all products in subcategories, this is what I got:

• Category (0)
- - Subcategory (27)
- - Subcategory (3)
- - Subcategory (12)
- - Subcategory (8)

The worst thing is that when the code is implemented the loading of the category page becomes extremely slow, like 20 seconds everytime.

This is the edited file modules/ps_categorytree/ps_categorytree.php

<?php
/**
 * Copyright since 2007 PrestaShop SA and Contributors
 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License 3.0 (AFL-3.0)
 * that is bundled with this package in the file LICENSE.md.
 * It is also available through the world-wide-web at this URL:
 * https://opensource.org/licenses/AFL-3.0
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to [email protected] so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to https://devdocs.prestashop.com/ for more information.
 *
 * @author    PrestaShop SA and Contributors <[email protected]>
 * @copyright Since 2007 PrestaShop SA and Contributors
 * @license   https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
 */
if (!defined('_PS_VERSION_')) {
    exit;
}

use PrestaShop\PrestaShop\Core\Module\WidgetInterface;

class Ps_CategoryTree extends Module implements WidgetInterface
{
    /**
     * @var string Name of the module running on PS 1.6.x. Used for data migration.
     */
    const PS_16_EQUIVALENT_MODULE = 'blockcategories';

    public function __construct()
    {
        $this->name = 'ps_categorytree';
        $this->tab = 'front_office_features';
        $this->version = '2.0.2';
        $this->author = 'PrestaShop';

        $this->bootstrap = true;
        parent::__construct();

        $this->displayName = $this->getTranslator()->trans('Category tree links', [], 'Modules.Categorytree.Admin');
        $this->description = $this->getTranslator()->trans('Help navigation on your store, show your visitors current category and subcategories.', [], 'Modules.Categorytree.Admin');
        $this->ps_versions_compliancy = ['min' => '1.7.1.0', 'max' => _PS_VERSION_];
    }

    public function install()
    {
        // If the PS 1.6 module wasn't here, set the default values
        if (!$this->uninstallPrestaShop16Module()) {
            Configuration::updateValue('BLOCK_CATEG_MAX_DEPTH', 4);
            Configuration::updateValue('BLOCK_CATEG_ROOT_CATEGORY', 1);
        }

        return parent::install()
            && $this->registerHook('displayLeftColumn')
        ;
    }

    /**
     * Migrate data from 1.6 equivalent module (if applicable), then uninstall
     */
    public function uninstallPrestaShop16Module()
    {
        if (!Module::isInstalled(self::PS_16_EQUIVALENT_MODULE)) {
            return false;
        }
        $oldModule = Module::getInstanceByName(self::PS_16_EQUIVALENT_MODULE);
        if ($oldModule) {
            // This closure calls the parent class to prevent data to be erased
            // It allows the new module to be configured without migration
            $parentUninstallClosure = function () {
                return parent::uninstall();
            };
            $parentUninstallClosure = $parentUninstallClosure->bindTo($oldModule, get_class($oldModule));
            $parentUninstallClosure();
        }

        return true;
    }

    public function uninstall()
    {
        if (!parent::uninstall() ||
            !Configuration::deleteByName('BLOCK_CATEG_MAX_DEPTH') ||
            !Configuration::deleteByName('BLOCK_CATEG_ROOT_CATEGORY')) {
            return false;
        }

        return true;
    }

    public function getContent()
    {
        $output = '';
        if (Tools::isSubmit('submitBlockCategories')) {
            $maxDepth = (int) (Tools::getValue('BLOCK_CATEG_MAX_DEPTH'));
            if ($maxDepth < 0) {
                $output .= $this->displayError($this->getTranslator()->trans('Maximum depth: Invalid number.', [], 'Admin.Notifications.Error'));
            } else {
                Configuration::updateValue('BLOCK_CATEG_MAX_DEPTH', (int) $maxDepth);
                Configuration::updateValue('BLOCK_CATEG_SORT_WAY', Tools::getValue('BLOCK_CATEG_SORT_WAY'));
                Configuration::updateValue('BLOCK_CATEG_SORT', Tools::getValue('BLOCK_CATEG_SORT'));
                Configuration::updateValue('BLOCK_CATEG_ROOT_CATEGORY', Tools::getValue('BLOCK_CATEG_ROOT_CATEGORY'));

                //$this->_clearBlockcategoriesCache();

                Tools::redirectAdmin(AdminController::$currentIndex . '&configure=' . $this->name . '&token=' . Tools::getAdminTokenLite('AdminModules') . '&conf=6');
            }
        }

        return $output . $this->renderForm();
    }

    private function getCategories($category)
    {
        $range = '';
        $maxdepth = Configuration::get('BLOCK_CATEG_MAX_DEPTH');
        if (Validate::isLoadedObject($category)) {
            if ($maxdepth > 0) {
                $maxdepth += $category->level_depth;
            }
            $range = 'AND nleft >= ' . (int) $category->nleft . ' AND nright <= ' . (int) $category->nright;
        }

        $resultIds = [];
        $resultParents = [];
        $result = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->executeS('
			SELECT c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite
			FROM `' . _DB_PREFIX_ . 'category` c
			INNER JOIN `' . _DB_PREFIX_ . 'category_lang` cl ON (c.`id_category` = cl.`id_category` AND cl.`id_lang` = ' . (int) $this->context->language->id . Shop::addSqlRestrictionOnLang('cl') . ')
			INNER JOIN `' . _DB_PREFIX_ . 'category_shop` cs ON (cs.`id_category` = c.`id_category` AND cs.`id_shop` = ' . (int) $this->context->shop->id . ')
			WHERE (c.`active` = 1 OR c.`id_category` = ' . (int) Configuration::get('PS_HOME_CATEGORY') . ')
			AND c.`id_category` != ' . (int) Configuration::get('PS_ROOT_CATEGORY') . '
			' . ((int) $maxdepth != 0 ? ' AND `level_depth` <= ' . (int) $maxdepth : '') . '
			' . $range . '
			AND c.id_category IN (
				SELECT id_category
				FROM `' . _DB_PREFIX_ . 'category_group`
				WHERE `id_group` IN (' . pSQL(implode(', ', Customer::getGroupsStatic((int) $this->context->customer->id))) . ')
			)
			ORDER BY `level_depth` ASC, ' . (Configuration::get('BLOCK_CATEG_SORT') ? 'cl.`name`' : 'cs.`position`') . ' ' . (Configuration::get('BLOCK_CATEG_SORT_WAY') ? 'DESC' : 'ASC'));
        foreach ($result as &$row) {
            $resultParents[$row['id_parent']][] = &$row;
            $resultIds[$row['id_category']] = &$row;
        }

        return $this->getTree($resultParents, $resultIds, $maxdepth, ($category ? $category->id : null));
    }

    public function getTree($resultParents, $resultIds, $maxDepth, $id_category = null, $currentDepth = 0)
    {
        if (is_null($id_category)) {
            $id_category = $this->context->shop->getCategory();
        }

        $children = [];

        if (isset($resultParents[$id_category]) && count($resultParents[$id_category]) && ($maxDepth == 0 || $currentDepth < $maxDepth)) {
            foreach ($resultParents[$id_category] as $subcat) {
                $children[] = $this->getTree($resultParents, $resultIds, $maxDepth, $subcat['id_category'], $currentDepth + 1);
            }
        }

        if (isset($resultIds[$id_category])) {
            $link = $this->context->link->getCategoryLink($id_category, $resultIds[$id_category]['link_rewrite']);
            $name = $resultIds[$id_category]['name'];
            $desc = $resultIds[$id_category]['description'];
        } else {
            $link = $name = $desc = '';
        }

        return [
            'id' => $id_category,
            'link' => $link,
            'name' => $name,
            'desc' => $desc,
            'children' => $children,
        ];
    }

    public function renderForm()
    {
        $fields_form = [
            'form' => [
                'legend' => [
                    'title' => $this->getTranslator()->trans('Settings', [], 'Admin.Global'),
                    'icon' => 'icon-cogs',
                ],
                'input' => [
                    [
                        'type' => 'radio',
                        'label' => $this->getTranslator()->trans('Category root', [], 'Modules.Categorytree.Admin'),
                        'name' => 'BLOCK_CATEG_ROOT_CATEGORY',
                        'hint' => $this->getTranslator()->trans('Select which category is displayed in the block. The current category is the one the visitor is currently browsing.', [], 'Modules.Categorytree.Admin'),
                        'values' => [
                            [
                                'id' => 'home',
                                'value' => 0,
                                'label' => $this->getTranslator()->trans('Home category', [], 'Modules.Categorytree.Admin'),
                            ],
                            [
                                'id' => 'current',
                                'value' => 1,
                                'label' => $this->getTranslator()->trans('Current category', [], 'Modules.Categorytree.Admin'),
                            ],
                            [
                                'id' => 'parent',
                                'value' => 2,
                                'label' => $this->getTranslator()->trans('Parent category', [], 'Modules.Categorytree.Admin'),
                            ],
                            [
                                'id' => 'current_parent',
                                'value' => 3,
                                'label' => $this->getTranslator()->trans('Current category, unless it has no subcategories, in which case the parent category of the current category is used', [], 'Modules.Categorytree.Admin'),
                            ],
                        ],
                    ],
                    [
                        'type' => 'text',
                        'label' => $this->getTranslator()->trans('Maximum depth', [], 'Modules.Categorytree.Admin'),
                        'name' => 'BLOCK_CATEG_MAX_DEPTH',
                        'desc' => $this->getTranslator()->trans('Set the maximum depth of category sublevels displayed in this block (0 = infinite).', [], 'Modules.Categorytree.Admin'),
                    ],
                    [
                        'type' => 'radio',
                        'label' => $this->getTranslator()->trans('Sort', [], 'Admin.Actions'),
                        'name' => 'BLOCK_CATEG_SORT',
                        'values' => [
                            [
                                'id' => 'name',
                                'value' => 1,
                                'label' => $this->getTranslator()->trans('By name', [], 'Admin.Global'),
                            ],
                            [
                                'id' => 'position',
                                'value' => 0,
                                'label' => $this->getTranslator()->trans('By position', [], 'Admin.Global'),
                            ],
                        ],
                    ],
                    [
                        'type' => 'radio',
                        'label' => $this->getTranslator()->trans('Sort order', [], 'Admin.Actions'),
                        'name' => 'BLOCK_CATEG_SORT_WAY',
                        'values' => [
                            [
                                'id' => 'name',
                                'value' => 1,
                                'label' => $this->getTranslator()->trans('Descending', [], 'Admin.Global'),
                            ],
                            [
                                'id' => 'position',
                                'value' => 0,
                                'label' => $this->getTranslator()->trans('Ascending', [], 'Admin.Global'),
                            ],
                        ],
                    ],
                ],
                'submit' => [
                    'title' => $this->getTranslator()->trans('Save', [], 'Admin.Actions'),
                ],
            ],
        ];

        $helper = new HelperForm();
        $helper->show_toolbar = false;
        $helper->table = $this->table;
        $helper->submit_action = 'submitBlockCategories';
        $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name;
        $helper->token = Tools::getAdminTokenLite('AdminModules');
        $helper->tpl_vars = [
            'fields_value' => $this->getConfigFieldsValues(),
        ];

        return $helper->generateForm([$fields_form]);
    }

    public function getConfigFieldsValues()
    {
        return [
            'BLOCK_CATEG_MAX_DEPTH' => Tools::getValue('BLOCK_CATEG_MAX_DEPTH', Configuration::get('BLOCK_CATEG_MAX_DEPTH')),
            'BLOCK_CATEG_SORT_WAY' => Tools::getValue('BLOCK_CATEG_SORT_WAY', Configuration::get('BLOCK_CATEG_SORT_WAY')),
            'BLOCK_CATEG_SORT' => Tools::getValue('BLOCK_CATEG_SORT', Configuration::get('BLOCK_CATEG_SORT')),
            'BLOCK_CATEG_ROOT_CATEGORY' => Tools::getValue('BLOCK_CATEG_ROOT_CATEGORY', Configuration::get('BLOCK_CATEG_ROOT_CATEGORY')),
        ];
    }

    public function setLastVisitedCategory()
    {
        if (method_exists($this->context->controller, 'getCategory') && ($category = $this->context->controller->getCategory())) {
            $this->context->cookie->last_visited_category = $category->id;
        } elseif (method_exists($this->context->controller, 'getProduct') && ($product = $this->context->controller->getProduct())) {
            if (!isset($this->context->cookie->last_visited_category)
                || !Product::idIsOnCategoryId($product->id, [['id_category' => $this->context->cookie->last_visited_category]])
                || !Category::inShopStatic($this->context->cookie->last_visited_category, $this->context->shop)
            ) {
                $this->context->cookie->last_visited_category = (int) $product->id_category_default;
            }
        }
    }

    public function renderWidget($hookName = null, array $configuration = [])
    {
        $this->setLastVisitedCategory();
        $this->smarty->assign($this->getWidgetVariables($hookName, $configuration));

        return $this->fetch('module:ps_categorytree/views/templates/hook/ps_categorytree.tpl');
    }

    public function getWidgetVariables($hookName = null, array $configuration = [])
    {
        $category = new Category((int) Configuration::get('PS_HOME_CATEGORY'), $this->context->language->id);

        if (Configuration::get('BLOCK_CATEG_ROOT_CATEGORY') && isset($this->context->cookie->last_visited_category) && $this->context->cookie->last_visited_category) {
            $category = new Category($this->context->cookie->last_visited_category, $this->context->language->id);
            if (Configuration::get('BLOCK_CATEG_ROOT_CATEGORY') == 2 && !$category->is_root_category && $category->id_parent) {
                $category = new Category($category->id_parent, $this->context->language->id);
            } elseif (Configuration::get('BLOCK_CATEG_ROOT_CATEGORY') == 3 && !$category->is_root_category && !$category->getSubCategories($category->id, true)) {
                $category = new Category($category->id_parent, $this->context->language->id);
            }
        }

        $categories = $this->getCategories($category);
return [
'categories' => $categories,
'c_tree_path' => isset($categories['children']) && count($categories['children']) && method_exists($this->context->controller, 'getCategory') && ($curr_category = $this->context->controller->getCategory()) ? self::getTreePath($categories['children'], $curr_category->id) : false,
'currentCategory' => $category->id,
];
    }
	
	public static function getTreePath($categories, $id, array $path = [])
{
    foreach ($categories as $cate) {
        if ($cate['id'] == $id){
            if(is_array($cate['children']) && count($cate['children']))
                $path[] = $cate['id'];
            return $path;
        }
        $path[] = $cate['id'];
        if(is_array($cate['children']) && count($cate['children'])) {
            if ($result = self::getTreePath($cate['children'], $id, $path))
                return $result;
        }
        array_pop($path);
    }
    return false;
}
	
public static function countProductInCat($idCategory){
$category=new Category((int)$idCategory);
$id_lang=Context::getContext()->language->id;
$productCount=$category->getProducts($id_lang, 1, 10000, null, null, false);
return $productCount;
}
	
	
}

And this is the edited (and overrided) file modules/ps_categorytree/views/templates/hook/ps_categorytree.tpl

{function name="categories" nodes=[] depth=0}
  {strip}
    {if $nodes|count}
	
      <ul class="category-sub-menu">
        {foreach from=$nodes item=node}
		
		{assign var=category_products value=Module::getInstanceByName('ps_categorytree')->countProductInCat($node.id)}
		
          <li data-depth="{$depth}"  class="{if (isset($category) && is_array($category) && isset($category.id) && $category.id==$node.id) || (isset($id_category_current) && $id_category_current==$node.id)} current_cate {/if}">
            {if $depth===0}
              <a href="{$node.link}">{$node.name} ({count($category_products)})</a>
              {if $node.children}
                <span class="collapse-icons{if $c_tree_path && !in_array($node.id, $c_tree_path)} collapsed{/if}" data-toggle="collapse" data-target="#exCollapsingNavbar{$node.id}" aria-expanded="{if $c_tree_path && in_array($node.id, $c_tree_path)}true{else}false{/if}">
                  <i class="fa fa-angle-down add" aria-hidden="true"></i>
                  <i class="fa fa-angle-up remove" aria-hidden="true"></i>
                </span>
                <div class="collapse{if $c_tree_path && in_array($node.id, $c_tree_path)} show{/if}" id="exCollapsingNavbar{$node.id}">
                  {categories nodes=$node.children depth=$depth+1}
                </div>
              {/if}
            {else}
              <a class="category-sub-link" href="{$node.link}">{$node.name} ({count($category_products)})</a>
              {if $node.children}
                <span class="arrows{if $c_tree_path && !in_array($node.id, $c_tree_path)} collapsed{/if} collapse-icons" data-toggle="collapse" data-target="#exCollapsingNavbar{$node.id}" aria-expanded="{if $c_tree_path && in_array($node.id, $c_tree_path)}true{else}false{/if}">
                   <i class="fa fa-angle-down add" aria-hidden="true"></i>
                   <i class="fa fa-angle-up remove" aria-hidden="true"></i>
                </span>
                <div class="collapse{if $c_tree_path && in_array($node.id, $c_tree_path)} show{/if}" id="exCollapsingNavbar{$node.id}">
                  {categories nodes=$node.children depth=$depth+1}
                </div>
              {/if}
            {/if}
          </li>
        {/foreach}
      </ul>
    {/if}
  {/strip}
{/function}


{if $categories.children|count}

{assign var=category_products_dva value=Module::getInstanceByName('ps_categorytree')->countProductInCat($categories.id)}

  <div class="block block-toggle block-categories block-links js-block-toggle">
    <h5 class="block-title"><span><a href="{$categories.link nofilter}">{$categories.name} ({count($category_products_dva)})</a></span> </h5>
    <div class="category-top-menu block-content">
      {categories nodes=$categories.children}
    </div>
  </div>
{/if}

If anyone can help me I would really appreciate, this is an important function for my client and I can't get what I'm doing wrong.

Thank you very much!

Link to comment
Share on other sites

Indeed, that's not really optimized. 

To optimize the speed, change this 

$productCount=$category->getProducts($id_lang, 1, 10000, null, null, false);

by this

$productCount=$category->getProducts($id_lang, 1, 10000, null, null, true);

The last param that I put to true, indicates that we only want to get the total of products instead of the whole products object. What you were doing was getting all the products objects of each of your categories. (Including, name, description, price, and a ton of information). Now, we only get total number. Wayyyy faster.

 

Now, to fix the top level category number :

public static function countProductInCat($idCategory){
	$category=new Category((int)$idCategory);
	$id_lang=Context::getContext()->language->id;
	$id_shop = Context::getContext()->shop->id;
	$productCount = 0;
	if ($idCategory == TOP_LEVEL_CATEGORY_ID) {
		$children = $category->getChildren($idCategory, $id_lang, true, $id_shop);
		foreach ($children as $child) {
			$total += $child->getProducts($id_lang, 1, 10000, null, null, false);
		}
	} else {
		$productCount=$category->getProducts($id_lang, 1, 10000, null, null, false);
	}
	return $productCount;
}

For the last piece of code, I didn't test it, sorry if it's not working.

  • Sad 1
Link to comment
Share on other sites

Hi Tom,

thank you very much for you reply!

At first didn't try the code for the top category number but only tried to see if changing that value from false to true had an impact on loading speed.
The loading time is still very very slow, i would say the same, and there is now another problem: all the categories indicate a quantity of products equivalent to 1.

• Category (1)
- - Subcategory (1)
- - Subcategory (1)
- - Subcategory (1)
- - Subcategory (1)

If I put the value back to false the subcategories have the right number of products.

Then I tried to implement directly the code for the top category number (and left the "false" value as you did), the top category still shows 0 products and still If I change the value to true all the categories shows 1 as result.

Thank you again for your time!

Davide

 

Link to comment
Share on other sites

  • 3 weeks later...
On 6/27/2022 at 4:58 PM, Tom Girou said:

Indeed, that's not really optimized. 

To optimize the speed, change this 

$productCount=$category->getProducts($id_lang, 1, 10000, null, null, false);

by this

$productCount=$category->getProducts($id_lang, 1, 10000, null, null, true);

The last param that I put to true, indicates that we only want to get the total of products instead of the whole products object. What you were doing was getting all the products objects of each of your categories. (Including, name, description, price, and a ton of information). Now, we only get total number. Wayyyy faster.

 

Now, to fix the top level category number :

public static function countProductInCat($idCategory){
	$category=new Category((int)$idCategory);
	$id_lang=Context::getContext()->language->id;
	$id_shop = Context::getContext()->shop->id;
	$productCount = 0;
	if ($idCategory == TOP_LEVEL_CATEGORY_ID) {
		$children = $category->getChildren($idCategory, $id_lang, true, $id_shop);
		foreach ($children as $child) {
			$total += $child->getProducts($id_lang, 1, 10000, null, null, false);
		}
	} else {
		$productCount=$category->getProducts($id_lang, 1, 10000, null, null, false);
	}
	return $productCount;
}

For the last piece of code, I didn't test it, sorry if it's not working.

Hi Tom, do you think there is a solution?

Thank you very much and if you don't have time don't worry to reply, I don't want to bother you 🙂

 

 

Link to comment
Share on other sites

6 hours ago, Tom Girou said:

Hi,

Honestly I thought what I wrote would work. So without debugging your own code I can't help you more, sorry.

Hi Tom, don't worry and thank you anyway!!

stifler97 just solved the problem with the module he published here!

Thank you again and have a nice day!

 

 

Link to comment
Share on other sites

1 hour ago, stifler97 said:

Hi @Dema7

I just upgraded the ps_categorytree module.

Check this out.

 

Hi stifler97, thank you a lot, I installed the module and overrided the ps_categorytree.tpl in my theme without issues!

I also applied this edit to made the categories to expand and highlight the current one https://www.sunnytoo.com/8222/how-to-set-the-category-tree-links-module-to-expand-the-brunch-containing-current-category-in-prestashop-1-7

You can see it working in the attached screenshot!!

Can I ask you another thing? Do you think it is possible to have the root categories to show the sum of all products in subcategories? Or do I need to manual assign the product to all the categories, for example "Miscellaneous" + "Fossil" + "Ammonite"?

Thank you again!!!

 

 

 

 

 

 

Screenshot 2022-07-20 at 14.50.14.png

Link to comment
Share on other sites

I could not understand what you need. The only thing that I know is that, if the products are not assigned to the root category, they are not counted. Otherwise they get counted. So I see that all of your root categories have (17) products. Is that right?
Can you share the shop url so I can check better?

Edited by stifler97 (see edit history)
Link to comment
Share on other sites

2 hours ago, stifler97 said:

I could not understand what you need. The only think that I know is that, if the products are not assigned to the root category, they are not counted. Other wise they get counted. So I see that all of your root categories have (17) products. Is that right?
Can you share the shop url so I can check better?

Hi,

you are right, I didn't realize all the root had 17 products...

This is an example page https://thebeautyintherocks.com/en/576-amethyst-cameos

This is the code I used in the override of ps_categorytree.tpl

{function name="categories" nodes=[] depth=0}
  {strip}
    {if $nodes|count}
      <ul class="category-sub-menu">
        {foreach from=$nodes item=node}
          <li data-depth="{$depth}"  class="{if (isset($category) && is_array($category) && isset($category.id) && $category.id==$node.id) || (isset($id_category_current) && $id_category_current==$node.id)} current_cate {/if}">
            {if $depth===0}
              <a href="{$node.link}">{$node.name} {if $categories.numberOfProducts}({$categories.numberOfProducts}){/if}</a>
              {if $node.children}
                <span class="collapse-icons{if $c_tree_path && !in_array($node.id, $c_tree_path)} collapsed{/if}" data-toggle="collapse" data-target="#exCollapsingNavbar{$node.id}" aria-expanded="{if $c_tree_path && in_array($node.id, $c_tree_path)}true{else}false{/if}">
                  <i class="fa fa-angle-down add" aria-hidden="true"></i>
                  <i class="fa fa-angle-up remove" aria-hidden="true"></i>
                </span>
                <div class="collapse{if $c_tree_path && in_array($node.id, $c_tree_path)} show{/if}" id="exCollapsingNavbar{$node.id}">
                  {categories nodes=$node.children depth=$depth+1}
                </div>
              {/if}
            {else}
              <a class="category-sub-link" href="{$node.link}">{$node.name} {if $node.numberOfProducts}({$node.numberOfProducts}){/if}</a>
              {if $node.children}
                <span class="arrows{if $c_tree_path && !in_array($node.id, $c_tree_path)} collapsed{/if} collapse-icons" data-toggle="collapse" data-target="#exCollapsingNavbar{$node.id}" aria-expanded="{if $c_tree_path && in_array($node.id, $c_tree_path)}true{else}false{/if}">
                   <i class="fa fa-angle-down add" aria-hidden="true"></i>
                   <i class="fa fa-angle-up remove" aria-hidden="true"></i>
                </span>
                <div class="collapse{if $c_tree_path && in_array($node.id, $c_tree_path)} show{/if}" id="exCollapsingNavbar{$node.id}">
                  {categories nodes=$node.children depth=$depth+1}
                </div>
              {/if}
            {/if}
          </li>
        {/foreach}
      </ul>
    {/if}
  {/strip}
{/function}

{if $categories.children|count}
  <div class="block block-toggle block-categories block-links js-block-toggle">
    <h5 class="block-title"><span><a href="{$categories.link nofilter}">{$categories.name}</a></span> </h5>
    <div class="category-top-menu block-content">
      {categories nodes=$categories.children}
    </div>
  </div>
{/if}

Thank you!!

Davide

 

Link to comment
Share on other sites

Hey David. Nice to meet you. Beside the issues you have, I think you missed two points:

Instead of this:

<a href="{$node.link}">{$node.name} {if $categories.numberOfProducts}({$categories.numberOfProducts}){/if}</a>

write this:

<a href="{$node.link}">{$node.name} {if $node.numberOfProducts}({$node.numberOfProducts}){/if}</a>

 

And also instead of this:

<h5 class="block-title"><span><a href="{$categories.link nofilter}">{$categories.name}</a></span> </h5>

Write this:

<h5 class="block-title"><span><a href="{$categories.link nofilter}">{$categories.name} {if $categories.numberOfProducts}({$categories.numberOfProducts}){/if}</a></span> </h5>

 

Link to comment
Share on other sites

Another thing is, it is always recommended to put your products in a category tree. If their tree of category gets disconnected from the root category, it will be bad for SEO and also for search filters within your website. Imaging some one filters products of the root category only, and then the products which are assigned to the next level of children categories do no show up in that filter, and that is not a bug, that is a bad usage of categories and the app. You better fix your catalog data before it gets larger and larger.

Link to comment
Share on other sites

14 hours ago, stifler97 said:

Hey David. Nice to meet you. Beside the issues you have, I think you missed two points:

Instead of this:

<a href="{$node.link}">{$node.name} {if $categories.numberOfProducts}({$categories.numberOfProducts}){/if}</a>

write this:

<a href="{$node.link}">{$node.name} {if $node.numberOfProducts}({$node.numberOfProducts}){/if}</a>

 

And also instead of this:

<h5 class="block-title"><span><a href="{$categories.link nofilter}">{$categories.name}</a></span> </h5>

Write this:

<h5 class="block-title"><span><a href="{$categories.link nofilter}">{$categories.name} {if $categories.numberOfProducts}({$categories.numberOfProducts}){/if}</a></span> </h5>

 

Thank you,
now it is working fine!! And as you suggested I assigned all products to their parent categories...

I hope your work will help someone else!

Again, thank you and have a nice day!

Davide

Link to comment
Share on other sites

You are welcome Davide. I am glad I could help you.
So please take a time to like my posts about the free module. This helps others to see the content. Thanks. I have opened a feature request in Github for this module. The core developers must accept this and then every one can use this in later versions by default.

This is the link to the feature request in Github: https://github.com/PrestaShop/PrestaShop/issues/29129

This is the link of the module post:

 

Link to comment
Share on other sites

On 7/21/2022 at 4:36 PM, stifler97 said:

You are welcome Davide. I am glad I could help you.
So please take a time to like my posts about the free module. This helps others to see the content. Thanks. I have opened a feature request in Github for this module. The core developers must accept this and then every one can use this in later versions by default.

This is the link to the feature request in Github: https://github.com/PrestaShop/PrestaShop/issues/29129

This is the link of the module post:

 

 

Hi stifler97, sorry for the late response, I liked the post and hope they will integrate the edit you did on the next release!
Thank you again for your work!

Have a nice day,
Davide

 

 

  • Thanks 1
Link to comment
Share on other sites

  • 8 months later...

Hi stifler97,
I hope you are well!

I tryed to port the edit to the new version of the module 2.0.3 on Prestashop version 8.0.3 but I can't get it right...

Do you think you can take a look at it? You can find the new module with my attempt attached here 🙂

Thank you very much in advance!!! Have a nice day,
Davide

ps_categorytree.zip

Edited by Dema7 (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...