woocommerce 中的四舍五入价格

Rounding price in woocommerce

我正在尝试在结帐页面上将 WC 中的价格四舍五入,如果产品价格为 70,55,则 TAX (+20%) = 14,11

那么,如何将最终价格四舍五入到 14.10,如果接近 15,则如何四舍五入到 14.15,就在 5 或 0

您可以使用 add_filter( 'woocommerce_calculated_total', 'your_custom_function_name_here' );,然后像下面这样编写自定义函数以四舍五入到最接近的 5 美分...即 0.05 或 0.10:

add_filter( 'woocommerce_calculated_total', 'my_custom_roundoff' );
function my_custom_roundoff( $total ) {
$round_num = round($total / 0.05) * 0.05;
$total = number_format($round_num, 2); // this is required for showing zero in the last decimal
return $total;
}