Automattic\WooCommerce\StoreApi\Schemas\V1
ProductCategorySchema::get_category_review_count
Get total number of reviews for products in a category.
Метод класса: ProductCategorySchema{}
Хуков нет.
Возвращает
int.
Использование
// protected - в коде основоного (родительского) или дочернего класса $result = $this->get_category_review_count( $term );
- $term(WP_Term) (обязательный)
- Term object.
Код ProductCategorySchema::get_category_review_count() ProductCategorySchema::get category review count WC 10.5.0
protected function get_category_review_count( $term ) {
global $wpdb;
$children = get_term_children( $term->term_id, 'product_cat' );
if ( ! $children || is_wp_error( $children ) ) {
$terms_to_count_str = absint( $term->term_id );
} else {
$terms_to_count = array_unique( array_map( 'absint', array_merge( array( $term->term_id ), $children ) ) );
$terms_to_count_str = implode( ',', $terms_to_count );
}
$products_of_category_sql = "
SELECT SUM(comment_count) as review_count
FROM {$wpdb->posts} AS posts
INNER JOIN {$wpdb->term_relationships} AS term_relationships ON posts.ID = term_relationships.object_id
WHERE term_relationships.term_taxonomy_id IN (" . esc_sql( $terms_to_count_str ) . ')
';
$review_count = $wpdb->get_var( $products_of_category_sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
return (int) $review_count;
}