Automattic\WooCommerce\StoreApi\Routes\V1

AbstractTermsRoute::get_terms_response()protectedWC 1.0

Get terms matching passed in args.

Method of the class: AbstractTermsRoute{}

No Hooks.

Return

\WP_REST_Response.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_terms_response( $taxonomy, $request );
$taxonomy(string) (required)
Taxonomy to get terms from.
$request(\WP_REST_Request) (required)
Request object.

AbstractTermsRoute::get_terms_response() code WC 9.3.3

protected function get_terms_response( $taxonomy, $request ) {
	$page          = (int) $request['page'];
	$per_page      = $request['per_page'] ? (int) $request['per_page'] : 0;
	$prepared_args = array(
		'taxonomy'   => $taxonomy,
		'exclude'    => $request['exclude'],
		'include'    => $request['include'],
		'order'      => $request['order'],
		'orderby'    => $request['orderby'],
		'hide_empty' => (bool) $request['hide_empty'],
		'number'     => $per_page,
		'offset'     => $per_page > 0 ? ( $page - 1 ) * $per_page : 0,
		'search'     => $request['search'],
	);

	$term_query = new WP_Term_Query();
	$objects    = $term_query->query( $prepared_args );
	$return     = [];

	foreach ( $objects as $object ) {
		$data     = $this->prepare_item_for_response( $object, $request );
		$return[] = $this->prepare_response_for_collection( $data );
	}

	$response = rest_ensure_response( $return );

	// See if pagination is needed before calculating.
	if ( $per_page > 0 && ( count( $objects ) === $per_page || $page > 1 ) ) {
		$term_count = $this->get_term_count( $taxonomy, $prepared_args );
		$response   = ( new Pagination() )->add_headers( $response, $request, $term_count, ceil( $term_count / $per_page ) );
	}

	return $response;
}