Automattic\WooCommerce\Admin\API\Reports\Customers

DataStore::get_existing_customer_id_from_order()public staticWC 1.0

Returns an existing customer ID for an order if one exists.

Method of the class: DataStore{}

No Hooks.

Return

Int|true|false.

Usage

$result = DataStore::get_existing_customer_id_from_order( $order );
$order(object) (required)
WC Order.

DataStore::get_existing_customer_id_from_order() code WC 9.3.3

public static function get_existing_customer_id_from_order( $order ) {
	global $wpdb;

	if ( ! is_a( $order, 'WC_Order' ) ) {
		return false;
	}

	$user_id = $order->get_customer_id();

	if ( 0 === $user_id ) {
		$customer_id = $wpdb->get_var(
			$wpdb->prepare(
				"SELECT customer_id FROM {$wpdb->prefix}wc_order_stats WHERE order_id = %d",
				$order->get_id()
			)
		);

		if ( $customer_id ) {
			return $customer_id;
		}

		$email = $order->get_billing_email( 'edit' );

		if ( $email ) {
			return self::get_guest_id_by_email( $email );
		} else {
			return false;
		}
	} else {
		return self::get_customer_id_by_user_id( $user_id );
	}
}