Advertisement
businessdad

EU VAT Assistant - Collect VAT data for past orders

Jan 9th, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.36 KB | None | 0 0
  1. /**
  2.  * Triggers EU VAT Assistant's collection of VAT data for past orders, in a specific range of order IDs.
  3.  *
  4.  * HOW TO USE THIS CODE
  5.  * 1. Add the code to the bottom of your theme's functions.php file (see https://www.skyverge.com/blog/add-custom-code-to-wordpress/).
  6.  * 2. Open the EU VAT Assistant settings page, by going to WooCommerce > EU VAT Assistant.
  7.  * 3. Change the URL of the page, by adding the following arguments to it:
  8.  *  - process_vat_past_orders=1
  9.  *  - from_order_id = <ID OF THE FIRST ORDER TO PROCESS>
  10.  *  - to_order_id = <ID OF THE LAST ORDER TO PROCESS>
  11.  * Example: https://example.org/wp-admin/admin.php?page=wc_aelia_eu_vat_assistant&process_vat_past_orders=1&from_order_id=123&to_order_id=567
  12.  * 4. Load the new URL. The EU VAT Assistant will process past orders, and print out the list of the order IDS
  13.  *    it processed. The output will be "raw" (i.e. not very aesthetically pleasing, as its purpose is just
  14.  *    debugging).
  15.  *
  16.  * GPL DISCLAIMER
  17.  * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
  18.  * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
  19.  * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
  20.  * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
  21.  * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
  22.  *
  23.  * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
  24.  */
  25. add_action('admin_init', function() {
  26.     if(!@$_GET['process_vat_past_orders']) {
  27.         return;
  28.     }
  29.  
  30.     if(!@$_GET['from_order_id']) {
  31.       return;
  32.     }
  33.  
  34.     if(!@$_GET['to_order_id']) {
  35.       return;
  36.     }
  37.  
  38.     // Get the minimum and maximum order ID to process
  39.     $min_order_id = $_GET['from_order_id']);
  40.     $max_order_id = $_GET['to_order_id'];
  41.  
  42.     global $wpdb;
  43.     $SQL = "
  44.         SELECT
  45.             P.ID
  46.         FROM {$wpdb->posts} P
  47.         WHERE
  48.             (P.post_type = 'shop_order') AND
  49.             (P.ID BETWEEN {$min_order_id} AND {$max_order_id})
  50.         ORDER BY
  51.             P.ID
  52.     ";
  53.     // Get the list of order IDs to process
  54.     $orders = $wpdb->get_results($SQL);
  55.  
  56.     // Extract the IDs from the list of objects returned
  57.     // by the WPDB query
  58.     $orders = wp_list_pluck($orders, 'ID');
  59.  
  60.     foreach($orders as $order_id) {
  61.         // Display the order being processed. That can be used in case the
  62.         // processing times out, to resume the processing
  63.         echo "PROCESSING ORDER ID: {$order_id}<br>";
  64.  
  65.         // Load the order
  66.         $order = new \Aelia\WC\EU_VAT_Assistant\Order($order_id);
  67.         if(!is_object($order)) {
  68.             continue;
  69.         }
  70.  
  71.         // Update the order meta, as the EU VAT Assistant would do
  72.         $order->update_meta_data('vat_number', '');
  73.         $order->update_meta_data('_vat_country', $order->get_billing_country());
  74.         $order->update_meta_data('_vat_number_validated', 'no-number');
  75.         $order->update_meta_data('vies_response', array());
  76.         $order->update_meta_data('vies_consultation_number', 'Not returned');
  77.         $order->update_meta_data('_customer_location_self_certified', 'no');
  78.         $order->save();
  79.  
  80.         // Generate and store details about order VAT
  81.         $order->update_vat_data();
  82.         // Save EU VAT compliance evidence
  83.         $order->store_vat_evidence();
  84.     }
  85. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement