WC_REST_Terms_Controller::get_terms_for_product() protected WC 1.0
Get the terms attached to a product.
This is an alternative to get_terms() that uses get_the_terms() instead, 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.
{} It's a method of the class: WC_REST_Terms_Controller{}
No Hooks.
Return
Array. List of term objects. (Total count in $this->total_terms).
Usage
// protected - for code of main (parent) or child class $result = $this->get_terms_for_product( $prepared_args, $request );
- $prepared_args(array) (required)
- Arguments for get_terms().
- $request(WP_REST_Request) (required)
- Full details about the request.
Code of WC_REST_Terms_Controller::get_terms_for_product() WC REST Terms Controller::get terms for product WC 5.0.0
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;
}