可以使用 woocommerce_get_formatted_order_total 过滤钩舍入订单总价在格式化为显示在订单总行之前完成:
add_filter( 'woocommerce_get_formatted_order_total', 'round_formatted_order_total', 10, 2 );
function round_formatted_order_total( $formatted_total, $order ) {
$formatted_total = wc_price( round( $order->get_total() ), array( 'currency' => $order->get_currency() ) );
return $formatted_total;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试和工作。
或者,如果您需要对所有订单总数进行四舍五入,您将使用 woocommerce_get_order_item_totals 挂钩,您必须在其中四舍五入您需要的所有行。
在下面的示例中,小计和总计行将被四舍五入:
add_filter( 'woocommerce_get_order_item_totals', 'rounded_formatted_order_totals', 10, 3 );
function rounded_formatted_order_totals( $total_rows, $order, $tax_display ) {
$tax_display = $tax_display ? $tax_display : get_option( 'woocommerce_tax_display_cart' );
// For subtotal line
if ( isset( $total_rows['cart_subtotal'] ) ) {
$subtotal = 0;
foreach ( $order->get_items() as $item ) {
$subtotal += $item->get_subtotal();
if ( 'incl' === $tax_display ) {
$subtotal += $item->get_subtotal_tax();
}
}
$subtotal = wc_price( round( $subtotal ), array( 'currency' => $order->get_currency() ) );
if ( 'excl' === $tax_display && $this->get_prices_include_tax() ) {
$subtotal .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
$total_rows['cart_subtotal']['value'] = $subtotal;
}
// For total line
if ( isset( $total_rows['order_total'] ) ) {
$total = wc_price( round( $order->get_total() ), array( 'currency' => $order->get_currency() ) );
$total_rows['order_total']['value'] = $total;
}
return $total_rows;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试和工作。