这里有一个解决方案。但是您必须自己对 woocommerce 相关模板进行所有必要的更改。
首先,如果您不知道如何正确自定义 WooCommerce 模板,请阅读以下内容:
Template Structure + Overriding Templates via a Theme
然后现在使用下面的自定义函数来处理显示的格式化 html 价格(将价格从 4 位小数更改为 2 位小数并保留 html 标签),您将能够对 woocommerce 相关模板进行必要的更改:
function wc_shrink_price( $price_html ){
// Extract the price (without formatting html code)
$price = floatval(preg_replace('/[^0-9\.,]+/', '', $price_html));
// Round price with 2 decimals precision
$shrink_price = round($price, 2);
// Replace old existing price in the original html structure and return the result
return str_replace($price, $shrink_price, $price_html);
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。
此代码已经过测试并且可以工作
用法示例:
在 33 行的 woocommerce 模板 cart/cart_totals.php 上,您有以下原始代码:
(用于小计显示价格)
<td data-title="<?php esc_attr_e( 'Subtotal', 'woocommerce' ); ?>"><?php wc_cart_totals_subtotal_html(); ?></td>
如果你搜索 wc_cart_totals_subtotal_html() 函数,你会看到它正在使用这个 WC_Cart 方法:WC()->cart->get_cart_subtotal() ...
所以你可以这样替换它:
<td data-title="<?php esc_attr_e( 'Subtotal', 'woocommerce' ); ?>">
<?php
// Replacement function by a WC_Cart method
$subtotal_html_price = WC()->cart->get_cart_subtotal();
// Here we use our custom function to get a formated html price with 2 decimals
echo wc_shrink_price( $subtotal_html_price );
?>
</td>
如您所见,您需要对所有购物车价格执行类似的操作。
购物车和结帐模板位于 cart 和 checkout 子文件夹中……
现在是你工作的时候了!