Jump to content

Subcategories links inside a subcategory?


Recommended Posts

I want to add the subcategories menu of a category inside each subcategory of this category (similar to a tab menu inside each category) . I know I need to modify the category.tpl file, and the code maybe similar to:

 

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

   </li>
  {/foreach}

 

 

But this code doesnt works inside a subcategory,needs some modifications.

 

You can see more info about my question here: http://www.prestashop.com/forums/topic/228708-subcategories-menu-inside-a-subcategory/page__p__1123841#entry1123841

 

 

any help?

 

Thanks so so much.

Link to comment
Share on other sites

I'm not sure i completely understood what you want to achieve. Do you want to get a list of the sibling categories of the current ones? I mean, brothers and sisters of the current subcategory?

 

Yes! Exactly. I want to show the brothers and sisters subcategories of the current subcategory for make a "tab menu".

Link to comment
Share on other sites

Ok, let's try something:

 

In category.tpl, add this line where you want the list to appear:

 

{hook h='category' id_category=$category->id}

 

We are creating a new hook and passing the category ID as a parameter.

 

Now create a new module, add registerHook('category') in the install function, then create hookCategory like this

 

public function hookCategory($params)
{
   $id_parent = $params['id_parent'];
   $parent_category = new category($id_parent);
   $children = $parent_category->getSubcategories($this->context->language->id);
// finally, assign them to smarty and return a tpl

}

 

Of course, you might want to add some kind of switch to target specific categories only, this is just a rough idea :)

  • Like 1
Link to comment
Share on other sites

<p>

<br>

Ok, let&#39;s try something:<br>

<br>

In category.tpl, add this line where you want the list to appear:<br>

<br>

{hook h=&#39;category&#39; id_category=$category->id}<br>

<br>

We are creating a new hook and passing the category ID as a parameter.<br>

<br>

Now create a new module, add registerHook(&#39;category&#39;) in the install function, then create hookCategory like this<br>

<br>

<br>
public function hookCategory($params)<br>
{<br>
    $id_parent = $params[&#39;id_parent&#39;];<br>
    $parent_category = new category($id_parent);<br>
    $children = $parent_category->getSubcategories($this->context->language->id);<br>
// finally, assign them to smarty and return a tpl<br>
<br>
}<br>

<br>

<br>

Of course, you might want to add some kind of switch to target specific categories only, this is just a rough idea <img alt=":)" class="bbc_emoticon" src="http://www.prestashop.com/forums/public/style_emoticons/default/smile.png"><br>

 

Thanks so much! I am going to try this. It seems difficult but not impossible. Thanks again.

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

  • 1 year later...

Hi,

I'm trying to apply the Nemo1 tips with some modifies.

I add in category.tpl the following row

{hook h='category' id_category=$subcategory.id_category}

Then I create a module with this function

public function hookCategory($params) {
		$id_category = $params['id_category'];
		$category[] = new Category($id_category);
		global $smarty;
		$smarty->assign(array('category'=> $category));

		return $this->display(__FILE__, 'mymodule.tpl');
	}

and I make this tpl file

{if isset($category)}
	{$category.name}
{/if}

But when I reload the page I have this error

"Fatal error: Cannot use object of type Category as array in..."

Please someone can tell me what wrong?

Thanks

Link to comment
Share on other sites

Hi Nemo,

thanks for your reply.

But I've still problem...

Here the modificated code

 - In category tpl:

{hook h='category' category=$subcategory}

 - In the new module:

public function hookCategory($params) {
		$category = $params['category'];
		$children = $category->getSubcategories($this->context->language->id);
		...other code here


		return $this->display(__FILE__, 'subcategoriesmenuoncategory.tpl');
	}

but when I reload the page I have a fatal error:

 

"Fatal error: Call to a member function getSubcategories() on a non-object in..."

 

I'm passing the object "category", why I can't invoke the metode "getSubcategories"?

 

Thanks

Link to comment
Share on other sites

Hi Nemo1,

I resolved so:

public function hookCategory($params) {
		$category = $params['category'];
		$id_category = $category['id_category'];
		$parent_category = new Category($id_category);
		$children = $parent_category->getSubcategories($this->context->language->id);
		// finally, assign them to smarty and return a tpl
		global $smarty;
		$smarty->assign('category', $category);
    	        $smarty->assign('children', $children);
        	$this->context->controller->addCSS($this->_path.'css/menucategory.css');
		return $this->display(__FILE__, 'menucategory.tpl');
	}

It's strange, because I passed an object.

Thanks for all

Link to comment
Share on other sites

Hi Nemo,

first thank for all helpful tips.

Now I have a problem with css. T_T

I tried to add CSS in any way but seems it ignores me.

I tried with:

Tools::addCSS(($this->_path).'css/menucategory.css', 'all');

and so with:

$this->context->controller->addCSS(($this->_path).'css/menucategory.css', 'all');

and also with:

public function hookHeader() {
		$this->context->controller->addCSS(($this->_path).'css/menucategory.css', 'all');
	}

I cleared the cache and compile smarty folders but nothing, my css isn't loaded.

Any suggest?

My prestashop version is 1.5.6.2

 

Thanks

Link to comment
Share on other sites

Hi Vekia,

I think no, how I can see it?

I added in category.tpl "{hook h='category' category=$subcategory}" and than I created a new module with this function:

public function hookCategory($params) {
		$category = $params['category'];
		$id_category = $category['id_category'];
		$parent_category = new Category($id_category);
		$children = $parent_category->getSubcategories($this->context->language->id);
		$this->context->smarty->assign('category', $category);
		$this->context->smarty->assign('children', $children);
 		if ($this->psversion()==5 || $this->psversion()==6){
 			$this->context->controller->addCSS(($this->_path).'css/menucategory.css', 'all');
 		} else {
 			Tools::addCSS(($this->_path).'css/menucategory.css', 'all');
 		}
		return $this->display(__FILE__, 'menucategory.tpl');
	}

I forgot something?

 

Thanks

Link to comment
Share on other sites

displayhookheader is a place to include css / js librariers

all other hooks are wrong place to include css / js files

 

if you will check page structure you will see that libraries are included in <head></head> section of your website

not in left / right  columns or sth

Link to comment
Share on other sites

  • 2 months later...
  • 1 month 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...