您上面的过滤器(返回 true)会触发 wc_trim_zeros() function 的执行,它确实仅在价格只有 0 位小数时才会删除零。
您需要改用formatted_woocommerce_price hook。
以下过滤器将删除所有尾随零:
add_filter('formatted_woocommerce_price', function($formatted_price, $price, $decimals, $decimal_separator) {
// Need to trim 0s only if we have the decimal separator present.
if (strpos($formatted_price, $decimal_separator) !== false) {
$formatted_price = rtrim($formatted_price, '0');
// After trimming trailing 0s, it may happen that the decimal separator will remain there trailing... just get rid of it, if it's the case.
$formatted_price = rtrim($formatted_price, $decimal_separator);
}
return $formatted_price;
}, 10, 4);
更新:如果它是第三个和最后一个小数,下面的代码只会删除一个尾随零:
add_filter('formatted_woocommerce_price', function($formatted_price, $price, $decimals, $decimal_separator) {
// Need to trim 0s only if we have the decimal separator present.
if (strpos($formatted_price, $decimal_separator) !== false) {
$formatted_price = preg_replace('/^(\d+' . preg_quote($decimal_separator, '/' ) . '\d{2})0$/', "$1", $formatted_price);
}
return $formatted_price;
}, 10, 4);
免责声明:代码未试用,直接写在答案框中。