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

如何在WooCommerce管理面板中显示价格千位分隔符

WooCommerce管理面板价格千位分隔符不生效修复方案

WooCommerce后台默认不会读取「设置-常规」里配置的千位分隔符参数,这套格式化规则仅对前台渲染逻辑生效,后台商品编辑、订单明细、报表页面的价格输出直接读取原始数字元数据,没有套用格式化过滤器,需要手动挂载过滤器实现规则同步。

推荐实现方案:自定义代码片段

将以下代码添加到当前子主题的functions.php文件,或者使用自定义代码片段插件插入即可,无需修改WooCommerce核心文件:

/**
 * 同步WooCommerce前台千位分隔符配置到后台管理页面
 */
// 全局后台价格展示格式化
add_filter('woocommerce_format_price', function($formatted_price, $price, $args) {
    if (!is_admin() || !is_numeric($price)) {
        return $formatted_price;
    }
    $thousand_sep = wc_get_price_thousand_separator();
    $decimal_sep = wc_get_price_decimal_separator();
    $decimals = wc_get_price_decimals();
    return number_format((float)$price, $decimals, $decimal_sep, $thousand_sep);
}, 20, 3);

// 适配后台订单页、报表页金额展示
add_filter('woocommerce_get_order_item_totals', function($total_rows, $order, $tax_display) {
    if (!is_admin()) {
        return $total_rows;
    }
    foreach ($total_rows as $key => $row) {
        if (!isset($row['value'])) continue;
        $raw_value = str_replace([wc_get_price_thousand_separator(), wc_get_price_decimal_separator(), get_woocommerce_currency_symbol()], ['', '.', ''], $row['value']);
        if (is_numeric(trim($raw_value))) {
            $total_rows[$key]['value'] = wc_price((float)trim($raw_value));
        }
    }
    return $total_rows;
}, 20, 3);

可选配置(按需开启)

如果需要让商品编辑页的价格输入框也显示千位分隔符,可以额外添加以下代码。注意:部分老旧的第三方商品插件可能无法识别带分隔符的输入值,导致价格保存异常,生产环境建议先在测试站验证后再开启:

// 商品编辑页价格输入框展示千位分隔符
add_filter('woocommerce_product_get_regular_price', function($price, $product) {
    if (!is_admin() || !is_numeric($price) || (defined('DOING_AJAX') && DOING_AJAX)) {
        return $price;
    }
    return number_format((float)$price, wc_get_price_decimals(), wc_get_price_decimal_separator(), wc_get_price_thousand_separator());
}, 20, 2);

add_filter('woocommerce_product_get_sale_price', function($price, $product) {
    if (!is_admin() || !is_numeric($price) || (defined('DOING_AJAX') && DOING_AJAX)) {
        return $price;
    }
    return number_format((float)$price, wc_get_price_decimals(), wc_get_price_decimal_separator(), wc_get_price_thousand_separator());
}, 20, 2);

生效验证

  • 代码添加完成后,清空WordPress对象缓存、浏览器缓存
  • 刷新商品列表、订单编辑页,即可看到价格已按你配置的千位分隔符规则展示
  • 如果安装了多币种、价格批量编辑类插件,可将代码中过滤器的优先级参数(即add_filter里的数字20)调整为99,避免被其他插件的逻辑覆盖

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 19:15:46