Automattic\WooCommerce\Admin\API\Reports\Orders\Stats

DataStore::is_returning_customer()public staticWC 1.0

Check to see if an order's customer has made previous orders or not

Method of the class: DataStore{}

No Hooks.

Return

true|false.

Usage

$result = DataStore::is_returning_customer( $order, $customer_id );
$order(array) (required)
WC_Order object.
$customer_id(int|false)
Customer ID. Optional.
Default: null

DataStore::is_returning_customer() code WC 8.6.1

public static function is_returning_customer( $order, $customer_id = null ) {
	if ( is_null( $customer_id ) ) {
		$customer_id = \Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore::get_existing_customer_id_from_order( $order );
	}

	if ( ! $customer_id ) {
		return false;
	}

	$oldest_orders = \Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore::get_oldest_orders( $customer_id );

	if ( empty( $oldest_orders ) ) {
		return false;
	}

	$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() < wc_string_to_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;
	}

	// 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() > 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;
}