Advertisement
businessdad

EU VAT Assistant - Update VAT on past orders

Oct 5th, 2018
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.86 KB | None | 0 0
  1. /**
  2.  * Aelia EU VAT Assistant for WooCommerce - Update VAT data for existing orders.
  3.  * This code loops through existing orders and calls the EU VAT Assistant functions
  4.  * that update the VAT data stored against each one.
  5.  *
  6.  * HOW TO USE THIS CODE
  7.  * Add the code to the bottom of your theme's functions.php file (see https://www.skyverge.com/blog/add-custom-code-to-wordpress/).
  8.  *
  9.  * The script accepts the following URL arguments:
  10.  * - update_order_vat_data=1. This argument triggers the processing of orders.
  11.  * - start_date=YYMMDD. The script will take orders created on or after this date.
  12.  * - end_date=YYMMDD. The script will take orders created BEFORE this date.
  13.  * - debug=1. If specified, the script will print out debug information.
  14.  *
  15.  * GPL DISCLAIMER
  16.  * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
  17.  * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
  18.  * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
  19.  * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
  20.  * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
  21.  *
  22.  * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
  23.  *
  24.  * @author Aelia
  25.  */
  26. add_action('admin_init', function() {
  27.     if(!is_admin() || empty($_GET['update_order_vat_data']) || !current_user_can('manage_woocommerce')) {
  28.         return;
  29.     }
  30.  
  31.     global $wpdb;
  32.  
  33.     // Start date
  34.     if(isset($_GET['start_date'])) {
  35.         $start_date = $_GET['start_date'];
  36.     }
  37.     else {
  38.         // Take the 1st of January of current year as the default start date
  39.         $start_date = date('Y0101');
  40.     }
  41.  
  42.     // End date
  43.     if(isset($_GET['end_date'])) {
  44.         $end_date = $_GET['end_date'];
  45.     }
  46.     else {
  47.         // Take tomorrow as the default end date
  48.         $default_end_date = new DateTime('tomorrow');
  49.         $end_date = $default_end_date->format('Ymd');
  50.     }
  51.  
  52.     $px = $wpdb->prefix;
  53.     $SQL = "
  54.         SELECT
  55.             ORDERS.ID AS order_id
  56.         FROM
  57.             {$px}posts AS ORDERS
  58.         WHERE
  59.             (ORDERS.post_type IN ('shop_order')) AND
  60.             (ORDERS.post_status IN ('wc-processing', 'wc-completed')) AND
  61.             (ORDERS.post_date >= '$start_date') AND
  62.             (ORDERS.post_date < '$end_date')
  63.     ";
  64.  
  65.     // Debug
  66.     if(isset($_GET['debug'])) {
  67.         var_dump($SQL);
  68.     }
  69.     // Fetch the orders to process
  70.     $dataset = $wpdb->get_results($SQL);
  71.  
  72.     if(isset($_GET['debug'])) {
  73.         var_dump($SQL, $dataset);
  74.     }
  75.  
  76.     foreach($dataset as $entry) {
  77.         $order = new \Aelia\WC\EU_VAT_Assistant\Order($entry->order_id);
  78.         // Generate and store details about order VAT
  79.         $order->update_vat_data();
  80.         // Save EU VAT compliance evidence
  81.         //$order->store_vat_evidence();
  82.     }
  83. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement