/** * Aelia EU VAT Assistant - Disable tax calculations on the backend for orders that have a "VAT number * validated" meta saved against them. * * HOW TO USE THIS CODE * Simply add the code to the bottom of your theme's functions.php file, and it * will run automatically. For more information: https://www.skyverge.com/blog/add-custom-code-to-wordpress/ * * GPL DISCLAIMER * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law. * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is" * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction. * * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us */ /** * Intercepts the "Recalculate" Ajax call performed by WooCommerce in the backend, * when the Recalculate button is clicked, to disable tax calculations for orders * that have a valid VAT number associated to it. * * IMPORTANT * This code only checks the value of the '_vat_number_validated' meta. The * following aspects are not covered: * - Changing the billing country. * - Changing the VAT number. * - Tax recalculations triggered by operations other than the "Recalculate" button on the Edit Order page. * * Selecting a different country or VAT number would require a new validation. * The EU VAT Assistant is designed to validate the VAT number on the frontend, * and won't take any actions for orders created or modified in the backend. Due * to that, changing country or VAT number won't affect the data already collected, * or the value of the VAT exemption meta. */ add_action('wp_ajax_woocommerce_calc_line_taxes', function() { // If the EU VAT Assistant is not active, do nothing if(empty($GLOBALS['wc-aelia-eu-vat-assistant'])) { return; } // Retrieve the order ID $order_id = absint($_POST['order_id']); if(!is_numeric($order_id)) { return; } // Retrieve the value of the "vat_number_validated" meta $order = wc_get_order($order_id); $vat_number_validated = $order->get_meta('_vat_number_validated'); // If the VAT number was validated, disable the tax calculation // altogether. This is necessary because WooCommerce doesn't allow // to set a VAT exemption on the customer, during this operation if($vat_number_validated === 'valid') { add_filter('wc_tax_enabled', function($tax_enabled) { return false; }); } }, 1, 1);