Woocommerce 中特定国家/地区的简单基于重量的应税费用

Simple weight based taxable fee for a specific country in Woocommerce

在 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' );

但我需要对这笔费用征税。如何使其纳税?

有些错误,您的代码已过时。请尝试以下 (需支付 应税 费用):

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 文件。已测试并有效。

Make a fee taxable

To make a fee taxable, in the WC_Cart add_fee() method, you need to set the 3rd argument to true (taxable)…

The 4th optional argument is related to the tax class that you can specify if you need to set a specific tax class