Skip to content

Commit

Permalink
Update returning customer values to respect excluded statuses (woocom…
Browse files Browse the repository at this point in the history
…merce#1557)

* Excluded statuses from customers oldest orders

* Fix tests for returning customer where excluded statuses are used

* Remove save_post hook used for testing

* Extract conditions for first order swapping
  • Loading branch information
joshuatf authored Feb 15, 2019
1 parent 9560bb5 commit adc575a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -532,11 +532,17 @@ public static function get_customer_id_by_user_id( $user_id ) {
*/
public static function get_oldest_orders( $customer_id ) {
global $wpdb;
$orders_table = $wpdb->prefix . 'wc_order_stats';
$orders_table = $wpdb->prefix . 'wc_order_stats';
$excluded_statuses = array_map( array( __CLASS__, 'normalize_order_status' ), self::get_excluded_report_order_statuses() );
$excluded_statuses_condition = '';
if ( ! empty( $excluded_statuses ) ) {
$excluded_statuses_str = implode( "','", $excluded_statuses );
$excluded_statuses_condition = "AND status NOT IN ('{$excluded_statuses_str}')";
}

return $wpdb->get_results(
$wpdb->prepare(
"SELECT order_id, date_created FROM {$orders_table} WHERE customer_id = %d ORDER BY date_created, order_id ASC LIMIT 2",
"SELECT order_id, date_created FROM {$orders_table} WHERE customer_id = %d {$excluded_statuses_condition} ORDER BY date_created, order_id ASC LIMIT 2",
$customer_id
)
); // WPCS: unprepared SQL ok.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,21 +527,28 @@ protected static function is_returning_customer( $order ) {
return false;
}

$first_order = $oldest_orders[0];
$first_order = $oldest_orders[0];
$second_order = isset( $oldest_orders[1] ) ? $oldest_orders[1] : false;
$excluded_statuses = self::get_excluded_report_order_statuses();

// Order is older than previous first order.
if ( $order->get_date_created() < new WC_DateTime( $first_order->date_created ) ) {
if ( $order->get_date_created() < new WC_DateTime( $first_order->date_created ) &&
! in_array( $order->get_status(), $excluded_statuses, true )
) {
self::set_customer_first_order( $customer_id, $order->get_id() );
return false;
}
// First order date has changed and next oldest is now the first order.
$second_order = isset( $oldest_orders[1] ) ? $oldest_orders[1] : false;
if (
(int) $order->get_id() === (int) $first_order->order_id &&

// The current order is the oldest known order.
$is_first_order = (int) $order->get_id() === (int) $first_order->order_id;
// Order date has changed and next oldest is now the first order.
$date_change = $second_order &&
$order->get_date_created() > new WC_DateTime( $first_order->date_created ) &&
$second_order &&
new WC_DateTime( $second_order->date_created ) < $order->get_date_created()
) {
new WC_DateTime( $second_order->date_created ) < $order->get_date_created();
// Status has changed to an excluded status and next oldest order is now the first order.
$status_change = $second_order &&
in_array( $order->get_status(), $excluded_statuses, true );
if ( $is_first_order && ( $date_change || $status_change ) ) {
self::set_customer_first_order( $customer_id, $second_order->order_id );
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/reports/class-wc-tests-reports-orders-stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ public function test_populate_and_query_statuses() {
'taxes' => 0,
'shipping' => 0,
'net_revenue' => 100,
'num_returning_customers' => 1,
'num_new_customers' => 0,
'num_returning_customers' => 0,
'num_new_customers' => 1,
'products' => 1,
'segments' => array(),
),
Expand All @@ -228,8 +228,8 @@ public function test_populate_and_query_statuses() {
'num_items_sold' => 4,
'avg_items_per_order' => 4,
'avg_order_value' => 100,
'num_returning_customers' => 1,
'num_new_customers' => 0,
'num_returning_customers' => 0,
'num_new_customers' => 1,
'segments' => array(),
),
),
Expand Down

0 comments on commit adc575a

Please sign in to comment.