Update returning customer values to respect excluded statuses (https://github.com/woocommerce/woocommerce-admin/pull/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
This commit is contained in:
Joshua T Flowers 2019-02-15 10:09:16 +08:00 committed by GitHub
parent 5d3ce289f9
commit cba9ab61b2
3 changed files with 28 additions and 15 deletions

View File

@ -533,10 +533,16 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
public static function get_oldest_orders( $customer_id ) { public static function get_oldest_orders( $customer_id ) {
global $wpdb; 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( return $wpdb->get_results(
$wpdb->prepare( $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 $customer_id
) )
); // WPCS: unprepared SQL ok. ); // WPCS: unprepared SQL ok.

View File

@ -528,20 +528,27 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto
} }
$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. // 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() ); self::set_customer_first_order( $customer_id, $order->get_id() );
return false; 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; // The current order is the oldest known order.
if ( $is_first_order = (int) $order->get_id() === (int) $first_order->order_id;
(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 ) && $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 ); self::set_customer_first_order( $customer_id, $second_order->order_id );
return true; return true;
} }

View File

@ -205,8 +205,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
'taxes' => 0, 'taxes' => 0,
'shipping' => 0, 'shipping' => 0,
'net_revenue' => 100, 'net_revenue' => 100,
'num_returning_customers' => 1, 'num_returning_customers' => 0,
'num_new_customers' => 0, 'num_new_customers' => 1,
'products' => 1, 'products' => 1,
'segments' => array(), 'segments' => array(),
), ),
@ -228,8 +228,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
'num_items_sold' => 4, 'num_items_sold' => 4,
'avg_items_per_order' => 4, 'avg_items_per_order' => 4,
'avg_order_value' => 100, 'avg_order_value' => 100,
'num_returning_customers' => 1, 'num_returning_customers' => 0,
'num_new_customers' => 0, 'num_new_customers' => 1,
'segments' => array(), 'segments' => array(),
), ),
), ),