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

WooCommerce数量变更自动更新价格时千位分隔符缺失问题求助

解决WooCommerce数量变更时价格千位分隔符缺失问题

你可以通过JavaScript的Intl.NumberFormat API实现千位分隔符格式化,完美匹配你需要的123.456 Ft显示样式。

修改后的完整代码:

$price = $product->get_price();
[...]
wc_enqueue_js( "      
    $( '[name=quantity]' ).on( 'input change', function() {
        var qty = $( this ).val();
        var price = '" . esc_js( $price ) . "';
        var total = price * qty;
        // 按匈牙利格式格式化数字(千位用点分隔)
        var formattedPrice = new Intl.NumberFormat('hu-HU', {
            style: 'decimal',
            minimumFractionDigits: 0,
            maximumFractionDigits: 0
        }).format(total);
        
        if ( qty > 1 ) {
            $('#subtot > span').html(formattedPrice + ' " . esc_js( $currency ) . "');
        } else {
            $( '#subtot > span').html( '' );
        }
    });
" );

核心说明:

  • Intl.NumberFormat('hu-HU'):指定匈牙利地区数字格式,该地区默认用点作为千位分隔符,完全契合你的需求;若需适配其他地区,替换对应locale代码即可。
  • minimumFractionDigits和maximumFractionDigits设为0,确保价格只显示整数部分,和原代码toFixed(0)效果一致。
  • 修正了原代码中字符串拼接的引号语法问题,避免JS运行报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 12:52:20