businessdad icon

EU VAT Assistant - Collect VAT data for past orders

businessdad | PRO | 01/09/19 08:27:24 PM UTC | 0 ⭐ | 984 👁️ | Never ⏰ | []
PHP |

3.36 KB

|

None

|

0 👍

/

0 👎

/**
 * Triggers EU VAT Assistant's collection of VAT data for past orders, in a specific range of order IDs.
 *
 * HOW TO USE THIS CODE
 * 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/).
 * 2. Open the EU VAT Assistant settings page, by going to WooCommerce > EU VAT Assistant.
 * 3. Change the URL of the page, by adding the following arguments to it:
 *  - process_vat_past_orders=1
 *  - from_order_id = <ID OF THE FIRST ORDER TO PROCESS>
 *  - to_order_id = <ID OF THE LAST ORDER TO PROCESS>
 * 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
 * 4. Load the new URL. The EU VAT Assistant will process past orders, and print out the list of the order IDS
 *    it processed. The output will be "raw" (i.e. not very aesthetically pleasing, as its purpose is just 
 *    debugging).
 *
 * 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
 */
add_action('admin_init', function() {
    if(!@$_GET['process_vat_past_orders']) {
        return;
    }
 
    if(!@$_GET['from_order_id']) {
      return;
    }
 
    if(!@$_GET['to_order_id']) {
      return;
    }
 
    // Get the minimum and maximum order ID to process
    $min_order_id = $_GET['from_order_id']);
    $max_order_id = $_GET['to_order_id'];
 
    global $wpdb;
    $SQL = "
        SELECT
            P.ID
        FROM {$wpdb->posts} P
        WHERE
            (P.post_type = 'shop_order') AND
            (P.ID BETWEEN {$min_order_id} AND {$max_order_id})
        ORDER BY
            P.ID
    ";
    // Get the list of order IDs to process
    $orders = $wpdb->get_results($SQL);
 
    // Extract the IDs from the list of objects returned
    // by the WPDB query
    $orders = wp_list_pluck($orders, 'ID');
 
    foreach($orders as $order_id) {
        // Display the order being processed. That can be used in case the
        // processing times out, to resume the processing
        echo "PROCESSING ORDER ID: {$order_id}<br>";
 
        // Load the order
        $order = new \Aelia\WC\EU_VAT_Assistant\Order($order_id);
        if(!is_object($order)) {
            continue;
        }
 
        // Update the order meta, as the EU VAT Assistant would do
        $order->update_meta_data('vat_number', '');
        $order->update_meta_data('_vat_country', $order->get_billing_country());
        $order->update_meta_data('_vat_number_validated', 'no-number');
        $order->update_meta_data('vies_response', array());
        $order->update_meta_data('vies_consultation_number', 'Not returned');
        $order->update_meta_data('_customer_location_self_certified', 'no');
        $order->save();
 
        // Generate and store details about order VAT
        $order->update_vat_data();
        // Save EU VAT compliance evidence
        $order->store_vat_evidence();
    }
});

Comments