Opencart 2.1.0.2版本:如何在搜索结果页显示产品型号
解决OpenCart 2.1.0.2搜索结果页显示产品型号的问题
嘿,我刚好熟悉OpenCart 2.1.0.2的架构,给你一套亲测有效的步骤,帮你实现这个需求:
1. 从控制器传递型号数据到模板
首先得让后端把产品型号的数据传给前端模板。找到catalog/controller/product/search.php文件,定位到处理搜索结果的foreach ($results as $result)循环,在构建产品数组的时候,把型号字段加进去:
// 在循环内的产品数组中新增'model'字段 $data['products'][] = array( 'product_id' => $result['product_id'], 'thumb' => $image, 'name' => $result['name'], 'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..', 'price' => $price, 'special' => $special, 'tax' => $tax, 'minimum' => $result['minimum'] > 0 ? $result['minimum'] : 1, 'rating' => $result['rating'], 'href' => $this->url->link('product/product', 'product_id=' . $result['product_id'] . $url), 'model' => $result['model'] // 新增这行,传递型号数据给模板 );
2. 修改前端模板显示型号
接下来修改搜索结果的模板文件,让型号显示出来。找到你当前使用的主题模板文件,路径是catalog/view/theme/你的主题名/template/product/search.tpl(默认主题就是default)。在产品列表的循环里,找个合适的位置(比如产品名称下方)添加型号的显示代码:
<!-- 示例:在产品名称之后添加型号显示 --> <div class="product-info"> <h4><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a></h4> <!-- 新增型号显示代码 --> <p class="product-model">型号: <?php echo $product['model']; ?></p> <p><?php echo $product['description']; ?></p> <!-- 其他现有代码 --> </div>
如果想要让型号样式更协调,可以给它加一点CSS样式,比如在主题的CSS文件(比如catalog/view/theme/你的主题名/stylesheet/product.css)里添加:
.product-model { color: #777; font-size: 0.9rem; margin: 0.5rem 0; }
3. 刷新缓存确保生效
修改完文件后,一定要去OpenCart后台的「系统→存储」页面,点击「刷新缓存」按钮,确保修改的内容生效。之后再去搜索页面测试,就能看到每个产品的型号显示出来啦!
内容的提问来源于stack exchange,提问作者chrysst
相关产品推荐
相关产品推荐

