WooCommerce 中的圆形购物车小计
Round cart subtotal in WooCommerce
使用此代码时,我可以操纵小计(它工作正常)
add_action( 'woocommerce_calculate_totals', 'action_cart_calculate_totals', 10, 1 );
function action_cart_calculate_totals( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( !WC()->cart->is_empty() ):
## Displayed subtotal (+10%)
$cart_object->subtotal *= 1.1;
## Displayed TOTAL (+10%)
// $cart_object->total *= 1.1;
## Displayed TOTAL CART CONTENT (+10%)
// $cart_object->cart_contents_total *= 1.1;
endif;
}
但我想使用下面的代码,用于新的 subtotal
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;
}
然而这并没有达到预期的效果。如何在第一个提到的函数中使用这个舍入函数?
woocommerce_calculated_total
hook 用于允许插件过滤总计,并在修改时对购物车总计求和。
对于 subtotal
,请改用 woocommerce_cart_subtotal
过滤器挂钩。
所以你得到:
function filter_woocommerce_cart_subtotal( $subtotal, $compound, $cart ) {
// Rounds a float
$round_num = round( $cart->subtotal / 0.05 ) * 0.05;
// Format a number with grouped thousands
$number_format = number_format( $round_num, 2 );
// Subtotal
$subtotal = wc_price( $number_format );
return $subtotal;
}
add_filter( 'woocommerce_cart_subtotal', 'filter_woocommerce_cart_subtotal', 10, 3 );
使用此代码时,我可以操纵小计(它工作正常)
add_action( 'woocommerce_calculate_totals', 'action_cart_calculate_totals', 10, 1 );
function action_cart_calculate_totals( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( !WC()->cart->is_empty() ):
## Displayed subtotal (+10%)
$cart_object->subtotal *= 1.1;
## Displayed TOTAL (+10%)
// $cart_object->total *= 1.1;
## Displayed TOTAL CART CONTENT (+10%)
// $cart_object->cart_contents_total *= 1.1;
endif;
}
但我想使用下面的代码,用于新的 subtotal
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;
}
然而这并没有达到预期的效果。如何在第一个提到的函数中使用这个舍入函数?
woocommerce_calculated_total
hook 用于允许插件过滤总计,并在修改时对购物车总计求和。
对于 subtotal
,请改用 woocommerce_cart_subtotal
过滤器挂钩。
所以你得到:
function filter_woocommerce_cart_subtotal( $subtotal, $compound, $cart ) {
// Rounds a float
$round_num = round( $cart->subtotal / 0.05 ) * 0.05;
// Format a number with grouped thousands
$number_format = number_format( $round_num, 2 );
// Subtotal
$subtotal = wc_price( $number_format );
return $subtotal;
}
add_filter( 'woocommerce_cart_subtotal', 'filter_woocommerce_cart_subtotal', 10, 3 );