You need to enable JavaScript to run this app. 如何修改WooCommerce订阅产品页价格显示,将"/月"替换为展示订阅总时长?
优惠活动
大模型
产品
解决方案
定价
更多

如何修改WooCommerce订阅产品页价格显示,将"/月"替换为展示订阅总时长?

WooCommerce Subscriptions 自定义订阅价格展示规则实现

基础原则

  • 所有自定义逻辑不需要修改WooCommerce、WooCommerce Subscriptions的核心文件,避免插件版本更新时自定义内容丢失
  • 代码需添加到当前使用主题的子主题functions.php文件,或轻量代码片段插件中,禁止直接修改父主题核心文件
  • 规则仅作用于有固定总时长的订阅产品,终身订阅、无固定结束时间的订阅产品会自动保持原有价格展示逻辑,普通实物/虚拟产品不受影响

可直接复用的自定义代码

add_filter('woocommerce_get_price_html', 'custom_fixed_subscription_price_label', 10, 2);
function custom_fixed_subscription_price_label($original_price_html, $product) {
    // 校验产品类型,非订阅类产品直接返回原价格展示
    $is_subscription = (
        is_a($product, 'WC_Product_Subscription') 
        || is_a($product, 'WC_Product_Subscription_Variation') 
        || is_a($product, 'WC_Product_Variable_Subscription')
    );
    if (!$is_subscription) {
        return $original_price_html;
    }

    // 跳过无固定时长的订阅产品
    $subscription_total_length = $product->get_subscription_length();
    if (empty($subscription_total_length) || $subscription_total_length == 0) {
        return $original_price_html;
    }

    // 读取订阅产品的核心参数
    $subscription_period = $product->get_subscription_period();
    $subscription_pay_interval = $product->get_subscription_interval();
    $regular_unit_price = $product->get_regular_price();
    $current_sale_price = $product->get_sale_price();
    $active_unit_price = $product->get_price();

    // 配置时长单位对应文本,可根据站点语言习惯调整
    $period_text_map = [
        'day' => '天',
        'week' => '周',
        'month' => '个月',
        'year' => '年'
    ];
    $total_subscription_duration = $subscription_total_length * $subscription_pay_interval;
    $duration_display_text = $total_subscription_duration . $period_text_map[$subscription_period];

    // 计算总金额,保留WooCommerce默认的货币格式化样式
    if (!empty($current_sale_price) && $current_sale_price < $regular_unit_price) {
        $total_regular_amount = wc_price($regular_unit_price * $subscription_total_length * $subscription_pay_interval);
        $total_sale_amount = wc_price($active_unit_price * $subscription_total_length * $subscription_pay_interval);
        $custom_price_output = sprintf(
            '<del>%s</del> <ins>%s 共%s</ins>',
            $total_regular_amount,
            $total_sale_amount,
            $duration_display_text
        );
    } else {
        $total_amount = wc_price($active_unit_price * $subscription_total_length * $subscription_pay_interval);
        $custom_price_output = sprintf('%s 共%s', $total_amount, $duration_display_text);
    }

    return $custom_price_output;
}

实际生效效果

  • 原本详情页显示的$10/月(对应3个月固定时长订阅),会自动替换为$30 共3个月
  • 5个月、12个月等不同时长的订阅产品,会自动匹配自身配置的时长参数计算对应总金额,不需要逐产品手动修改展示文案
  • 带折扣的订阅产品会保留WooCommerce默认的原价划线、折扣价高亮的展示样式
  • 规则会同步作用于产品详情页、订阅产品列表、购物车、结账页的价格展示位置,不需要多位置单独调整

新手操作注意事项

  • 代码添加完成后,清空站点缓存插件、服务器层面的静态缓存,确保新的展示规则前端可见
  • 如果需要调整文案格式,比如把“共X个月”改成“订阅X个月总价”,只需要修改代码里的对应拼接文本即可,不需要调整核心计算逻辑
  • 不要将自定义代码添加到WooCommerce Subscriptions插件的核心目录文件中,插件更新时所有文件修改都会被重置

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.23 06:15:04