根据Woocommerce中的购物车总额添加动态分层费用

时间:2018-05-28 10:18:38

标签: php wordpress woocommerce cart fee

add_action( 'woocommerce_cart_calculate_fees', 'custom_fee_based_on_cart_total', 10, 1 );
function custom_fee_based_on_cart_total( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    // The percetage
    $value = 12; // 15%
    // The cart total
    $cart_total = $cart_object->cart_contents_total; 

    // The conditional Calculation
    $fee = $cart_total >= 25 ? $cart_total * $value / 100 : 0;

    if ( $fee != 0 ) 
        $cart_object->add_fee( __( "Kosten MandaBai", "woocommerce" ), $fee, false );
}

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

add_action( 'woocommerce_cart_calculate_fees', 'tiered_fee_based_on_cart_total', 10, 1 );
function tiered_fee_based_on_cart_total( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) 
        return;

    $total = $cart->cart_contents_total; // The cart total

    if( $total < 50 )
        $fee = 8;
    elseif( $total >= 50 && $total < 100 )
        $fee = 10;
    else
        $fee = 12;

    if ( $fee != 0 ) 
        $cart->add_fee( __( "Kosten MandaBai", "woocommerce" ), -$fee, false );
}

代码放在活动子主题(或活动主题)的functions.php文件中。经过测试和工作。