OpenCart商品页实现价格下调11.76470588%及双价格展示需求
OpenCart 商品页面价格下调展示实现方案
核心计算逻辑
下调11.76470588%的本质是:折扣后价格 = 原最终价格 × (1 - 0.1176470588),其中0.1176470588是下调百分比的小数形式。以下提供两种落地实现方式:
方案一:直接在模板中计算(快速修改)
OpenCart商品页面模板路径为 catalog/view/theme/你的主题/template/product/product.twig,找到价格展示的代码块,按需修改:
- 仅展示下调后的价格
{% set discounted_price = price * 0.8823529412 %} <span class="price">{{ discounted_price|currency }}</span>
注:0.8823529412是1 - 0.1176470588的简化结果,|currency是OpenCart内置过滤器,自动适配站点货币格式。
- 同时展示原价格与折扣后价格
<!-- 原价格加删除线样式 --> <span class="old-price"><del>{{ price|currency }}</del></span> <!-- 计算并展示折扣后价格 --> {% set discounted_price = price * 0.8823529412 %} <span class="new-price" style="color: #e74c3c; font-weight: bold;">{{ discounted_price|currency }}</span>
方案二:控制器处理变量(规范复用)
若需多页面复用该逻辑,建议在商品控制器中提前计算价格:
打开 catalog/controller/product/product.php,在原价格赋值代码附近添加:
// 计算下调11.76470588%后的价格(适配税设置) $raw_price = $this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')); $discounted_price = $raw_price * 0.8823529412; $data['discounted_price'] = $this->currency->format($discounted_price, $this->session->data['currency']);
随后在模板中直接调用变量:
<span class="old-price"><del>{{ price|currency }}</del></span> <span class="new-price">{{ discounted_price }}</span>
含税/不含税场景适配
如果你的价格涉及税率转换(比如你提到的1.19/1.05税率),可调整计算逻辑:
{# 示例:原价格含税(19%)→ 转不含税 → 计算折扣 → 按新税率(5%)转含税 #} {% set discounted_price = (price / 1.19) * 0.8823529412 * 1.05 %} <span class="price">{{ discounted_price|currency }}</span>
你之前代码的问题说明
你写的$percent = (($rowx->Orgprice - $rowx->SalePrice)*100)/$rowx->Orgprice;是计算折扣百分比的公式,而非直接生成折扣后价格,因此无法得到你想要的下调后价格结果。
内容的提问来源于stack exchange,提问作者RCI
相关产品推荐
相关产品推荐

