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

如何在WooCommerce店铺页展示两种不同价格格式(部分带sup标签小数)

问题原因

你使用的formatted_woocommerce_price是WooCommerce全局价格格式化过滤器,所有调用wc_price()生成的价格都会自动应用小数部分加<sup>标签的逻辑,因此原价、节省金额也会被修改格式。

解决方案

推荐使用临时移除过滤器的方案,无需修改原有sup格式化逻辑,改造成本最低:
修改你现有的woocommerce_get_price_html钩子代码如下:

add_filter( 'woocommerce_get_price_html', 'bbloomer_simple_product_price_format', 10, 2 );
function bbloomer_simple_product_price_format( $price, $product ) {
   if ( $product->is_on_sale() && $product->is_type('simple') ) {
      // 临时移除小数sup格式化规则
      remove_filter( 'formatted_woocommerce_price', 'ts_woo_decimal_price', 10 );
      // 生成常规格式的原价、节省金额
      $regular_price = wc_price( $product->get_regular_price() );
      $save_amount = wc_price( $product->get_regular_price() - $product->get_sale_price() );
      // 恢复sup格式化规则,保证促销价等其他场景正常生效
      add_filter( 'formatted_woocommerce_price', 'ts_woo_decimal_price', 10, 5 );

      $info_price = sprintf( 
          __( '<div class="was-now-save" style="padding:0 10px 0 10px;"><div class="was">Was: %1$s</div><div class="save">Save: %2$s</div></div>', 'woocommerce' ), 
          $regular_price, 
          $save_amount 
      );
   }
   return '<div style="display:flex; align-items: center; justify-content: center;">' . $price . '<span>' . $info_price . '</span></div>';
}

修改完成后,促销价的小数sup格式会保留,原价和节省金额会显示为常规价格格式,不会被加上sup标签。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 18:06:01