Advertisement
businessdad

WooCommerce - Disable VAT calculation in backend

Sep 27th, 2017
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.80 KB | None | 0 0
  1. /**
  2.  * Aelia EU VAT Assistant - Disable tax calculations on the backend for orders that have a "VAT number
  3.  * validated" meta saved against them.
  4.  *
  5.  * HOW TO USE THIS CODE
  6.  * Simply add the code to the bottom of your theme's functions.php file, and it
  7.  * will run automatically. For more information: https://www.skyverge.com/blog/add-custom-code-to-wordpress/
  8.  *
  9.  * GPL DISCLAIMER
  10.  * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
  11.  * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
  12.  * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
  13.  * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
  14.  * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
  15.  *
  16.  * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
  17.  */
  18.  
  19. /**
  20.  * Intercepts the "Recalculate" Ajax call performed by WooCommerce in the backend,
  21.  * when the Recalculate button is clicked, to disable tax calculations for orders
  22.  * that have a valid VAT number associated to it.
  23.  *
  24.  * IMPORTANT
  25.  * This code only checks the value of the '_vat_number_validated' meta. The
  26.  * following aspects are not covered:
  27.  * - Changing the billing country.
  28.  * - Changing the VAT number.
  29.  * - Tax recalculations triggered by operations other than the "Recalculate" button on the Edit Order page.
  30.  *
  31.  * Selecting a different country or VAT number would require a new validation.
  32.  * The EU VAT Assistant is designed to validate the VAT number on the frontend,
  33.  * and won't take any actions for orders created or modified in the backend. Due
  34.  * to that, changing country or VAT number won't affect the data already collected,
  35.  * or the value of the VAT exemption meta.
  36.  */
  37. add_action('wp_ajax_woocommerce_calc_line_taxes', function() {
  38.   // If the EU VAT Assistant is not active, do nothing
  39.   if(empty($GLOBALS['wc-aelia-eu-vat-assistant'])) {
  40.     return;
  41.   }
  42.  
  43.   // Retrieve the order ID
  44.   $order_id = absint($_POST['order_id']);
  45.   if(!is_numeric($order_id)) {
  46.     return;
  47.   }
  48.  
  49.   // Retrieve the value of the "vat_number_validated" meta
  50.   $order = wc_get_order($order_id);
  51.   $vat_number_validated = $order->get_meta('_vat_number_validated');
  52.  
  53.   // If the VAT number was validated, disable the tax calculation
  54.   // altogether. This is necessary because WooCommerce doesn't allow
  55.   // to set a VAT exemption on the customer, during this operation
  56.   if($vat_number_validated === 'valid') {
  57.     add_filter('wc_tax_enabled', function($tax_enabled) {
  58.       return false;
  59.     });
  60.   }
  61. }, 1, 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement