Jump to content

( Simple loop to list categories and subcategories and number of products in subcategories


Recommended Posts




I'm trying to add a piece of code to my homepage to display a list of all the categories and under each category it's subcategories with a number at side with the amount of products in it.

Something like this:



- Category 1

- - Sub category (42)

- - Sub category (13)


- Category 3

- - Sub category (17)

- - Sub category (33)



Although it may be simple for some, I've been searching for 2 ways for a way to do it, but without result. Tried modifying the CategoryController.php override but without success (no result in homepage, but then I realized the Category page was actually crashing).


The code I have now shows the main categories, but not the sub categories.

{foreach from=$categories item=subcategory name=homeCategories}
		<div class="subcategory-image">
			<a href="{$link->getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'html':'UTF-8'}"
			   title="{$subcategory.name|escape:'html':'UTF-8'}" class="img">
				{if $subcategory.id_image}
					<img class="replace-2x"
						 src="{$link->getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium_default')|escape:'html':'UTF-8'}"
						 alt="" width="{$mediumSize.width|escape:'htmlall':'UTF-8'}" height="{$mediumSize.height|escape:'htmlall':'UTF-8'}"/>
					<img class="replace-2x" src="{$img_cat_dir|escape:'htmlall':'UTF-8'}{$lang_iso|escape:'htmlall':'UTF-8'}-default-medium_default.jpg"
						 alt="" width="{$mediumSize.width|escape:'htmlall':'UTF-8'}" height="{$mediumSize.height|escape:'htmlall':'UTF-8'}"/>
			<a class="subcategory-name" href="{$link->getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'html':'UTF-8'}">{$subcategory.name|truncate:25:'...'|escape:'html':'UTF-8'}</a>

		<ul class="sub-menu">
			<!-- It loops with a foreach -->
			<li><a href="#">Sub-category <span class="sub-counter">60 (number of products in this subcategory)</span></a></li>

Any kind sould to help me out, pretty please? :)


Thanks in advance.



Edited by cableguy1115 (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...