删除 WooCommerce 中最小购物车总数和特定国家/地区的所有税费

时间:2021-03-12 19:58:55

标签: php wordpress woocommerce cart tax

我们需要对超过 150 欧元的英国订单收取 0 增值税,包括运费和支付网关费用,但不包括 20% 的正常增值税。

因此,如果英国住宅地址以 130 的价格订购某样东西并且运输和支付网关费用为 9,那么我们收取增值税,因此客户支付 139+9+20% 的增值税,但是,如果订单为 130,并且运输和支付网关费用是 23,所以总共 153 不含增值税,我们不收税。

我创建了这个,但它仍然对运费征税,而且 PayPal 附加费用也被征税,我的脑袋被绞碎了,真的以为我会寻求建议。



add_action( 'woocommerce_before_calculate_totals','auto_add_tax_for_room', 10, 1 );
function auto_add_tax_for_room( $cart ) {
    if ( is_admin() && ! defined('DOING_AJAX') ) return; 
    
    $shipping_country = WC()->customer->get_shipping_country();
    $subtotal = 0;
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $subtotal += $cart_item['data']->get_price('edit') * $cart_item['quantity'];
    }
    $subtotal = intval($subtotal);
    

    if($shipping_country!='GB' || $subtotal < 125) return;
    
    $percent = 0;
    // Calculation
    $surcharge = ( $cart->cart_contents_total + $cart->shipping_total ) * $percent / 100;

    // Add the fee (tax third argument disabled: false)
   foreach ( $cart->get_cart() as $cart_item ) {
        // get product price
        $price = $cart_item['data']->get_price();

        $cart_item['data']->set_tax_class( 'Zero rate' ); // Above 2500
    }
    return;
}

2 个答案:

答案 0 :(得分:3)

钩子 woocommerce_before_calculate_totals 仅适用于购物车项目,此处的税级仅适用于产品,因为 $cart_item['data']WC_Product 对象。

如果您设定的产品价格不含税,那么还有另一种更简单的替代方案,可以在满足您的条件时取消所有税费。

请尝试以下操作:

add_action( 'woocommerce_calculate_totals', 'set_customer_tax_exempt' );
function set_customer_tax_exempt( $cart ) {
    if ( is_admin() && ! defined('DOING_AJAX') )
        return;

    $min_amount = 150; // Minimal cart amount
    $countries  = array('GB'); // Defined countries array

    $subtotal   = floatval( $cart->cart_contents_total );
    $shipping   = floatval( $cart->shipping_total );
    $fees       = floatval( $cart->fee_total );
    $total      = $subtotal + $shipping + $fees; // cart total (without taxes including shipping and fees)

    $country    = WC()->customer->get_shipping_country();
    $country    = empty($country) ? WC()->customer->get_billing_country() : $country;
    $vat_exempt = WC()->customer->is_vat_exempt();
    $condition  = in_array( $country, $countries ) && $total >= $min_amount;

    if ( $condition && ! $vat_exempt ) {
        WC()->customer->set_is_vat_exempt( true ); // Set customer tax exempt
    } elseif ( ! $condition && $vat_exempt ) {
        WC()->customer->set_is_vat_exempt( false ); // Remove customer tax exempt
    }
}

add_action( 'woocommerce_thankyou', 'remove_customer_tax_exempt' );
function remove_customer_tax_exempt( $order_id ) {
    if ( WC()->customer->is_vat_exempt() ) {
        WC()->customer->set_is_vat_exempt( false );
    }
}

代码位于活动子主题(或活动主题)的functions.php 文件中。经测试有效。

答案 1 :(得分:0)

感谢 LoicTheAztec 的帮助,我最终使用了以下内容进行了一些更改,并解决了英国的税收问题

add_action( 'woocommerce_before_calculate_totals','auto_add_tax_for_room', 10, 1 );
function auto_add_tax_for_room( $cart ) {
    if ( is_admin() && ! defined('DOING_AJAX') ) return; 
    
    $shipping_country = WC()->customer->get_shipping_country();
    $subtotal = 0;
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $subtotal += $cart_item['data']->get_price('edit') * $cart_item['quantity'];
    }
    $subtotal = intval($subtotal);  

    if($shipping_country!='GB' || $subtotal < 125) return;
    
    $percent = 0;
    // Calculation
    $surcharge = ( $cart->cart_contents_total + $cart->shipping_total ) * $percent / 100;

    // Add the fee (tax third argument disabled: false)
   foreach ( $cart->get_cart() as $cart_item ) {
        // get product price
        $price = $cart_item['data']->get_price();

        $cart_item['data']->set_tax_class( 'Zero rate' ); 
       
    }
    return;
}

function flat_rates_cost( $rates, $package ) {
    $shipping_country = WC()->customer->get_shipping_country();
    
    $subtotal = 0;
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $subtotal += $cart_item['data']->get_price('edit') * $cart_item['quantity'];
    }
    $subtotal = intval($subtotal);
    
    if($shipping_country!='GB' || $subtotal < 125) return $rates;
    
    foreach ( $rates as $rate_key => $rate ){
        if ( 'free_shipping' !== $rate->method_id ) {
            $has_taxes = false;
            $taxes = [];
 
            // Taxes rate cost (if enabled)
            foreach ($rates[$rate_key]->taxes as $key => $tax){
                if( $tax > 0 ){
                    $has_taxes = true;
                    $taxes[$key] = 0; // Set to 0 (zero)
                }
            }
            if( $has_taxes )
                $rates[$rate_key]->taxes = 0;
        }
    }
    return $rates;
}
add_filter( 'woocommerce_package_rates', 'flat_rates_cost', 10, 2 );