Jump to content

I need the sibling categories.


cableguy1115

Recommended Posts

Hello,

 

I've been trying for some days now to modify the "category.tpl" and "CategoryController.php", so the subcategory menu show me the sibling categories when reaching the last bottom subcategories.

For example, if I have a category tree of [1] -> [2] -> [3]. When you click on 1 you get 2, when on 2 you get 3, but when you click on 3 the subcategory menu should show me all the categories at the same level with 3 (since it's the bottom category). I imagine thinking we can do it by getting the parent category, then listing with a foreach all the children of the parent. Problem is I'm always getting problems en errors, can someone help me?

 

I have for now in my category.tpl modified the next function:

		{if isset($subcategories)}
			{if (isset($display_subcategories) && $display_subcategories eq 1) || !isset($display_subcategories) }
			
			
			<div id="subcategories">
					<ul class="clearfix">
				{foreach from=$subcategories item=subcategory}
					<li>
						<h5><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></h5>
						{if $subcategory.description}
							<div class="cat_desc">{$subcategory.description}</div>
						{/if}
					</li>
				{/foreach}
				</ul>
			</div>
		   
			{/if}
		
		{else}
		
		<!-- HERE IS WHERE IT SHOULD START -->
		<!-- No subcategory is found, I should list all the sibling categories of the curent one Here. -->
			
		{/if}

AND a function in CategoryController.php

public function getSiblings()
	{
		$_parent = new Category($this->category->id_parent, self::$cookie->id_lang);
		$_siblings = getChildren($parent_category,self::$cookie->id_lang,true);
		$_allsiblings = "";


		foreach ($_siblings as $sib) {
			$_allsiblings .= "<li><h5>". $sib ."</h5></li> | ";
		}
	}

A very big thank you to anyone able to help me, in advance thanks!!

Link to comment
Share on other sites

Here's my version of what you want to do. For the file override/controllers/front/CategoryController.php:

<?php

class CategoryController extends CategoryControllerCore
{
    protected function assignSubcategories()
    {
        if ($sub_categories = $this->category->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),
                'is_leaf_category'       => false
            ));
        } else {
            $parent_category = new Category($this->category->id_parent);
            $sub_categories = $parent_category->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),
                'is_leaf_category'       => true
            ));
        }
    }     
}

Then if you want to have a different label such as "Siblings", you can simply change line 77 of category.tpl from:

			<p class="subcategory-heading">{l s='Subcategories'}</p>

to:

			<p class="subcategory-heading">{if $is_leaf_category}{l s='Siblings'}{else}{l s='Subcategories'}{/if}</p>
  • Like 1
Link to comment
Share on other sites

A million thanks, Rocky! I overrid the function assignSubcategories() and now it's half-way done! My problem now is that if I don't select any big category, the sub-category menu shows all of them together 

 

For example:

 

Category 1: Food;

Subcategories 2, 3: Spicy, Sweet.

 

When I go to the page and I click on "Spicy", instead of first selecting "Food" (on the other filter), the sub-category menu will show [ food / drink / etc / Spicy / Sweet ].

Would there be any way to avoid that, either with a conditional or just hiding the stuff with css?

 

Again, a million thanks!

Link to comment
Share on other sites

Sorry, I'm now confused about what you mean by subcategory menu. I assumed it was just the subcategories displayed above the product listings. Are you referring to the "Categories block", "Top horizontal menu", "Layered navigation block" or a third-party module? Maybe a screenshot or link to your website would help me understand.

Link to comment
Share on other sites

Okay, after your message I had to go deeper into the problem and see what's really happening. Upon further investigation, I hope I can depict the problem a bit better and where all these problems come from.

 

The Prestashop is a online shoe shop.

 

We have 5 main categories (for the 5 main pages):

  1. Models.
  2. Summer collection.
  3. ...

Next, inside Models and each of the main category we have the next categories:

  1. Nike.
  2. Puma.
  3. ...
  4. Man. (as for the sex)
  5. Woman. (as for the sex)

 

Next, a third level category, inside NIke, Puma, etc., we hace again only two categories: man and woman. As the first person who built that told me, they do this two level sex categories so you can choose all models of a certain sex without the need to choose a model or particular shoemaker, and to also filter it by sex when you choose a shoemaker or a model.

 

So, basically, the idea was to have a page with all the shoe models, and to be able to choose from there if it's man or woman made, but without the need to enter a certain model and select "man / woman".

 

I highly doubt this is the best approach to solve this situation, with so many nested categories, and probably we could have used tags too, but we don't have much knowledge on Prestashop's programming particularities or envinroment variables, nor it's already existing premade functions, etc.. I'm all ears if anyone can suggest a better structure for this, and as always, a million thanks.

 

 

post-1268061-0-11854800-1464815010_thumb.jpg

Link to comment
Share on other sites

It seems you're using categories for everything, which causes the category structure to be very complicated. I would suggest simplifying it by using features, manufacturers and the "Layered navigation" block.

 

For example, you could go to Catalog > Features and create a "Gender" feature with "Man" and "Woman" feature values. Then edit each product and choose either "Man" or "Woman" as the gender on the Features subtab. You can then enable the "Layered navigation block", edit the template and enable the "Gender" feature. That way, you can filter by gender from any category without needing "Man" and "Woman" subcategories.

 

You can also add all the brands as manufacturers on the Catalog > Manufacturers tab and then enable the "Manufacturers" filter on the "Layered navigation block" configuration page. Then, you should be able to have a simple menu with only a few items, then the customer can use the "Layered navigation" module to filter the products to their preferences. You can use the "Top horizontal menu" module to create a menu with specific categories and a manufacturers submenu.

 

That's what I'd do if I was creating the website. I hope it helps.

  • Like 1
Link to comment
Share on other sites

Indeed seems the best aproach anyone could have come up with. I'm starting right away with a local shop to see if everything would work accordingly and gonna start doing it on the online one as soon as everything checks out.

 

You saved my life lad,

Thanks!!!

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