diff --git a/includes/admin/reports/class-wc-report-customers.php b/includes/admin/reports/class-wc-report-customers.php
index 503f470992f..326ad5334f5 100644
--- a/includes/admin/reports/class-wc-report-customers.php
+++ b/includes/admin/reports/class-wc-report-customers.php
@@ -69,46 +69,56 @@ class WC_Report_Customers extends WC_Admin_Report {
* Output customers vs guests chart.
*/
public function customers_vs_guests() {
-
- $customer_order_totals = $this->get_order_report_data(
- array(
- 'data' => array(
- 'ID' => array(
- 'type' => 'post_data',
- 'function' => 'COUNT',
- 'name' => 'total_orders',
- ),
+ $customer_args = array(
+ 'data' => array(
+ 'ID' => array(
+ 'type' => 'post_data',
+ 'function' => 'COUNT',
+ 'name' => 'total_orders',
),
- 'where_meta' => array(
- array(
- 'meta_key' => '_customer_user',
- 'meta_value' => '0',
- 'operator' => '>',
- ),
- ),
- 'filter_range' => true,
- )
+ ),
+ 'filter_range' => true,
);
+ $guest_args = $customer_args;
- $guest_order_totals = $this->get_order_report_data(
- array(
- 'data' => array(
- 'ID' => array(
- 'type' => 'post_data',
- 'function' => 'COUNT',
- 'name' => 'total_orders',
- ),
+ // On WC 3.5.0 the ID of the user that placed the order was moved from the post meta _customer_user to the post_author field in the wp_posts table.
+ if ( version_compare( get_option( 'woocommerce_db_version' ), '3.5.0', '>=' ) ) {
+ $customer_args['where'] = array(
+ array(
+ 'key' => 'post_author',
+ 'value' => '0',
+ 'operator' => '>',
),
- 'where_meta' => array(
- array(
- 'meta_key' => '_customer_user',
- 'meta_value' => '0',
- 'operator' => '=',
- ),
+ );
+
+ $guest_args['where'] = array(
+ array(
+ 'key' => 'post_author',
+ 'value' => '0',
+ 'operator' => '=',
),
- 'filter_range' => true,
- )
- );
+ );
+ } else {
+ $customer_args['where_meta'] = array(
+ array(
+ 'meta_key' => '_customer_user',
+ 'meta_value' => '0',
+ 'operator' => '>',
+ ),
+ );
+
+ $guest_args['where_meta'] = array(
+ array(
+ 'meta_key' => '_customer_user',
+ 'meta_value' => '0',
+ 'operator' => '=',
+ ),
+ );
+ }
+
+ $customer_order_totals = $this->get_order_report_data( $customer_args );
+ $guest_order_totals = $this->get_order_report_data( $guest_args );
+
?>
@@ -246,61 +256,63 @@ class WC_Report_Customers extends WC_Admin_Report {
public function get_main_chart() {
global $wp_locale;
- $customer_orders = $this->get_order_report_data(
- array(
- 'data' => array(
- 'ID' => array(
- 'type' => 'post_data',
- 'function' => 'COUNT',
- 'name' => 'total_orders',
- ),
- 'post_date' => array(
- 'type' => 'post_data',
- 'function' => '',
- 'name' => 'post_date',
- ),
+ $customer_args = array(
+ 'data' => array(
+ 'ID' => array(
+ 'type' => 'post_data',
+ 'function' => 'COUNT',
+ 'name' => 'total_orders',
),
- 'where_meta' => array(
- array(
- 'meta_key' => '_customer_user',
- 'meta_value' => '0',
- 'operator' => '>',
- ),
+ 'post_date' => array(
+ 'type' => 'post_data',
+ 'function' => '',
+ 'name' => 'post_date',
),
- 'group_by' => $this->group_by_query,
- 'order_by' => 'post_date ASC',
- 'query_type' => 'get_results',
- 'filter_range' => true,
- )
+ ),
+ 'group_by' => $this->group_by_query,
+ 'order_by' => 'post_date ASC',
+ 'query_type' => 'get_results',
+ 'filter_range' => true,
);
+ $guest_args = $customer_args;
- $guest_orders = $this->get_order_report_data(
- array(
- 'data' => array(
- 'ID' => array(
- 'type' => 'post_data',
- 'function' => 'COUNT',
- 'name' => 'total_orders',
- ),
- 'post_date' => array(
- 'type' => 'post_data',
- 'function' => '',
- 'name' => 'post_date',
- ),
+ // On WC 3.5.0 the ID of the user that placed the order was moved from the post meta _customer_user to the post_author field in the wp_posts table.
+ if ( version_compare( get_option( 'woocommerce_db_version' ), '3.5.0', '>=' ) ) {
+ $customer_args['where'] = array(
+ array(
+ 'key' => 'post_author',
+ 'value' => '0',
+ 'operator' => '>',
),
- 'where_meta' => array(
- array(
- 'meta_key' => '_customer_user',
- 'meta_value' => '0',
- 'operator' => '=',
- ),
+ );
+
+ $guest_args['where'] = array(
+ array(
+ 'key' => 'post_author',
+ 'value' => '0',
+ 'operator' => '=',
),
- 'group_by' => $this->group_by_query,
- 'order_by' => 'post_date ASC',
- 'query_type' => 'get_results',
- 'filter_range' => true,
- )
- );
+ );
+ } else {
+ $customer_args['where_meta'] = array(
+ array(
+ 'meta_key' => '_customer_user',
+ 'meta_value' => '0',
+ 'operator' => '>',
+ ),
+ );
+
+ $guest_args['where_meta'] = array(
+ array(
+ 'meta_key' => '_customer_user',
+ 'meta_value' => '0',
+ 'operator' => '=',
+ ),
+ );
+ }
+
+ $customer_orders = $this->get_order_report_data( $customer_args );
+ $guest_orders = $this->get_order_report_data( $guest_args );
$signups = $this->prepare_chart_data( $this->customers, 'user_registered', '', $this->chart_interval, $this->start_date, $this->chart_groupby );
$customer_orders = $this->prepare_chart_data( $customer_orders, 'post_date', 'total_orders', $this->chart_interval, $this->start_date, $this->chart_groupby );
diff --git a/includes/wc-user-functions.php b/includes/wc-user-functions.php
index 18b3ac195d8..9b70993ef1e 100644
--- a/includes/wc-user-functions.php
+++ b/includes/wc-user-functions.php
@@ -218,11 +218,15 @@ function wc_customer_bought_product( $customer_email, $user_id, $product_id ) {
$result = get_transient( $transient_name );
if ( false === $result ) {
- $customer_data = array( $user_id );
+ $customer_data = array();
if ( $user_id ) {
$user = get_user_by( 'id', $user_id );
+ if ( version_compare( get_option( 'woocommerce_db_version' ), '3.5.0', '<' ) ) {
+ $customer_data[] = $user_id;
+ }
+
if ( isset( $user->user_email ) ) {
$customer_data[] = $user->user_email;
}
@@ -239,19 +243,31 @@ function wc_customer_bought_product( $customer_email, $user_id, $product_id ) {
return false;
}
- $result = $wpdb->get_col(
- "
- SELECT im.meta_value FROM {$wpdb->posts} AS p
- INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id
- INNER JOIN {$wpdb->prefix}woocommerce_order_items AS i ON p.ID = i.order_id
- INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS im ON i.order_item_id = im.order_item_id
- WHERE p.post_status IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
- AND pm.meta_key IN ( '_billing_email', '_customer_user' )
- AND im.meta_key IN ( '_product_id', '_variation_id' )
- AND im.meta_value != 0
- AND pm.meta_value IN ( '" . implode( "','", $customer_data ) . "' )
- "
- ); // WPCS: unprepared SQL ok.
+ if ( version_compare( get_option( 'woocommerce_db_version' ), '3.5.0', '>=' ) && $user_id ) {
+ // Since WC 3.5 wp_posts.post_author is used to store the ID of the customer who placed an order.
+ $query = "SELECT im.meta_value FROM {$wpdb->posts} AS p
+ INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id
+ INNER JOIN {$wpdb->prefix}woocommerce_order_items AS i ON p.ID = i.order_id
+ INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS im ON i.order_item_id = im.order_item_id
+ WHERE p.post_status IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
+ AND p.post_author = {$user_id}
+ AND pm.meta_key = '_billing_email'
+ AND im.meta_key IN ( '_product_id', '_variation_id' )
+ AND im.meta_value != 0
+ AND pm.meta_value IN ( '" . implode( "','", $customer_data ) . "' )";
+ } else {
+ $query = "SELECT im.meta_value FROM {$wpdb->posts} AS p
+ INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id
+ INNER JOIN {$wpdb->prefix}woocommerce_order_items AS i ON p.ID = i.order_id
+ INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS im ON i.order_item_id = im.order_item_id
+ WHERE p.post_status IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
+ AND pm.meta_key IN ( '_billing_email', '_customer_user' )
+ AND im.meta_key IN ( '_product_id', '_variation_id' )
+ AND im.meta_value != 0
+ AND pm.meta_value IN ( '" . implode( "','", $customer_data ) . "' )";
+ }
+
+ $result = $wpdb->get_col( $query ); // WPCS: unprepared SQL ok.
$result = array_map( 'absint', $result );
set_transient( $transient_name, $result, DAY_IN_SECONDS * 30 );