ldest 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() > wc_string_to_datetime( $first_order->date_created ) && wc_string_to_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; } return (int) $order->get_id() !== (int) $first_order->order_id; } /** * Set a customer's first order and all others to returning. * * @param int $customer_id Customer ID. * @param int $order_id Order ID. */ protected static function set_customer_first_order( $customer_id, $order_id ) { global $wpdb; $orders_stats_table = self::get_db_table_name(); $wpdb->query( $wpdb->prepare( // phpcs:ignore Generic.Commenting.Todo.TaskFound // TODO: use the %i placeholder to prepare the table name when available in the the minimum required WordPress version. // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared "UPDATE {$orders_stats_table} SET returning_customer = CASE WHEN order_id = %d THEN false ELSE true END WHERE customer_id = %d", $order_id, $customer_id ) ); } /** * Initialize query objects. */ protected function initialize_queries() { $this->clear_all_clauses(); unset( $this->subquery ); $this->total_query = new SqlQuery( $this->context . '_total' ); $this->total_query->add_sql_clause( 'from', self::get_db_table_name() ); $this->interval_query = new SqlQuery( $this->context . '_interval' ); $this->interval_query->add_sql_clause( 'from', self::get_db_table_name() ); $this->interval_query->add_sql_clause( 'group_by', 'time_interval' ); } }