WC_Comments::get_rating_counts_for_product
Get product rating count for a product. Please note this is not cached.
Method of the class: WC_Comments{}
No Hooks.
Returns
Int[].
Usage
$result = WC_Comments::get_rating_counts_for_product( $product );
- $product(WC_Product) (required) (passed by reference — &)
- Product instance.
Changelog
| Since 3.0.0 | Introduced. |
WC_Comments::get_rating_counts_for_product() WC Comments::get rating counts for product code WC 10.4.3
public static function get_rating_counts_for_product( &$product ) {
global $wpdb;
$counts = array();
$raw_counts = $wpdb->get_results(
$wpdb->prepare(
"
SELECT meta_value, COUNT( * ) as meta_value_count FROM $wpdb->commentmeta
LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
WHERE meta_key = 'rating'
AND comment_post_ID = %d
AND comment_approved = '1'
AND meta_value > 0
GROUP BY meta_value
",
$product->get_id()
)
);
foreach ( $raw_counts as $count ) {
$counts[ $count->meta_value ] = absint( $count->meta_value_count ); // WPCS: slow query ok.
}
return $counts;
}