Update wc_customer_bought_product() and customers report to use post_author instead of _customer_user

This commit is contained in:
Rodrigo Primo 2018-05-07 17:15:07 -03:00
parent 14287bdcf3
commit b97f2749d6
2 changed files with 126 additions and 98 deletions

View File

@ -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 );
?>
<div class="chart-container">
<div class="chart-placeholder customers_vs_guests pie-chart" style="height:200px"></div>
@ -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 );

View File

@ -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 );