Jump to content

[SOLVED] HOW TO Display total product numbers at the end of each category name


Recommended Posts

Hello,

May i know how to display total product numbers at the end of each category name?

For example :
Product1 (4)
Product2 (9)
Product3 (12)

The number in the bracket is the total number of product for that particular category.
I'm using the latest version of Prestashop which is 1.5.5.0. I've tried searching for the answer in this forum but most of it are related to the older version of prestashop.
I'm hoping that someone has solved this problem and has a good heart in sharing the success.

Best regards
Shaamt.

Link to comment
Share on other sites

hope that you're talking about blockcategories module.

 

open: /modules/blockcategories/blockcategories.php

instead of this:

    public function getTree($resultParents, $resultIds, $maxDepth, $id_category = 1, $currentDepth = 0)
    {
        global $link;

        $children = array();
        if (isset($resultParents[$id_category]) AND sizeof($resultParents[$id_category]) AND ($maxDepth == 0 OR $currentDepth < $maxDepth))
            foreach ($resultParents[$id_category] as $subcat)
                $children[] = $this->getTree($resultParents, $resultIds, $maxDepth, $subcat['id_category'], $currentDepth + 1);
        if (!isset($resultIds[$id_category]))
            return false;
        return array('id' => $id_category, 'link' => $link->getCategoryLink($id_category, $resultIds[$id_category]['link_rewrite']),
                     'name' => $resultIds[$id_category]['name'], 'desc'=> $resultIds[$id_category]['description'],
                     'children' => $children);
    }

use:

 

   public function getTree($resultParents, $resultIds, $maxDepth, $id_category = 1, $currentDepth = 0)
    {
        global $link;

        $children = array();
        if (isset($resultParents[$id_category]) AND sizeof($resultParents[$id_category]) AND ($maxDepth == 0 OR $currentDepth < $maxDepth))
            foreach ($resultParents[$id_category] as $subcat)
                $children[] = $this->getTree($resultParents, $resultIds, $maxDepth, $subcat['id_category'], $currentDepth + 1);
        if (!isset($resultIds[$id_category]))
            return false;
$productsCount = (int)Db::getInstance()->getValue('SELECT COUNT(*) FROM '._DB_PREFIX_.'category_product WHERE id_category = '. $id_category);
        return array('id' => $id_category, 'link' => $link->getCategoryLink($id_category, $resultIds[$id_category]['link_rewrite']),
                     'name' => $resultIds[$id_category]['name'], 'desc'=> $resultIds[$id_category]['description'],
                     'children' => $children, 'productsCount'=>$productsCount);
    }

then in: /modules/blockcategories/category-tree-branch.tpl

instead of:

<a href="{$node.link}" {if isset($currentCategoryId) && ($node.id == $currentCategoryId)}class="selected"{/if} title="{$node.desc|escape:html:'UTF-8'}">{$node.name|escape:html:'UTF-8'}</a>

use:

<a href="{$node.link}" {if isset($currentCategoryId) && ($node.id == $currentCategoryId)}class="selected"{/if} title="{$node.desc|escape:html:'UTF-8'}">{$node.name|escape:html:'UTF-8'} ({$node.productsCount})</a>
  • Sad 1
Link to comment
Share on other sites

Thanks for your reply.

 

I've tried it before but but failed because the Categories Block are not showing any product categories.

It seem that the replaceable code is not the same as mine. The code that you gave me for blockcategories.php is:

 

public function getTree($resultParents, $resultIds, $maxDepth, $id_category = 1, $currentDepth = 0)
    {
        global $link;

        $children = array();
        if (isset($resultParents[$id_category]) AND sizeof($resultParents[$id_category]) AND ($maxDepth == 0 OR $currentDepth < $maxDepth))
            foreach ($resultParents[$id_category] as $subcat)
                $children[] = $this->getTree($resultParents, $resultIds, $maxDepth, $subcat['id_category'], $currentDepth + 1);
        if (!isset($resultIds[$id_category]))
            return false;z
        
return array('id' => $id_category, 'link' => $link->getCategoryLink($id_category, $resultIds[$id_category]['link_rewrite']),
                     'name' => $resultIds[$id_category]['name'], 'desc'=> $resultIds[$id_category]['description'],
                     'children' => $children);
    }

 

 

but mine (Prestashop 1.5.5.0) is:

 

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

        $children = array();
        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]))
            return false;
        $return = array('id' => $id_category, 'link' => $this->context->link->getCategoryLink($id_category, $resultIds[$id_category]['link_rewrite']),
                     'name' => $resultIds[$id_category]['name'], 'desc'=> $resultIds[$id_category]['description'],
                     'children' => $children);
        return $return;
    }

 

In category-tree-branch.tpl your code is:

 

<a href="{$node.link}" {if isset($currentCategoryId) && ($node.id == $currentCategoryId)}class="selected"{/if} title="{$node.desc|escape:html:'UTF-8'}">{$node.name|escape:html:'UTF-8'}</a>

 

but mine (Prestashop 1.5.5.0) are

 

<a href="{$node.link|escape:'htmlall':'UTF-8'}" {if isset($currentCategoryId) && $node.id == $currentCategoryId}class="selected"{/if}
title="{$node.desc|strip_tags|trim|escape:'htmlall':'UTF-8'}">{$node.name|escape:'htmlall':'UTF-8'}</a>

 

Are you able to synchronized the modified code with the code as in prestashop 1.5.5.0?

Anyway thanks for your help.

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

it is for prestashop 1.5.5.0

just use code that i suggested, have you tried it?

 

replace getTree function with mine, and also .tpl file code with mine

 

Yes. I've tried it and it display a blank categories name as shown here.

PrestaShopForum1_zps67ae3e6c.jpg

 

instead of

 

PrestaShopForum2_zps1cdce5a7.jpg

Link to comment
Share on other sites

hello

 

i prepared own module for this, take a look:

I've tried your module and it looks really great. But the module include the "disabled" products in the calculation. Could you modify the module a bit by only calculating the "enabled" product. Thanks for your help.

Link to comment
Share on other sites

I've tried your module and it looks really great. But the module include the "disabled" products in the calculation. Could you modify the module a bit by only calculating the "enabled" product. Thanks for your help.

hello

 

ohh you've got aboslutely right, sorry for this :) it will be available to download ASAP. i will let you know!

Link to comment
Share on other sites

  • 3 months later...
  • 1 year later...
  • 2 weeks later...

Hi guys

 

I agree with Click_303, this Vekia's modules works 95% except that if the parent category does not have any products and the sub-categories do have products, the sub-categories are displaying the total number of products correctly but the parent category display "0".

 

Vekia, can you please change the code so that if the number of products count is "0" then do not display anything????

 

This fix will make the module work correctly and more people will be in a position to use it!

 

Thanks

Regards

Greg

Link to comment
Share on other sites

  • 1 year later...
×
×
  • Create New...