Jump to content

Show number of products in sub-categories v1.5.3.1


Recommended Posts

Hi all,

 

I am using Prestashop version 1.5.3.1.

 

I have been searching and implementing code from topics on here to try and show the number of products in my sub-categories but I just cannot get it working.

 

There are a number of topics on here that I have followed, but the one that has got me closest to displaying anything relevant is when I added this line of code to /themes/default/category.tpl:

 

 

<h3><a href="{$link->getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'htmlall':'UTF-8'}" class="cat_name">{$subcategory.name|escape:'htmlall':'UTF-8'}</a> [{l s='%d products' sprintf=$subcategory}]

 

This line of code outputs the ID of the subcategory but not the number of products e.g. 34 products.

 

I am assuming that a function needs editing in one of the PHP files so that I can pass through the number of products in each subcategory, but I do not know how to do this. I have tried editing the

code in the /modules/blockcategories/blockcategories.php files as suggested in a lot of topics, but I can't get it working. I don't even know if the file I am editing is passing through the information that I need to category.tpl ?

 

I have seen this topic here: http://www.prestashop.com/forums/topic/190264-solved-show-number-of-products-next-to-categories/ but I think this applies to the category_tree_branch.tpl file. I require the number of products in the subcategories to be displayed in the cateogry.tpl file.

 

Please can someone help me fix this? It is driving me slightly insane.

 

Any help will be MUCH appreciated.

 

Kind regards

Jp

Link to comment
Share on other sites

In case anyone wants to know how to do this..........

 

 

Hi I got this working by doing the following in v1.5.3.1:

 

In /classes/Category.php find this code:

 

public function getSubCategories($id_lang, $active = true)
{
  if (!Validate::isBool($active))
   die(Tools::displayError());
 $groups = FrontController::getCurrentCustomerGroups();
 $sql_groups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');
 $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  SELECT c.*, cl.id_lang, cl.name, cl.description, cl.link_rewrite, cl.meta_title, cl.meta_keywords, cl.meta_description
  FROM `'._DB_PREFIX_.'category` c
  '.Shop::addSqlAssociation('category', 'c').'
  LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
   ON (c.`id_category` = cl.`id_category`
   AND `id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').')
  LEFT JOIN `'._DB_PREFIX_.'category_group` cg
   ON (cg.`id_category` = c.`id_category`)
  WHERE `id_parent` = '.(int)$this->id.'
   '.($active ? 'AND `active` = 1' : '').'
   AND cg.`id_group` '.$sql_groups.'
  GROUP BY c.`id_category`
  ORDER BY `level_depth` ASC, category_shop.`position` ASC
 ');
 foreach ($result as &$row)
 {
  $row['id_image'] = file_exists(_PS_CAT_IMG_DIR_.$row['id_category'].'.jpg') ? (int)$row['id_category'] : Language::getIsoById($id_lang).'-default';
  $row['legend'] = 'no picture';
 }
 return $result;
}

 

and change it to this:

 

public function getSubCategories($id_lang, $active = true)
{
  if (!Validate::isBool($active))
   die(Tools::displayError());
 $groups = FrontController::getCurrentCustomerGroups();
 $sql_groups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');
 $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  SELECT c.*, cl.id_lang, cl.name, cl.description, cl.link_rewrite, cl.meta_title, cl.meta_keywords, cl.meta_description
  FROM `'._DB_PREFIX_.'category` c
  '.Shop::addSqlAssociation('category', 'c').'
  LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
   ON (c.`id_category` = cl.`id_category`
   AND `id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').')
  LEFT JOIN `'._DB_PREFIX_.'category_group` cg
   ON (cg.`id_category` = c.`id_category`)
  WHERE `id_parent` = '.(int)$this->id.'
   '.($active ? 'AND `active` = 1' : '').'
   AND cg.`id_group` '.$sql_groups.'
  GROUP BY c.`id_category`
  ORDER BY `level_depth` ASC, category_shop.`position` ASC
 ');
 foreach ($result as &$row)
 {
  $row['id_image'] = file_exists(_PS_CAT_IMG_DIR_.$row['id_category'].'.jpg') ? (int)$row['id_category'] : Language::getIsoById($id_lang).'-default';
  $row['legend'] = 'no picture';
  $categ = new Category((int)$row['id_category'], (int)$id_lang);
  $row['nbproducts'] =$categ->getProducts(NULL, NULL, NULL,  NULL,  NULL, true);
 }
 return $result;
}

 

Basically you've added this code:

 

  $categ = new Category((int)$row['id_category'], (int)$id_lang);
  $row['nbproducts'] =$categ->getProducts(NULL, NULL, NULL,  NULL,  NULL, true);

 

Then in /themes/your_theme/category.tpl use the following code inside your foreach loop {foreach from=$subcategories item=subcategory} to get the number of products:

 

{$subcategory.nbproducts}

 

Worked a treat for me!!!

 

EDIT: PS I found this here: http://www.prestashop.com/forums/topic/154373-product-count-on-subcatergories/

Link to comment
Share on other sites

  • 1 year later...

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...