如何在总和中加%,但只在行中加,而不是实际提升总价。

问题描述 投票:0回答:1
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%的 "额外费用",我会送一些额外的东西。

woocommerce cart percentage
1个回答
1
投票

因为这个是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 ); 
© www.soinside.com 2019 - 2024. All rights reserved.