【问题标题】:Get categories in a Prestashop theme获取 Prestashop 主题中的类别
【发布时间】:2016-05-15 12:27:08
【问题描述】:

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

我的代码header.tpl

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

问题:错误 500

【问题讨论】:

  • 您检查过您的 apache 错误日志文件吗?您使用的是哪个版本的 Prestashop?
  • 我使用的是最新版本,错误是Category 不存在...但是我在哪里可以将类别发送到模板?

标签: smarty prestashop


【解决方案1】:

您编写的代码对 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:只返回活跃的caregories。
  4. $id_shop:如果您在安装中有多个商店,则为商店的 ID。

打印要调试的变量

如果你想调试或者查看变量,你可以试试下面的sn-ps代码:

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

或:

{* Print all variables *}
{debug}

【讨论】:

    【解决方案2】:

    模板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
    

    【讨论】:

    • 这个解决方案很好,但我认为它有一个缺点:你必须开发一个模块。如果您想修改结果的结果,例如,制作类别的自定义过滤器,此解决方案会更好。但如果你没有任何特殊功能,最简单的方法是通过静态调用。
    • 是的,但如果您想让系统安全地进行主题升级,开发模块是首选方式。
    猜你喜欢
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-25
    相关资源
    最近更新 更多