在 WooCommerce 中应用优惠券后如何舍入价格?

How to round price after applying coupon in WooCommerce?

我已将以下代码添加到我的 function.php,但在使用优惠券后价格没有四舍五入。

我正在使用 woocommerce 2.5.5。

这是我的代码:

add_filter( 'woocommerce_get_price_excluding_tax', 'round_price_product', 10, 1 );
add_filter( 'woocommerce_get_price_including_tax', 'round_price_product', 10, 1 );
add_filter( 'woocommerce_tax_round', 'round_price_product', 10, 1);
add_filter( 'woocommerce_get_price', 'round_price_product', 10, 1);

function round_price_product( $price ){
    // Return rounded price
    return round( $price );
}

怎么了?

您在函数中 return 赋值,相反,您可能需要 return $price 变量:

add_filter( 'woocommerce_get_price_excluding_tax', 'round_price_product', 10, 1 );
add_filter( 'woocommerce_get_price_including_tax', 'round_price_product', 10, 1 );
add_filter( 'woocommerce_tax_round', 'round_price_product', 10, 1);
add_filter( 'woocommerce_get_price', 'round_price_product', 10, 1);

function round_price_product( $price ){
  //Store and Return rounded price
  $price = round( $price );
  return $price;
}