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

CustomerSchema::get_item_response │ public │ WC 1.0

Get the item response.

Метод класса: CustomerSchema{}

Хуков нет.

Возвращает

array. The item response.

Использование

$CustomerSchema = new CustomerSchema();
$CustomerSchema->get_item_response( $item, $request, $include_fields ): array;
$item(разное) (обязательный)
WordPress representation of the item.
$request(WP_REST_Request) (обязательный)
Request object.
$include_fields(массив)
Fields to include in the response.
По умолчанию: array()

Код CustomerSchema::get_item_response() WC 11.2.2

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

	// Only compute fields that will actually be returned. When $include_fields is empty the full
	// response is requested, so everything is computed (default behavior, byte-identical output).
	// When a sparse _fields subset is requested, skip the per-customer aggregates and the avatar
	// lookup the caller did not ask for, instead of computing them and discarding them after
	// array_intersect_key.
	$is_field_included = static function ( string $field ) use ( $include_fields ) {
		return empty( $include_fields ) || in_array( $field, $include_fields, true );
	};

	// 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'       => $is_field_included( 'orders_count' ) ? $item->get_order_count() : null,
		'total_spent'        => $is_field_included( 'total_spent' ) ? $item->get_total_spent() : null,
		'avatar_url'         => $is_field_included( 'avatar_url' ) ? $item->get_avatar_url() : null,
		'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;
}