skip to Main Content

we need to charge 0 VAT for UK orders that are greater than 150 euro including shipping and payment gateway fees but excluding the 20% normal VAT.

So if a British residential address orders something at 130 and shipping and payment gateway fees are 9 then we charge VAT so the customer pays 139+9+20% VAT, but, if the order is 130 and shipping and payment gateway fees are 23 so a total of 153 without VAT we charge no tax.

I created this but still, it’s taxing shipping fees and PayPal added fees also are getting taxed, my head is minced really thought I’d reach out for suggestions.



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

Answers


  1. Chosen as BEST ANSWER

    Thanks to LoicTheAztec help I made some changes ended up using the following and it worked to fix the UK taxes issues

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


  2. The hook woocommerce_before_calculate_totals is just for cart items and the tax class here only apply to products as $cart_item['data'] is the WC_Product Object.

    If your product prices are set without taxes, there is another alternative much more simpler that will remove all taxes when your conditions are met.

    Try the following instead:

    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 );
        }
    }
    

    Code goes in functions.php file of the active child theme (or active theme). Tested and works.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search