WC_API_Customers::get_customer()publicWC 2.1

Get the customer for the given ID

Method of the class: WC_API_Customers{}

Hooks from the method

Return

Array|WP_Error.

Usage

$WC_API_Customers = new WC_API_Customers();
$WC_API_Customers->get_customer( $id, $fields );
$id(int) (required)
the customer ID
$fields(array)
-
Default: null

Changelog

Since 2.1 Introduced.

WC_API_Customers::get_customer() code WC 8.7.0

public function get_customer( $id, $fields = null ) {
	global $wpdb;

	$id = $this->validate_request( $id, 'customer', 'read' );

	if ( is_wp_error( $id ) ) {
		return $id;
	}

	$customer      = new WC_Customer( $id );
	$last_order    = $customer->get_last_order();
	$customer_data = array(
		'id'               => $customer->get_id(),
		'created_at'       => $this->server->format_datetime( $customer->get_date_created() ? $customer->get_date_created()->getTimestamp() : 0 ), // API gives UTC times.
		'last_update'      => $this->server->format_datetime( $customer->get_date_modified() ? $customer->get_date_modified()->getTimestamp() : 0 ), // API gives UTC times.
		'email'            => $customer->get_email(),
		'first_name'       => $customer->get_first_name(),
		'last_name'        => $customer->get_last_name(),
		'username'         => $customer->get_username(),
		'role'             => $customer->get_role(),
		'last_order_id'    => is_object( $last_order ) ? $last_order->get_id() : null,
		'last_order_date'  => is_object( $last_order ) ? $this->server->format_datetime( $last_order->get_date_created() ? $last_order->get_date_created()->getTimestamp() : 0 ) : null, // API gives UTC times.
		'orders_count'     => $customer->get_order_count(),
		'total_spent'      => wc_format_decimal( $customer->get_total_spent(), 2 ),
		'avatar_url'       => $customer->get_avatar_url(),
		'billing_address'  => array(
			'first_name' => $customer->get_billing_first_name(),
			'last_name'  => $customer->get_billing_last_name(),
			'company'    => $customer->get_billing_company(),
			'address_1'  => $customer->get_billing_address_1(),
			'address_2'  => $customer->get_billing_address_2(),
			'city'       => $customer->get_billing_city(),
			'state'      => $customer->get_billing_state(),
			'postcode'   => $customer->get_billing_postcode(),
			'country'    => $customer->get_billing_country(),
			'email'      => $customer->get_billing_email(),
			'phone'      => $customer->get_billing_phone(),
		),
		'shipping_address' => array(
			'first_name' => $customer->get_shipping_first_name(),
			'last_name'  => $customer->get_shipping_last_name(),
			'company'    => $customer->get_shipping_company(),
			'address_1'  => $customer->get_shipping_address_1(),
			'address_2'  => $customer->get_shipping_address_2(),
			'city'       => $customer->get_shipping_city(),
			'state'      => $customer->get_shipping_state(),
			'postcode'   => $customer->get_shipping_postcode(),
			'country'    => $customer->get_shipping_country(),
		),
	);

	return array( 'customer' => apply_filters( 'woocommerce_api_customer_response', $customer_data, $customer, $fields, $this->server ) );
}