Jump to content

[Solved] Creating category links


Recommended Posts

I want to get specific category information by category_id on every page, not  only category or product related. No problem with link, I just use i.e. {$link->getCategoryLink('12')} where 12 is category ID. But how to get category name? Is there any global function for that?

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

at the moment core doesnt contain any public static function to get category name.

you can create it in front controller 

 

public static function getCategoryName($id){

$category = new Category ($id,Context::getContext()->language->id);

return $category->name;

}

 

 

then you will be able to use

{FrontController::getCategoryName(12)}

in any .tpl file you want.

  • Like 4
Link to comment
Share on other sites

Just one more thing. Function getCategoryLink() uses some params:

// Set available keywords
$params = array();
$params['id'] = $category->id;
$params['rewrite'] = (!$alias) ? $category->link_rewrite : $alias;
$params['meta_keywords'] =	Tools::str2url($category->meta_keywords);
$params['meta_title'] = Tools::str2url($category->meta_title);

But it seems only ID and rewrite is used here. Is there any way to get meta parameters from it? Also I could add $params['name'] = $category->name; and therefore have one complete function to create full link with link, name, keywords etc.

 

I will figure it out later... :P

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

  • 2 months later...

at the moment core doesnt contain any public static function to get category name.

you can create it in front controller 

 

public static function getCategoryName($id){

$category = new Category ($id,Context::getContext()->language->id);

return $category->name;

}

 

 

then you will be able to use

{FrontController::getCategoryName(12)}

in any .tpl file you want.

So I added this function to /controllers/front/CategoryController.php.

 

Now how do I call it in /themes/default-bootstrap/category.tpl?

  • Like 1
Link to comment
Share on other sites

  • 3 months later...

Sorry, but I did all the steps but doesn't work (to me).

 

I have this error:

Fatal error: Call to undefined method FrontController::getCategoryName() in C:\EasyPHP\data\localweb\prestashop\tools\smarty\sysplugins\smarty_internal_templatebase.php(157) : eval()'d code on line 162

 

I'm working with PS 1.6.0.9

Thanks!

Link to comment
Share on other sites

  • 6 months later...

Where did you add this exactly?

 

 

public static function getCategoryName($id){

$category = new Category ($id,Context::getContext()->language->id);

return $category->name;

}

I would think you should duplicate the controller class into overrides so that updates do not overwrite your custom code?

Link to comment
Share on other sites

×
×
  • Create New...