【问题标题】:Add dynamic tiered fee based on cart total in Woocommerce根据 Woocommerce 中的购物车总数添加动态分层费用
【发布时间】:2018-05-28 10:18:38
【问题描述】:
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 );
}

【问题讨论】:

  • 我试过上面的代码,效果很好,但我需要一些基于购物车总价格而不是总购物车“百分比”的东西。例如:总购物车介于:0 - 49,99 费用必须 = 8 总购物车介于:50 - 99.99 费用必须 = 10 总购物车 100 以上费用必须 = 12
  • 您可以使用 $min 和 $max 进行检查,例如检查 $cart_total

标签: php wordpress woocommerce cart fee


【解决方案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 文件中。经过测试并且可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    相关资源
    最近更新 更多