获取 Prestashop 主题中的类别

Get categories in a Prestashop theme

我想在我的 Prestashop 主题的 header (header.tpl) 中获取我的所有类别,但似乎效果不佳...

我的代码 header.tpl :

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

问题:错误 500

您编写的代码对 smarty 无效。 Prestashop 使用 Smarty 呈现模板。如果您想避免此类麻烦,请查看规则。此外,您在 Prestashop 的默认主题中有很多示例,以了解有关 Smarty 中编码的更多信息。

正确的代码是:

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

要传递的参数

  1. $id_parent :父类别 ID。 root id 类别为 1,home id 类别为 2。
  2. $id_lang:id语言。您可以在本地化区域中查看它以获取语言的 id。如果您启用了多种语言,您可以使用 $language 变量来获取 id。 List of global variables 在 Prestashop 中。
  3. $active: Return 只有活跃的照顾。
  4. $id_shop:如果你在一个安装中有多个商店,则为一个商店的id。

正在打印要调试的变量

如果您想调试或查看变量,您可以尝试以下代码片段:

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

或:

{* Print all variables *}
{debug}

模板 header.tpl 来自 FrontController.php 函数 displayHeader()

Category在那里不存在,因为header.tpl是所有页面都使用的综合模板。

有几个钩子可以用来添加内容:displayHeaderdisplayTopdisplayLeftColumndisplayRightColumndisplayFooter.

您可以在任何模块中添加所有类别,并像这样添加其中一个挂钩:

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