Jump to content

Category page. Display sub & sub sub categories


player.c64

Recommended Posts

Hi everybody,

I need to display on main category page all sub categories including sub sub categories See on attached visual.

pidoco---category-layout.png

I know it can be done by manipulating category.tpl. Trying to use syntax below but doesn't work

 {if isset($categoriesTree.children)}
  {foreach $categoriesTree.children as $child}
{if $child@last}
 {include file="$tpl_dir./category-tree-branch.tpl" node=$child last='true'}
{else}
 {include file="$tpl_dir./category-tree-branch.tpl" node=$child}
{/if}
  {/foreach}
 {/if}

 

Many thanks for all help.

Link to comment
Share on other sites

Hi,

I believe sub-subs are not retrieved in the category page. Only subcategories. . You might want to extend the category controller and edit the following method:

 

protected function assignSubcategories()
{
 if ($subCategories = $this->category->getSubCategories($this->context->language->id))
 {
  $this->context->smarty->assign(array(
   'subcategories' => $subCategories,
   'subcategories_nb_total' => count($subCategories),
   'subcategories_nb_half' => ceil(count($subCategories) / 2)
  ));
 }
}

 

Just use a foreach and grab subcategories for these too :)

Link to comment
Share on other sites

If you don't know how to use overrides you can change that functions directly. Inside the IF, add foreach $subCategories as $key ->$subCat. THen inside ther foreach loop, create a new category object with the subcategory ID, then use the same function prestashop does (getSubcategory) on that object, and save it to $subCategories[$key]

Link to comment
Share on other sites

No no this is not to be done inside a template, but in categorycontroller.php, only php knowledge is required

 

Haven't tested it, but here it is

 

protected function assignSubcategories()
{
 if ($subCategories = $this->category->getSubCategories($this->context->language->id))
 {
  foreach ($subCategories as $key => $subcat) {

   $subcatObj = new Category($subcat['id_category']);
   $subcategories[$key]['subcategories'] = $subcatObj->getSubCategories($this->context->language->id));

  }
  $this->context->smarty->assign(array(
   'subcategories' => $subCategories,
   'subcategories_nb_total' => count($subCategories),
   'subcategories_nb_half' => ceil(count($subCategories) / 2)
  ));
 }
}

  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...

Hello !

 

After a long search I came across your post but it does not work :-/ I tried to change but to no avail: '(Any ideas why?

 

 

What I want to do:

on the page of categories I am looking to display the sub & sub-sub categories

 

Thank you very much!

 

 

 

(Excuse me for my English I'm french and i use google translate)

Link to comment
Share on other sites

  • 3 weeks later...
  • 1 month later...

This worked for me.
in CategoryController.php override ->

 

<?php

class CategoryController extends CategoryControllerCore
{
	public function setMedia()
	{
		$this->context->controller->addJs(_PS_JS_DIR_.'jquery/jquery-1.7.2.min.js');
		$this->context->controller->addJqueryUi('ui.accordion');
	}
	protected function assignSubcategories()
	{
		if ($subCategories = $this->category->getSubCategories($this->context->language->id))
		{
			foreach ($subCategories as $key => $subcat) {
		
			$subcatObj = new Category($subcat['id_category']);
			$subCategories[$key]['subcategories'] = $subcatObj->getSubCategories($this->context->language->id);
		}
			$this->context->smarty->assign(array(
				'subcategories' => $subCategories,
				'subcategories_nb_total' => count($subCategories),
				'subcategories_nb_half' => ceil(count($subCategories) / 2)
			));
		}
	}
}

and in tpl file ->

 

{foreach from=$subcategories item=subcategory}
	<h3>{$subcategory.name|escape:'htmlall':'UTF-8'}</h3>
	<div>
		<ul class="sub-menu">
			{foreach from=$subcategory['subcategories'] item=subcategories}
				<li>
					<a href="{$link->getCategoryLink($subcategories.id_category, $subcategories.link_rewrite)|escape:'htmlall':'UTF-8'}">{$subcategories.name|escape:'htmlall':'UTF-8'}</a>
				</li>
			{/foreach}
		</ul>
	</div>
{/foreach}

And now I have nice accardion type menu. :)

Link to comment
Share on other sites

  • 4 weeks later...

Hello,

I replaced all content in:

 

..\controllers\front\CategoryController.php

 

with:

<?php

class CategoryController extends CategoryControllerCore
{
    public function setMedia()
    {
        $this->context->controller->addJs(_PS_JS_DIR_.'jquery/jquery-1.7.2.min.js');
        $this->context->controller->addJqueryUi('ui.accordion');
    }
    protected function assignSubcategories()
    {
        if ($subCategories = $this->category->getSubCategories($this->context->language->id))
        {
            foreach ($subCategories as $key => $subcat) {
        
            $subcatObj = new Category($subcat['id_category']);
            $subCategories[$key]['subcategories'] = $subcatObj->getSubCategories($this->context->language->id);
        }
            $this->context->smarty->assign(array(
                'subcategories' => $subCategories,
                'subcategories_nb_total' => count($subCategories),
                'subcategories_nb_half' => ceil(count($subCategories) / 2)
            ));
        }
    }
}

and in category.tpl replaced this:

 

      {foreach from=$subcategories item=subcategory}
				<li class="clearfix">
					<a href="{$link->getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'htmlall':'UTF-8'}" title="{$subcategory.name|escape:'htmlall':'UTF-8'}" class="img">
						{if $subcategory.id_image}
							<img src="{$link->getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium_default')|escape:'html'}" alt="" width="{$mediumSize.width}" height="{$mediumSize.height}" />
						{else}
							<img src="{$img_cat_dir}default-medium_default.jpg" alt="" width="{$mediumSize.width}" height="{$mediumSize.height}" />
						{/if}
					</a>
					<a href="{$link->getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'htmlall':'UTF-8'}" class="cat_name">{$subcategory.name|escape:'htmlall':'UTF-8'}</a>
					{if $subcategory.description}
						<p class="cat_desc">{$subcategory.description}</p>
					{/if}
				</li>
			{/foreach}

with this:

{foreach from=$subcategories item=subcategory}
    <h3>{$subcategory.name|escape:'htmlall':'UTF-8'}</h3>
    <div>
        <ul class="sub-menu">
            {foreach from=$subcategory['subcategories'] item=subcategories}
                <li>
                    <a href="{$link->getCategoryLink($subcategories.id_category, $subcategories.link_rewrite)|escape:'htmlall':'UTF-8'}">{$subcategories.name|escape:'htmlall':'UTF-8'}</a>
                </li>
            {/foreach}
        </ul>
    </div>
{/foreach}

Please correct me if this is wrong.

Thank you.

Link to comment
Share on other sites

Did you override the controller correctly?

I think you did wrong at the first part. Don't replace all content in in YOURSITE/controllers/front/CategoryController.php.

This is what you have to do:
1. Undo ALL the things you did in YOURSITE/controllers/front/CategoryController.php

2. Navigate to YOURSITE/override/controllers/front and create file CategoryController.php

3. In that file place the script
 

<?php

class CategoryController extends CategoryControllerCore
{
    public function setMedia()
    {
        $this->context->controller->addJs(_PS_JS_DIR_.'jquery/jquery-1.7.2.min.js');
        $this->context->controller->addJqueryUi('ui.accordion');
    }
    protected function assignSubcategories()
    {
        if ($subCategories = $this->category->getSubCategories($this->context->language->id))
        {
            foreach ($subCategories as $key => $subcat) {
        
            $subcatObj = new Category($subcat['id_category']);
            $subCategories[$key]['subcategories'] = $subcatObj->getSubCategories($this->context->language->id);
        }
            $this->context->smarty->assign(array(
                'subcategories' => $subCategories,
                'subcategories_nb_total' => count($subCategories),
                'subcategories_nb_half' => ceil(count($subCategories) / 2)
            ));
        }
    }
}

And it should take effect. If not navigate to YOURSITE/cache and delete class_index.php

If nothing happens please be sure that you have created category with subcategories.

P.S. post any error messages if you get them.

Link to comment
Share on other sites

  • 7 months later...
  • 1 year later...

Sorry to dig up old post. Managed to get this working in the latest Prestashop (1.6) by modifying the controller slightly.

 

My override/controllers/front/CategoryController.php:

class CategoryController extends CategoryControllerCore
{
 
    protected function assignSubcategories()
    {
        if ($sub_categories = $this->category->getSubCategories($this->context->language->id)) {
			
			
		foreach ($sub_categories as $key => $subcat) {
			$subcatObj = new Category($subcat['id_category']);
			$sub_categories[$key]['subcategories'] = $subcatObj->getSubCategories($this->context->language->id);
		}
		
		
            $this->context->smarty->assign(array(
                'subcategories'          => $sub_categories,
                'subcategories_nb_total' => count($sub_categories),
                'subcategories_nb_half'  => ceil(count($sub_categories) / 2)
            ));
        }
    }
	
	

}

When you have this code in, delete the class_index.php cache file.

 

Then just lump the code provided by Klubas into your category.tpl file in your theme somewhere within your $subcategory loop. I've posted it here for convenience.

<ul class="sub-menu">
			{foreach from=$subcategory['subcategories'] item=subcategories}
				<li>
					<a href="{$link->getCategoryLink($subcategories.id_category, $subcategories.link_rewrite)|escape:'htmlall':'UTF-8'}">{$subcategories.name|escape:'htmlall':'UTF-8'}</a>
				</li>
			{/foreach}
		</ul>
Link to comment
Share on other sites

 

Sorry to dig up old post. Managed to get this working in the latest Prestashop (1.6) by modifying the controller slightly.

 

My override/controllers/front/CategoryController.php:

class CategoryController extends CategoryControllerCore
{
 
    protected function assignSubcategories()
    {
        if ($sub_categories = $this->category->getSubCategories($this->context->language->id)) {
			
			
		foreach ($sub_categories as $key => $subcat) {
			$subcatObj = new Category($subcat['id_category']);
			$sub_categories[$key]['subcategories'] = $subcatObj->getSubCategories($this->context->language->id);
		}
		
		
            $this->context->smarty->assign(array(
                'subcategories'          => $sub_categories,
                'subcategories_nb_total' => count($sub_categories),
                'subcategories_nb_half'  => ceil(count($sub_categories) / 2)
            ));
        }
    }
	
	

}

When you have this code in, delete the class_index.php cache file.

 

Then just lump the code provided by Klubas into your category.tpl file in your theme somewhere within your $subcategory loop. I've posted it here for convenience.

<ul class="sub-menu">
			{foreach from=$subcategory['subcategories'] item=subcategories}
				<li>
					<a href="{$link->getCategoryLink($subcategories.id_category, $subcategories.link_rewrite)|escape:'htmlall':'UTF-8'}">{$subcategories.name|escape:'htmlall':'UTF-8'}</a>
				</li>
			{/foreach}
		</ul>

 

Daelune, It worked perfectly!  Thank you!  Kisses to you.

Link to comment
Share on other sites

  • 2 weeks later...

This script to display all subcategories stopped working after upgrading to PHP 5.5 from 5.3

This is no longer true, my developer must had set BO > Performance "Disable all overrides " to YES.  This was the reason why my sub-categories stopped showing.  The solution by Nemo1 and Daelune still works on my PS 1.6.0.13

Link to comment
Share on other sites

Ah, in this case, no. You need to code some custom module, create a custom hook at the top of the category page (the file is category.tpl), then have it get all subcategories of the current one. You need to use recursion with this

$category->getSubCategories();

Like for each of the subcategories, get theirs, and so on.

Link to comment
Share on other sites

Ah, in this case, no. You need to code some custom module, create a custom hook at the top of the category page (the file is category.tpl), then have it get all subcategories of the current one. You need to use recursion with this

 

$category->getSubCategories();

 

Like for each of the subcategories, get theirs, and so on.

Hello Nemo1, could you provide code to such described solutions? Can be an example

Link to comment
Share on other sites

  • 5 years later...

If you just want a basic array of subcat, without levels, you can just use

$subCategories = array();

$category= new Category(Tools::getValue('id_category'));

if ($subCategories = $category->getSubCategories($this->context->language->id)) {

   foreach ($subCategories as $key => $subcat)

   {

      $subcatObj = new Category($subcat['id_category']);

      $subCategories= array_merge( $subCategories, $subcatObj->getSubCategories($this->context->language->id));

   }

}

Bonne journée

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