后台手动指定(最简单直接)
这种方法最简单,不需要修改核心文件,适合少量商品且不需要频繁更换模板的场景。

原理: 利用ECSHOP的“扩展属性”功能,为每个商品添加一个自定义属性来存储其对应的模板文件名,然后在商品详情页模板中读取这个属性,并动态加载指定的模板。
操作步骤:
-
添加商品扩展属性
- 登录ECSHOP后台。
- 进入 商品 -> 商品类型。
- 点击你正在使用的商品类型(普通商品”)进行编辑。
- 在“扩展属性”区域,点击 增加一个属性。
- 属性名称:填写
商品模板(或任何你喜欢的名称)。 - 属性值:留空,因为我们希望每个商品都可以有不同的值。
- 设置:确保“是否用于搜索”等选项按需设置。
- 点击 提交。
-
为商品指定模板
(图片来源网络,侵删)- 进入 商品 -> 商品列表,编辑你想要使用不同模板的商品。
- 在商品编辑页面,向下滚动到 扩展属性 部分,你会看到刚才添加的“商品模板”字段。
- 在 属性值 输入框中,填入你想要使用的模板文件名,
template_diy.html。 - 注意:这里的文件名是相对于你的主题文件夹的,
themes/default/goods/template_diy.html,你只需要输入template_diy.html即可。 - 保存商品。
-
修改商品详情页模板
- 使用FTP或文件管理器,打开你的ECSHOP主题文件夹,
themes/default/。 - 找到并编辑
goods.dwt文件。 - 在
{if $goods.goods_type == 0}这行代码之前(通常在文件顶部),添加以下PHP代码:
{php} // 获取当前商品的扩展属性 $sql = "SELECT attr_value FROM " . $ecs->table('goods_attr') . " WHERE goods_id = '$goods_id' AND attr_id = '你的属性ID'"; $attr_value = $db->getOne($sql); // 如果设置了自定义模板,则加载它 if (!empty($attr_value) && file_exists(ROOT_PATH . 'themes/' . $smarty->template_dir . '/goods/' . $attr_value)) { $template_file = 'goods/' . $attr_value; } {/php}重要: 你需要将
'你的属性ID'替换成实际的ID,这个ID可以在数据库的ecs_attribute表中找到,或者在编辑商品类型时,鼠标悬停在“商品模板”属性名称上,查看URL中的attr_id参数。为了更方便,我们可以换一种更简单的方式读取属性值,修改如下:
{php} // 从商品信息中直接获取,更简单 $custom_template = $goods.goods_attr; // 注意:这个字段默认不存,所以需要另一种方法 // 更可靠的方法:从goods_attr表查询 $attr_id = 12; // 假设你的“商品模板”属性ID是12,请替换成你自己的! $sql = "SELECT attr_value FROM " . $GLOBALS['ecs']->table('goods_attr') . " WHERE goods_id = '$goods_id' AND attr_id = '$attr_id'"; $attr_value = $GLOBALS['db']->getOne($sql); if (!empty($attr_value) && file_exists(ROOT_PATH . 'themes/' . $GLOBALS['_CFG']['template'] . '/goods/' . $attr_value)) { $template_file = 'goods/' . $attr_value; } {/php}更优化的修改方案: 上述方法需要手动查询数据库,有点麻烦,一个更优雅的修改方式是直接在
goods.php文件中为商品对象$goods添加一个新的属性,然后在模板中直接调用。
(图片来源网络,侵删)-
修改
goods.php: 打开includes/lib_goods.php,找到getGoodsInfo函数,在函数末尾,return $goods;之前,添加以下代码:// 获取自定义商品模板 $sql = "SELECT attr_value FROM " . $GLOBALS['ecs']->table('goods_attr') . " WHERE goods_id = '$goods_id' AND attr_id = '你的属性ID'"; $goods['custom_template'] = $GLOBALS['db']->getOne($sql); -
修改
goods.dwt:goods.dwt的修改就变得非常简单了,在文件顶部找到{include file='library/page_header.lbi'}之后,添加:{if $goods.custom_template && file_exists($smarty.template_dir|cat:'/goods/'|cat:$goods.custom_template)} {assign var="template_file" value="goods/"|cat:$goods.custom_template} {/if}这段代码的意思是:
$goods.custom_template存在,并且对应的模板文件也存在,那么就将$template_file变量设置为这个自定义模板的路径,ECSHOP的底层渲染机制会自动使用这个$template_file来渲染页面。 -
创建自定义模板: 在你的主题文件夹下(如
themes/default/)创建一个goods文件夹(如果不存在的话),然后在这个文件夹里创建你指定的模板文件,template_diy.html,在里面编写你独特的HTML代码。
- 使用FTP或文件管理器,打开你的ECSHOP主题文件夹,
按商品分类指定(推荐)
如果你的不同商品只是想用不同模板,并且这些商品可以归类到不同的分类下,那么按分类指定是更高效、更易于维护的方法。
原理: 为不同的商品分类设置一个“模板”扩展属性,然后在商品详情页,根据商品所属的分类来加载对应的模板。
操作步骤:
-
添加分类扩展属性
- 后台进入 商品 -> 商品分类。
- 编辑一个商品分类,在 扩展属性 部分添加一个新属性。
- 属性名称:
分类模板 - 属性值:留空。
- 保存分类。
-
为分类指定模板
- 再次编辑该分类,在“分类模板”的属性值中填入模板文件名,
template_category1.html。 - 保存。
- 再次编辑该分类,在“分类模板”的属性值中填入模板文件名,
-
修改商品详情页模板
-
同样,我们采用修改
goods.php的方式,这样最清晰。 -
修改
includes/lib_goods.php: 在getGoodsInfo函数的末尾,return $goods;之前,添加以下代码:// 获取商品分类的顶级分类ID $sql = "SELECT cat_name, cat_id, parent_id FROM " . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$goods[cat_id]'"; $category = $GLOBALS['db']->getRow($sql); while ($category['parent_id'] > 0) { $sql = "SELECT cat_name, cat_id, parent_id FROM " . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$category[parent_id]'"; $category = $GLOBALS['db']->getRow($sql); } $top_cat_id = $category['cat_id']; // 获取顶级分类的自定义模板 $sql = "SELECT attr_value FROM " . $GLOBALS['ecs']->table('category_attr') . " WHERE cat_id = '$top_cat_id' AND attr_id = '你的分类属性ID'"; $goods['category_template'] = $GLOBALS['db']->getOne($sql);注意:这里的
'你的分类属性ID'需要替换成你在ecs_attribute表中为“分类模板”属性找到的ID。 -
修改
goods.dwt: 在文件顶部添加以下代码:{if $goods.category_template && file_exists($smarty.template_dir|cat:'/goods/'|cat:$goods.category_template)} {assign var="template_file" value="goods/"|cat:$goods.category_template} {/if} -
创建自定义模板: 在
themes/default/goods/目录下创建template_category1.html等模板文件。
-
按商品ID指定(硬编码,不推荐但最直接)
这种方法适合一次性为几个特定ID的商品设置模板,不适合长期维护。
原理:
在 goods.dwt 中判断当前商品的ID,如果ID匹配,就加载指定的模板。
操作步骤:
-
修改
goods.dwt: 在文件顶部添加如下代码:{php} $custom_templates = array( 10 => 'template_special.html', // 商品ID为10时使用这个模板 25 => 'template_vip.html', // 商品ID为25时使用这个模板 ); if (isset($custom_templates[$goods_id]) && file_exists(ROOT_PATH . 'themes/' . $smarty->template_dir . '/goods/' . $custom_templates[$goods_id])) { $template_file = 'goods/' . $custom_templates[$goods_id]; } {/php} -
创建模板文件: 在
themes/default/goods/目录下创建template_special.html等文件。
缺点:每次要添加新商品时,都需要修改 goods.dwt 文件,非常不灵活,且容易出错。
总结与建议
| 方法 | 优点 | 缺点 | 适用场景 |
|---|---|---|---|
| 手动指定 | 灵活,单个商品可独立设置 | 需要为每个商品手动设置,属性ID需要查询 | 少量特殊商品,如活动商品、爆款等 |
| 按分类指定 | 推荐,易于批量管理和维护,符合网站结构逻辑 | 需要将商品归类到正确的分类下 | 大多数情况,特别是不同品类商品风格差异大时 |
| 按ID指定 | 实现简单,无需修改数据库 | 极不灵活,维护困难,硬编码 | 临时性、一次性需求,快速测试 |
强烈推荐使用【方法二:按商品分类指定】,因为它最符合电商网站的组织逻辑,也最易于长期维护。
无论使用哪种方法,请务必在修改前备份你的文件和数据库!
