tonymx227
tonymx227

Reputation: 5451

Get categories in a Prestashop theme

I'd like to get all my categories in my header (header.tpl) of my Prestashop theme but it seems not working well...

My code header.tpl :

{$childCategories= Category::getChildren(0, 0, $active = true, $id_shop = false);}
{printf($childCategories)}

Issue : Error 500

Upvotes: 3

Views: 7502

Answers (2)

Indrė
Indrė

Reputation: 1136

A template header.tpl comes from FrontController.php function displayHeader().

Category doesn't exists there, because header.tpl is a comprehensive template used in all pages.

There are several hooks which you can use adding content there: displayHeader, displayTop, displayLeftColumn, displayRightColumn, displayFooter.

You can add all categories in any module and one of these hooks like that:

$category = new Category((int)Configuration::get('PS_HOME_CATEGORY'), $this->context->language->id);
$sub_categories = $category->getSubCategories($this->context->language->id);
// else code

Upvotes: 0

Rufein
Rufein

Reputation: 209

The code you have written is not valid for smarty. Prestashop uses Smarty to render templates. Please, take a look to the rules if you want to avoid troubles like this. Also, you have a lot of examples in the default theme of Prestashop to learn more about coding in Smarty.

The right code would be:

{assign var='childCategories' value=Category::getChildren(1, 1, true, false)}

Arguments to be passed

  1. $id_parent : The parent category id. The root id category is 1 and the home id category is 2.
  2. $id_lang: The id language. You can check it in the localization area to get the id of a language. If you have enable multiple languages, you could use the $language variable to get the id. List of global variables in Prestashop.
  3. $active: Return only the active caregories.
  4. $id_shop: The id of a shop if you have got multiple shops in an instalation.

Printing variables to debug

If you want debug or see the variables, you could try the following snippets of code:

{* Print only the variable $childCategories *}
{$childCategories|var_dump}

or:

{* Print all variables *}
{debug}

Upvotes: 7

Related Questions