WooCommerce :根据购物车总数添加费用


WooCommerce : Adding fee based on the cart total

我正在尝试根据购物车总数添加费用。但是费用增加了woocommerce_cart_calculate_fees.但我不知道如何根据购物车总数来制作它。当更新或删除时,总数会发生变化,因此我无法获得适当的总数和添加费用。

function woo_add_cart_fee() {
    $cart_total = ;
    $fee = (int)$cart_total*5/100;
    WC()->cart->add_fee( 'Credits :', $fee, false, '' );
        }
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

这是我的代码。我找不到路。谁能帮忙。?提前谢谢。

试试这个:

function woo_dm_add_custom_fees(){
    $cart_total = 0;
    foreach( WC()->cart->get_cart() as $item ){ 
        $cart_total += $item["line_total"];
    }
    $fee = (int)$cart_total*5/100;
    WC()->cart->add_fee( "Credits :", $fee, false, '' );
}
add_action( 'woocommerce_cart_calculate_fees' , 'woo_dm_add_custom_fees' );
add_action( 'woocommerce_after_cart_item_quantity_update', 'woo_dm_add_custom_fees' );