Automattic\WooCommerce\StoreApi\Schemas\V1

ProductCategorySchema::get_category_review_count()protectedWC 1.0

Get total number of reviews for products in a category.

Method of the class: ProductCategorySchema{}

No Hooks.

Return

Int.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_category_review_count( $term );
$term(\WP_Term) (required)
Term object.

ProductCategorySchema::get_category_review_count() code WC 8.7.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;
}