WC_REST_Terms_Controller::get_terms_for_product()protectedWC 1.0

Get the terms attached to a product.

This is an alternative to get_terms() uses get_the_terms() which hits the object cache. There are a few things not supported, notably include, exclude. In self::get_items() these are instead treated as a full query.

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

Хуков нет.

Возвращает

Массив. List of term objects. (Total count in $this->total_terms).

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->get_terms_for_product( $prepared_args, $request );
$prepared_args(массив) (обязательный)
Arguments for get_terms().
$request(WP_REST_Request) (обязательный)
Full details about the request.

Код WC_REST_Terms_Controller::get_terms_for_product() WC 9.3.3

protected function get_terms_for_product( $prepared_args, $request ) {
	$taxonomy = $this->get_taxonomy( $request );

	$query_result = get_the_terms( $prepared_args['product'], $taxonomy );
	if ( empty( $query_result ) ) {
		$this->total_terms = 0;
		return array();
	}

	// get_items() verifies that we don't have `include` set, and default.
	// ordering is by `name`.
	if ( ! in_array( $prepared_args['orderby'], array( 'name', 'none', 'include' ), true ) ) {
		switch ( $prepared_args['orderby'] ) {
			case 'id':
				$this->sort_column = 'term_id';
				break;
			case 'slug':
			case 'term_group':
			case 'description':
			case 'count':
				$this->sort_column = $prepared_args['orderby'];
				break;
		}
		usort( $query_result, array( $this, 'compare_terms' ) );
	}
	if ( strtolower( $prepared_args['order'] ) !== 'asc' ) {
		$query_result = array_reverse( $query_result );
	}

	// Pagination.
	$this->total_terms = count( $query_result );
	$query_result      = array_slice( $query_result, $prepared_args['offset'], $prepared_args['number'] );

	return $query_result;
}