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:
parent
5d3ce289f9
commit
cba9ab61b2
|
@ -532,11 +532,17 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
|
|||
*/
|
||||
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.
|
||||
|
|
|
@ -527,21 +527,28 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto
|
|||
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;
|
||||
}
|
||||
|
|
|
@ -205,8 +205,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
|||
'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(),
|
||||
),
|
||||
|
@ -228,8 +228,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
|
|||
'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(),
|
||||
),
|
||||
),
|
||||
|
|
Loading…
Reference in New Issue