From f109008e0d1c921f1e7bb85429317cd1708d54a6 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Mon, 29 Oct 2018 11:15:22 -0300 Subject: [PATCH] Revert "Update wc_customer_bought_product() and customers report to use post_author instead of _customer_user" This reverts commit b97f2749d6851f7abbca737eb4a01400826670f7. --- .../reports/class-wc-report-customers.php | 180 ++++++++---------- includes/wc-user-functions.php | 44 ++--- 2 files changed, 98 insertions(+), 126 deletions(-) diff --git a/includes/admin/reports/class-wc-report-customers.php b/includes/admin/reports/class-wc-report-customers.php index 326ad5334f5..503f470992f 100644 --- a/includes/admin/reports/class-wc-report-customers.php +++ b/includes/admin/reports/class-wc-report-customers.php @@ -69,56 +69,46 @@ class WC_Report_Customers extends WC_Admin_Report { * Output customers vs guests chart. */ public function customers_vs_guests() { - $customer_args = array( - 'data' => array( - 'ID' => array( - 'type' => 'post_data', - 'function' => 'COUNT', - 'name' => 'total_orders', + + $customer_order_totals = $this->get_order_report_data( + array( + 'data' => array( + 'ID' => array( + 'type' => 'post_data', + 'function' => 'COUNT', + 'name' => 'total_orders', + ), ), - ), - 'filter_range' => true, + 'where_meta' => array( + array( + 'meta_key' => '_customer_user', + 'meta_value' => '0', + 'operator' => '>', + ), + ), + 'filter_range' => true, + ) ); - $guest_args = $customer_args; - // 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' => '>', + $guest_order_totals = $this->get_order_report_data( + array( + 'data' => array( + 'ID' => array( + 'type' => 'post_data', + 'function' => 'COUNT', + 'name' => 'total_orders', + ), ), - ); - - $guest_args['where'] = array( - array( - 'key' => 'post_author', - 'value' => '0', - 'operator' => '=', + 'where_meta' => array( + array( + 'meta_key' => '_customer_user', + 'meta_value' => '0', + 'operator' => '=', + ), ), - ); - } 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 ); - + 'filter_range' => true, + ) + ); ?>
@@ -256,63 +246,61 @@ class WC_Report_Customers extends WC_Admin_Report { public function get_main_chart() { global $wp_locale; - $customer_args = array( - 'data' => array( - 'ID' => array( - 'type' => 'post_data', - 'function' => 'COUNT', - 'name' => 'total_orders', + $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', + ), ), - 'post_date' => array( - 'type' => 'post_data', - 'function' => '', - 'name' => 'post_date', + 'where_meta' => array( + array( + 'meta_key' => '_customer_user', + 'meta_value' => '0', + 'operator' => '>', + ), ), - ), - '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; - // 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' => '>', + $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', + ), ), - ); - - $guest_args['where'] = array( - array( - 'key' => 'post_author', - 'value' => '0', - 'operator' => '=', + 'where_meta' => array( + array( + 'meta_key' => '_customer_user', + 'meta_value' => '0', + 'operator' => '=', + ), ), - ); - } 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 ); + 'group_by' => $this->group_by_query, + 'order_by' => 'post_date ASC', + 'query_type' => 'get_results', + 'filter_range' => true, + ) + ); $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 b8690b75b2d..06769ce2ffc 100644 --- a/includes/wc-user-functions.php +++ b/includes/wc-user-functions.php @@ -219,15 +219,11 @@ function wc_customer_bought_product( $customer_email, $user_id, $product_id ) { $result = get_transient( $transient_name ); if ( false === $result ) { - $customer_data = array(); + $customer_data = array( $user_id ); 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; } @@ -244,31 +240,19 @@ function wc_customer_bought_product( $customer_email, $user_id, $product_id ) { return false; } - 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 = $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. $result = array_map( 'absint', $result ); set_transient( $transient_name, $result, DAY_IN_SECONDS * 30 );