Automattic\WooCommerce\Internal\RestApi\Routes\V4\Customers

CustomerSchema::get_item_responsepublicWC 1.0

Get the item response.

Method of the class: CustomerSchema{}

No Hooks.

Returns

Array. The item response.

Usage

$CustomerSchema = new CustomerSchema();
$CustomerSchema->get_item_response( $item, $request, $include_fields ): array;
$item(mixed) (required)
WordPress representation of the item.
$request(WP_REST_Request) (required)
Request object.
$include_fields(array)
Fields to include in the response.
Default: array()

CustomerSchema::get_item_response() code WC 10.4.3

public function get_item_response( $item, WP_REST_Request $request, array $include_fields = array() ): array {
	if ( ! $item instanceof WC_Customer ) {
		return array();
	}

	$data = $item->get_data();

	// Normalize last active timestamp - treat empty string, '0', 0, or false as null.
	$last_active = $item->get_meta( 'wc_last_active' );
	$last_active = empty( $last_active ) ? null : $last_active;

	$formatted_data = array(
		'id'                 => $item->get_id(),
		'date_created'       => wc_rest_prepare_date_response( $item->get_date_created(), false ),
		'date_created_gmt'   => wc_rest_prepare_date_response( $item->get_date_created() ),
		'date_modified'      => wc_rest_prepare_date_response( $item->get_date_modified(), false ),
		'date_modified_gmt'  => wc_rest_prepare_date_response( $item->get_date_modified() ),
		'email'              => $data['email'],
		'first_name'         => $data['first_name'],
		'last_name'          => $data['last_name'],
		'role'               => $data['role'],
		'username'           => $data['username'],
		'billing'            => $data['billing'],
		'shipping'           => $data['shipping'],
		'is_paying_customer' => $data['is_paying_customer'],
		'orders_count'       => $item->get_order_count(),
		'total_spent'        => $item->get_total_spent(),
		'avatar_url'         => $item->get_avatar_url(),
		'last_active'        => $last_active ? wc_rest_prepare_date_response( $last_active, false ) : null,
		'last_active_gmt'    => $last_active ? wc_rest_prepare_date_response( $last_active ) : null,
	);

	// Filter fields if specified.
	if ( ! empty( $include_fields ) ) {
		$formatted_data = array_intersect_key( $formatted_data, array_flip( $include_fields ) );
	}

	return $formatted_data;
}