2

In Woocommerce checkout, I am trying to remove tax if billing_company is not empty:

Here is my code

add_action( 'woocommerce_checkout_update_order_review', 'bbloomer_taxexempt_checkout_based_on_zip' );

function bbloomer_taxexempt_checkout_based_on_zip( $post_data ) {
        global $woocommerce;
        $woocommerce->customer->set_is_vat_exempt( false );
        $Pay_options=$_POST['Pay_options'];
        parse_str($post_data);
        global $woocommerce;

        if ( $billing_company != null) $woocommerce->customer->set_is_vat_exempt( true );
}

In IF statement I also need to check if billing_country is "Croatia".

How can I achieve this?

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
shawtky
  • 15
  • 1
  • 6

3 Answers3

2

You can make the billing company checkout field update checkout on change with the following (without using any additional javascript or jQuery code) and check for country "HR" (Croatia):

add_filter( 'woocommerce_checkout_fields' , 'billing_company_trigger_update_checkout_on_change', 10, 1 );
function billing_company_trigger_update_checkout_on_change( $fields ) {

    $fields['billing']['billing_company']['class'][] = 'update_totals_on_change';

    return $fields;
}

add_action( 'woocommerce_checkout_update_order_review', 'checkout_vat_exempt_based_on_billing_company', 10, 1 );
function checkout_vat_exempt_based_on_billing_company( $post_data ) {
    parse_str($post_data, $results); // Since Php 7.2, the 2nd argument is recommended in parse_str() function
    extract($results);

    $customer = WC()->customer;

    // When billing company is filled and country is Croatia: Exempt taxes
    if ( ! empty($billing_company) && $billing_country === 'HR' && ! $customer->is_vat_exempt() ) {
        $customer->set_is_vat_exempt( true );
    }
    elseif ( $customer->is_vat_exempt() ){
        $customer->set_is_vat_exempt( false );
    }
}

To handle shipping country instead of billing country just replace in the code the variable $billing_country by $shipping_country

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

Note: Since a while, global $woocommerce and $woocommerce are replaced simply by WC()

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • thanks for your answer. I am looking for something similar, but I want to waive the shipping cost. I have tried chaging the code above with `$order = WC()->order;` and `$order->set_shipping_total( 0 );` but my `$order` variable is `null`.. Please advice! – user3253002 Apr 16 '20 at 22:58
0

Because on #billing_company change , there are no action to update checkout. So you need a javascript to do it

add_action('woocommerce_after_checkout_form',function($checkout){
    ?>
    <script type="text/javascript">
    jQuery(function($){
        $(document).on('change','#billing_company',function(){
            $(document.body).trigger("update_checkout");
        });
    });
    </script>
    <?php
});

and in php, you can use your code, and user : $billing_country == 'HR' to check Croatia country

add_action( 'woocommerce_checkout_update_order_review', 'bbloomer_taxexempt_checkout_based_on_zip' );

function bbloomer_taxexempt_checkout_based_on_zip( $post_data ) {
        global $woocommerce;
        $woocommerce->customer->set_is_vat_exempt( false );
        $Pay_options=$_POST['Pay_options'];
        parse_str($post_data);
        if ( $billing_company != null) 
            $woocommerce->customer->set_is_vat_exempt( true );
        if($billing_country == 'HR'){ // Croatia
            // Do what you need, for example set TAX
            $woocommerce->customer->set_is_vat_exempt( false );
        }

}
0

I think the above answers only remove the tax if you update the cart, on the checkout page. It will still have tax on the order confirmation, etc. Here's how to remove tax everywhere:


// from https://elliottrichmond.co.uk/woocommerce-how-to-make-a-user-exempt-from-tax-charges/
function wooc_user_tax_exempt_callback($wc_tax_enabled) {
    $cart = WC()->cart;

    $customer = WC()->customer;

    parse_str($post_data);
    // When billing company is filled and country is Croatia: Exempt taxes
    if ( ! empty($billing_company) && $billing_country === 'HR' && ! $customer->is_vat_exempt() ) {
        $wc_tax_enabled = false;
    }
    return $wc_tax_enabled;

}
add_filter( 'wc_tax_enabled', 'wooc_user_tax_exempt_callback', 10, 1);
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
phreakhead
  • 14,721
  • 5
  • 39
  • 40