FOX - Currency Switcher for WooCommerce

How to Adapt WooCommerce Reports for a Multi Currency Shop

A multi currency shop breaks WooCommerce reports quietly: totals in different currencies get added together as if they were the same number.

WooCommerce Currency Switcher can't by default work with WooCommerce reports as there is no hooks there, and it's not possible to plug-in there. But after some investigations of woocommerce code I found 1 way how to adapt reports for orders with different currencies (works only for Reports->Orders->”Sales by date”):
  • open file wp-content\plugins\woocommerce\includes\admin\reports\class-wc-admin-report.php
  • find there public function get_order_report_data
  • on the same end of the function before code return $result; drop code:
    $result = apply_filters('woocs_reports_get_order_report_query', $result, $args);
  • open functions.php file of your current wp theme and on the same bottom of the file drop the next code:
    add_filter('woocs_reports_get_order_report_query', function($result, $args) {
    
        if (is_array($result) AND isset($result[0]->total_sales))
        {
            global $wpdb;
            global $WOOCS;
            $range = '7day';
            if (isset($_GET['range']))
            {
                $range = $_GET['range'];
            }
            $start_date = '';
            $end_date = date('Y-m-d 23:59:59');
            switch ($range)
            {
                case 'custom':
                    $start_date = $_GET['start_date'];
                    break;
                case '7day':
                    $start_date = date('Y-m-d 00:00:00', time() - 7 * DAY_IN_SECONDS);
                    break;
                case 'month':
                    $start_date = date('Y-m-01 00:00:00');
                    break;
                case 'year':
                    $start_date = date('Y-01-01 00:00:00');
                    break;
    
                default:
                    $start_date = date('Y-m-d 00:00:00', time() - 7 * DAY_IN_SECONDS);
                    break;
            }
            $order_status = "wc-" . implode('","wc-', $args['order_status']);
            //***
            $sql = $wpdb->prepare('SELECT ID FROM ' . $wpdb->posts . ' WHERE post_type=%s AND post_status IN("' . $order_status . '") AND post_date >= %s AND post_date < %s', 'shop_order', $start_date, $end_date);
            $tmp = $wpdb->get_results($sql, ARRAY_N);        
            $orders = array();
            $result = array();
            if (!empty($tmp))
            {
                foreach ($tmp as $v)
                {
                    $orders[] = $v[0];
                }
    
                $result = array();
                $currencies = $WOOCS->get_currencies();
                foreach ($orders as $order_id)
                {
                    $tmp = array();
                    $order = new WC_Order($order_id);
    
                    $_order_currency = get_post_meta($order_id, '_order_currency', true);                
                    $order_rate = get_post_meta($order_id, '_woocs_order_rate', true);                
                    if (!$order_rate)
                    {
                        if (isset($currencies[$_order_currency]))
                        {
                            $order_rate = $currencies[$_order_currency]['rate'];
                        } else
                        {
                            continue;
                        }
                    }                
                    
                    if ($_order_currency != $WOOCS->default_currency)
                    {
                        $tmp['total_sales'] = $WOOCS->back_convert(get_post_meta($order_id, '_order_total', true), $order_rate, 4);
                        $tmp['total_shipping'] = $WOOCS->back_convert(get_post_meta($order_id, '_order_shipping', true), $order_rate, 4);
                        $tmp['total_tax'] = $WOOCS->back_convert(get_post_meta($order_id, '_order_tax', true), $order_rate, 4);
                        $tmp['total_shipping_tax'] = $WOOCS->back_convert(get_post_meta($order_id, '_order_shipping_tax', true), $order_rate, 4);
                    } else
                    {
                        $tmp['total_sales'] = get_post_meta($order_id, '_order_total', true);
                        $tmp['total_shipping'] = get_post_meta($order_id, '_order_shipping', true);
                        $tmp['total_tax'] = get_post_meta($order_id, '_order_tax', true);
                        $tmp['total_shipping_tax'] = get_post_meta($order_id, '_order_shipping_tax', true);
                    }
                    
                    $tmp['post_date'] = $order->order_date;
                    $result[] = (object) $tmp;
                }
            }
        }
    
        return $result;
    }, 10, 2);