Issues with applying VAT exemption only to products and not to shipping costs in WooCommerce

I’m having an issue with my WooCommerce store that I haven’t been able to solve despite multiple attempts and code adjustments.

I want the VAT to be removed only from products and not from shipping costs when a valid VAT number (for EU businesses) is entered. Despite various code tweaks and settings adjustments, the VAT is being deducted from both products and shipping costs.

Current Code:

// JavaScript for validating UID and handling VAT exemption
add_action('wp_enqueue_scripts', 'custom_enqueue_script');
function custom_enqueue_script() {
    if (is_checkout()) {
        wp_add_inline_script('jquery', "
            jQuery(document).ready(function($) {
                function showError(message, field) {
                    var errorElement = '<div class="validation-error" style="color: red; font-weight: bold;">' + message + '</div>';
                    if (field.next('.validation-error').length === 0) {
                        field.after(errorElement);
                    }
                }

                function removeError(field) {
                    field.next('.validation-error').remove();
                }

                function validateUID(country, uid) {
                    var regex;
                    var upperUID = uid.toUpperCase(); // Convert UID to uppercase
                    switch (country) {
                        case 'AT': regex = /^ATU[0-9]{8}$/; break;
                        case 'BE': regex = /^BE[0-9]{10}$/; break;
                        case 'BG': regex = /^BG[0-9]{9,10}$/; break;
                        case 'CY': regex = /^CY[0-9]{8}[A-Z]$/; break;
                        case 'CZ': regex = /^CZ[0-9]{8,10}$/; break;
                        case 'DE': regex = /^DE[0-9]{9}$/; break;
                        case 'DK': regex = /^DK[0-9]{8}$/; break;
                        case 'EE': regex = /^EE[0-9]{9}$/; break;
                        case 'EL': regex = /^EL[0-9]{9}$/; break;
                        case 'ES': regex = /^ES[A-Z0-9][0-9]{7}[A-Z0-9]$/; break;
                        case 'FI': regex = /^FI[0-9]{8}$/; break;
                        case 'FR': regex = /^FR[A-Z0-9]{2}[0-9]{9}$/; break;
                        case 'HR': regex = /^HR[0-9]{11}$/; break;
                        case 'HU': regex = /^HU[0-9]{8}$/; break;
                        case 'IE': regex = /^IE[0-9][A-Z0-9+*][0-9]{5}[A-Z]{1,2}$/; break;
                        case 'IT': regex = /^IT[0-9]{11}$/; break;
                        case 'LT': regex = /^LT[0-9]{9,12}$/; break;
                        case 'LU': regex = /^LU[0-9]{8}$/; break;
                        case 'LV': regex = /^LV[0-9]{11}$/; break;
                        case 'MT': regex = /^MT[0-9]{8}$/; break;
                        case 'NL': regex = /^NL[0-9]{9}B[0-9]{2}$/; break;
                        case 'PL': regex = /^PL[0-9]{10}$/; break;
                        case 'PT': regex = /^PT[0-9]{9}$/; break;
                        case 'RO': regex = /^RO[0-9]{2,10}$/; break;
                        case 'SE': regex = /^SE[0-9]{12}$/; break;
                        case 'SI': regex = /^SI[0-9]{8}$/; break;
                        case 'SK': regex = /^SK[0-9]{10}$/; break;
                        default: return false;
                    }
                    return regex.test(upperUID);
                }

                function checkCompanyFields() {
                    var billing_company = $('#billing_company').val();
                    var billing_firma_uid = $('#billing_firma_uid').val();
                    var billing_country = $('#billing_country').val();

                    removeError($('#billing_company'));
                    removeError($('#billing_firma_uid'));

                    if (billing_company && !billing_firma_uid) {
                        showError('Please enter a VAT number if you fill in the company name.', $('#billing_firma_uid'));
                    } else if (!billing_company && billing_firma_uid) {
                        showError('Please enter a company name if you fill in the VAT number.', $('#billing_company'));
                    } else if (billing_firma_uid && !validateUID(billing_country, billing_firma_uid)) {
                        showError('Please enter a valid VAT number.', $('#billing_firma_uid'));
                    } else {
                        $.ajax({
                            type: 'POST',
                            url: '" . admin_url('admin-ajax.php') . "',
                            data: {
                                action: 'check_company_field',
                                billing_company: billing_company,
                                billing_firma_uid: billing_firma_uid,
                                billing_country: billing_country,
                            },
                            success: function(response) {
                                $('body').trigger('update_checkout');
                            }
                        });
                    }
                }

                $('body').on('change', '#billing_company, #billing_firma_uid, #billing_country', function() {
                    checkCompanyFields();
                });

                checkCompanyFields();
            });
        ");
    }
}

// Handle the AJAX request to check the company field and set VAT exemption
add_action('wp_ajax_check_company_field', 'custom_check_company_field');
add_action('wp_ajax_nopriv_check_company_field', 'custom_check_company_field');
function custom_check_company_field() {
    $eu_countries = array(
        'AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FR', 'GR', 'HR', 'HU', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK'
    );

    $billing_country = isset($_POST['billing_country']) ? $_POST['billing_country'] : '';

    if (in_array($billing_country, $eu_countries) && isset($_POST['billing_company']) && !empty($_POST['billing_company']) && isset($_POST['billing_firma_uid']) && !empty($_POST['billing_firma_uid'])) {
        WC()->customer->set_is_vat_exempt(true);
        write_log('VAT exempt set to true');
    } else {
        WC()->customer->set_is_vat_exempt(false);
        write_log('VAT exempt set to false');
    }
    WC()->cart->calculate_totals();
    wp_die();
}

// Customize the tax class for products based on VAT exemption
add_filter('woocommerce_product_get_tax_class', 'customize_product_tax_class', 1, 2);
function customize_product_tax_class($tax_class, $product) {
    if (WC()->customer->is_vat_exempt() && !is_admin()) {
        write_log('Product tax class set to zero-rate for product: ' . $product->get_name());
        return 'zero-rate';
    }
    return $tax_class;
}

// Ensure shipping tax class is not affected by VAT exemption
add_filter('woocommerce_shipping_tax_class', 'set_shipping_tax_class', 10, 2);
function set_shipping_tax_class($tax_class, $instance) {
    write_log('Shipping tax class set to standard for instance: ' . $instance->get_id());
    return 'standard'; // Set shipping tax class to standard
}

// Debugging the shipping tax rates
add_action('woocommerce_cart_calculate_fees', 'debug_shipping_tax_rates');
function debug_shipping_tax_rates() {
    $packages = WC()->shipping->get_packages();
    foreach ($packages as $package) {
        foreach ($package['rates'] as $rate) {
            write_log('Shippin

I have implemented the provided code to validate the VAT number and apply VAT exemption in WooCommerce. I expected the VAT exemption to apply only to products and not to shipping costs. However, the VAT is being removed from both products and shipping costs. I have ensured that the shipping tax class is set to “Standard” and the product tax class for VAT exemption is set to “zero-rate”. Despite these settings, the issue persists.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật