add_action( 'woocommerce_cart_calculate_fees', 'custom_fee_based_on_cart_total', 10, 1 );
function custom_fee_based_on_cart_total( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
// The percentage
$percent = 15; // 15%
// The cart total
$cart_total = $cart->cart_contents_total;
// The conditional Calculation
$fee = $cart_total >= 25 ? $cart_total * $percent / 100 : 0;
if ( $fee != 0 )
$cart->add_fee( __( "Extra", "woocommerce" ), $fee, false );
}
我想找一个这样的代码。
例:你购物10元
总金额:10美元额外:11.5美元
我仍然希望总金额是10美元,但只是为了让他们知道如果他们支付15%的 "额外费用",我会送一些额外的东西。
因为这个是2次显示,所以使用
woocommerce_review_order_before_order_total
- 结账页woocommerce_cart_totals_before_order_total
- 购物车页面
function my_custom_fee_based_on_cart_total() {
// optional
//if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
// The percentage
$percent = 15; // 15%
// Get cart total
$cart_total = WC()->cart->get_cart_contents_total();
// The conditional Calculation
$fee = $cart_total >= 25 ? $cart_total * $percent / 100 : 0;
echo '<tr class="my-class"><th>Extra</th><td>' . wc_price($fee) . '</td></tr>';
}
add_action( 'woocommerce_review_order_before_order_total', 'my_custom_fee_based_on_cart_total', 10, 0 );
add_action( 'woocommerce_cart_totals_before_order_total', 'my_custom_fee_based_on_cart_total', 10, 0 );