Revert "Update wc_customer_bought_product() and customers report to use post_author instead of _customer_user"
This reverts commit b97f2749d6
.
This commit is contained in:
parent
86650d9fb8
commit
f109008e0d
|
@ -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,
|
||||
)
|
||||
);
|
||||
?>
|
||||
<div class="chart-container">
|
||||
<div class="chart-placeholder customers_vs_guests pie-chart" style="height:200px"></div>
|
||||
|
@ -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 );
|
||||
|
|
|
@ -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 );
|
||||
|
|
Loading…
Reference in New Issue