1

在 Woocommerce 中,我使用以下代码块根据特定国家/地区的总重量在购物车和结帐中添加自定义费用:

function weight_add_cart_fee() {

    // Set here your percentage
    $percentage = 0.17;

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

    // Get weight of all items in the cart
    $cart_weight = WC()->cart->get_cart_contents_weight();

    // calculate the fee amount
    $fee = $cart_weight * $percentage;

    // If weight amount is not null, adds the fee calcualtion to cart

    global $woocommerce;
    $country = $woocommerce->customer->get_country();
    if ( !empty( $cart_weight ) && $country == 'SK'  ) { 
        WC()->cart->add_fee( __('Recyklačný poplatok (podľa váhy): ', 'my_theme_slug'), $fee, false );
    }
}
add_action( 'woocommerce_cart_calculate_fees','weight_add_cart_fee' );

但我需要将此费用征税。如何使其应税?

4

1 回答 1

1

有一些错误,您的代码已过时。请尝试以下方法需缴纳应税费用)

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

    $percentage       = 0.17; // Percentage
    $targeted_country = 'SK'; // Country
    $cart_weight      = $cart->get_cart_contents_weight(); // Total weight
   

    if ( $cart_weight > 0 && WC()->customer->get_shipping_country() == $targeted_country ) {
        $cart->add_fee( __('Recyklačný poplatok (podľa váhy): ', 'my_theme_slug'), ($cart_weight * $percentage), true );
    }
}

代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作。

将费用征税

要使费用应纳税,在WC_Cart add_fee()方法中,您需要将第三个参数设置为 true(应纳税)...</p>

第 4 个可选参数与tax class您可以指定是否需要设置特定税级相关

于 2019-03-07T14:06:38.227 回答