You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PrestaShop 8.1 Smarty变量缺失警告及促销商品栏添加问题

环境
  • PrestaShop 8.1
  • PHP 8.1.23
  • Windows 10

问题1:首页Smarty编译缓存警告(ps_featuredproducts.tpl缺失变量)

问题详情

修改基础模板.tpl文件并重启XAMPP后,首页弹出警告,提示ps_featuredproducts.tpl中缺少count变量,但未修改modules/ps_featuredproducts/ps_featuredproducts.php控制器。警告内容:

Warning: Undefined array key "count" in C:\xampp\htdocs\prestashop_8.1\var\cache\dev\smarty\compile\combat\fa\6c\c3\fa6cc378d2942c8857b89d6bca728048c0caeedd_2.module.ps_featuredproductsviewstemplateshookps_featuredproducts.tpl.php on line 34

Warning: Attempt to read property "value" on null in C:\xampp\htdocs\prestashop_8.1\var\cache\dev\smarty\compile\combat\fa\6c\c3\fa6cc378d2942c8857b89d6bca728048c0caeedd_2.module.ps_featuredproductsviewstemplateshookps_featuredproducts.tpl.php on line 34

解决办法

  1. 强制清理Smarty缓存
    • 直接删除var/cache/dev/smarty/compile和var/cache/dev/smarty/cache目录下的所有文件;
    • 或后台进入高级参数 > 性能,点击清除缓存,勾选所有缓存类型。
  2. 检查模板修改内容
    • 确认你修改的主题模板(如themes/myNewTheme/templates/hook/ps_featuredproducts.tpl)中,是否新增了{$count.value}这类未被控制器传递的变量调用。如果是误加,直接删除或注释;如果确实需要该变量,在ps_featuredproducts.php的hookDisplayHome方法中补充赋值:
      $this->context->smarty->assign([
          // 保留原有变量
          'count' => ['value' => $this->getProductCount()], // 替换为实际获取数量的逻辑
      ]);
      
  3. 验证模块状态
    • 后台进入模块 > 已安装模块,确认ps_featuredproducts模块处于启用状态,无冲突覆盖情况。

问题2:商品筛选页左侧栏添加促销商品(布局不符)

问题详情

已在ps_specials.php中注册displayLeftColumn钩子,主题layout-both-columns.tpl中调用了{hook h="displayLeftColumn"},但主题config/theme.yml显示商品筛选页使用layout-left-column,导致促销商品无法正常显示。

解决办法

  1. 修正布局模板的钩子调用
    • 打开themes/myNewTheme/templates/layouts/layout-left-column.tpl,在左侧栏区域添加{hook h="displayLeftColumn"}代码,放置在筛选栏上下的合适位置。
  2. 确保钩子注册生效
    • 卸载并重新安装ps_specials模块,触发钩子注册逻辑;
    • 或后台进入高级参数 > 钩子,找到displayLeftColumn钩子,手动将ps_specials模块添加到关联列表中。
  3. 调整主题布局配置
    • 打开themes/myNewTheme/config/theme.yml,确认商品筛选页(通常是category页面)的布局配置正确:
      pages:
        category:
          layouts:
            default: layout-left-column
      
  4. 调试钩子数据输出
    • 在ps_specials.php的hookDisplayLeftColumn方法中添加调试代码,确认是否有促销商品数据:
      public function hookDisplayLeftColumn($params)
      {
          $specials = $this->getSpecials();
          $this->context->smarty->assign(['specials' => $specials]);
          // 调试用:打印数据确认是否存在
          var_dump($specials);
          return $this->display(__FILE__, 'views/templates/hook/ps_specials.tpl');
      }
      
    • 访问商品筛选页,检查数据输出,确保模板能正常渲染促销商品。

内容的提问来源于stack exchange,提问作者Fruitcake_Gary

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 00:53:15