Merge pull request #6284 from ragulka/rating-count-value

Allow getting rating count for a specific rating value
This commit is contained in:
Mike Jolley 2014-09-17 12:05:45 +01:00
commit 1b68406202
2 changed files with 19 additions and 7 deletions

View File

@ -975,24 +975,30 @@ class WC_Product {
/**
* get_rating_count function.
*
* @param int $value Optional. Rating value to get the count for. By default
* returns the count of all rating values.
* @return int
*/
public function get_rating_count() {
public function get_rating_count( $value = null ) {
if ( false === ( $count = get_transient( 'wc_rating_count_' . $this->id ) ) ) {
$value = intval( $value );
$value_suffix = $value ? '_' . $value : '';
if ( false === ( $count = get_transient( 'wc_rating_count_' . $this->id . $value_suffix ) ) ) {
global $wpdb;
$where_meta_value = $value ? $wpdb->prepare( " AND meta_value = %d", $value ) : " AND meta_value > 0";
$count = $wpdb->get_var( $wpdb->prepare("
SELECT COUNT(meta_value) 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
", $this->id ) );
", $this->id ) . $where_meta_value );
set_transient( 'wc_rating_count_' . $this->id, $count, YEAR_IN_SECONDS );
set_transient( 'wc_rating_count_' . $this->id . $value_suffix, $count, YEAR_IN_SECONDS );
}
return $count;

View File

@ -211,8 +211,14 @@ class WC_Comments {
* @param mixed $post_id
*/
public static function clear_transients( $post_id ) {
delete_transient( 'wc_average_rating_' . absint( $post_id ) );
delete_transient( 'wc_rating_count_' . absint( $post_id ) );
$post_id = absint( $post_id );
delete_transient( 'wc_average_rating_' . $post_id );
delete_transient( 'wc_rating_count_' . $post_id );
delete_transient( 'wc_rating_count_' . $post_id . '_1' );
delete_transient( 'wc_rating_count_' . $post_id . '_2' );
delete_transient( 'wc_rating_count_' . $post_id . '_3' );
delete_transient( 'wc_rating_count_' . $post_id . '_4' );
delete_transient( 'wc_rating_count_' . $post_id . '_5' );
}
/**