/** * Aelia EU VAT Assistant for WooCommerce - Validate VAT number and store VAT information against * an existing order. * * HOW TO USE THIS CODE * This code shows how to use the EU VAT Assistant to call the VIES service to validate a EU VAT number * and how to store or update the VAT information collected during that process. You can wrap this code * in a plugin, and wrap it into a function, then call it on Ajax events such as "wp_ajax_woocommerce_calc_line_taxes" * or "wp_ajax_collect_order_vat_info". * Note: when linking the execution of the code to the Ajax event, you should use a high priority (e.g. 1), so that * your custom code runs before the "standard" one. This is important because, normally, Ajax handler terminate the * script after running, and that would prevent your code from being executed. * * @see WC_AJAX::calc_line_taxes() * @see Aelia\WC\EU_VAT_Assistant\WC_Aelia_EU_VAT_Assistant::wp_ajax_collect_order_vat_info() * * IMPORTANT * This code will not remove VAT from an existing order that had taxes added to it, nor will it refund * VAT that has already been paid. If a customer placed an order and paid VAT, e.g. because they forgot, * to enter their VAT number, or because their number, you will have to refund such VAT manually. We recommend * to consult your tax advisor to discuss the best way to handle this process, in compliance with your local * tax laws. * * DISCLAIMER * THE USE OF THIS CODE IS AT YOUR OWN RISK. You remain fully liable for compliance with tax laws. * This code is offered free of charge and 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. * * The code is provided as an example and it's not covered by our support service. We won't be able to offer free support * in relation to it. Should you need a consultation, or assistance to customise this code, you can contact us to avail * of our paid consultation services: https://aelia.co/hire_us */ // Replace "123" with the ID of an actual order $order = new Aelia\WC\EU_VAT_Assistant\Order(123); // Call the VAT validation function. This call assumes that the order // already has a VAT number stored in its "vat_number" meta $raw_vies_response = apply_filters('wc_aelia_eu_vat_assistant_validate_vat_number', null, $order->get_billing_country(), $order->get_meta('vat_number'), true); if(!empty($raw_vies_response)) { // Extract the VAT validation $vat_number_validated = $raw_vies_response['euva_validation_result']; $order->update_meta_data('_vat_number_validated', $vat_number_validated); // Store the VIES response against the order $order->update_meta_data('vies_response', $raw_vies_response); if(is_array($raw_vies_response) && isset($raw_vies_response['raw_response']) && is_array($raw_vies_response['raw_response'])) { // Change all the keys in the response to lower-case. This is to avoid issues with keys being // returned like "requestIdentifier", "RequestIdentifier", "requestidentifier" by different versions // of the VIES service $raw_vies_response['raw_response'] = array_change_key_case($raw_vies_response['raw_response'], CASE_LOWER); } // Store the consultation number, if present if(!empty($raw_vies_response['raw_response']['requestidentifier'])) { $order->update_meta_data('vies_consultation_number', $raw_vies_response['raw_response']['requestidentifier']); } else { $order->update_meta_data('vies_consultation_number', __('Not returned', 'text-domain')); } $order->save_meta_data(); // Generate and store details about order VAT $order->update_vat_data(); // Save EU VAT compliance evidence $order->store_vat_evidence(); }