Made use of transients to store average ratings and improve performance. Closes #105.
This commit is contained in:
parent
c3f5ccb637
commit
223f9ccad3
|
@ -527,34 +527,45 @@ class woocommerce_product {
|
|||
return $price;
|
||||
}
|
||||
|
||||
/** Returns the product rating in html format */
|
||||
/** Returns the product rating in html format - ratings are stored in transient cache */
|
||||
function get_rating_html( $location = '' ) {
|
||||
global $wpdb;
|
||||
|
||||
if ($location) $location = '_'.$location;
|
||||
$star_size = apply_filters('woocommerce_star_rating_size'.$location, 16);
|
||||
|
||||
if ( false === ( $average_rating = get_transient( $this->id . '_woocommerce_average_rating' ) ) ) :
|
||||
|
||||
$count = $wpdb->get_var("
|
||||
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 = $this->id
|
||||
AND comment_approved = '1'
|
||||
AND meta_value > 0
|
||||
");
|
||||
|
||||
$ratings = $wpdb->get_var("
|
||||
SELECT SUM(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 = $this->id
|
||||
AND comment_approved = '1'
|
||||
");
|
||||
|
||||
if ( $count>0 ) :
|
||||
$rating = number_format($ratings / $count, 2);
|
||||
global $wpdb;
|
||||
|
||||
$count = $wpdb->get_var("
|
||||
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 = $this->id
|
||||
AND comment_approved = '1'
|
||||
AND meta_value > 0
|
||||
");
|
||||
|
||||
return '<div class="star-rating" title="'.sprintf(__('Rated %s out of 5', 'woothemes'), $rating).'"><span style="width:'.($rating*$star_size).'px"><span class="rating">'.$rating.'</span> '.__('out of 5', 'woothemes').'</span></div>';
|
||||
$ratings = $wpdb->get_var("
|
||||
SELECT SUM(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 = $this->id
|
||||
AND comment_approved = '1'
|
||||
");
|
||||
|
||||
if ( $count>0 ) :
|
||||
$average_rating = number_format($ratings / $count, 2);
|
||||
else :
|
||||
$average_rating = '';
|
||||
endif;
|
||||
|
||||
set_transient( $this->id . '_woocommerce_average_rating', $average_rating );
|
||||
|
||||
endif;
|
||||
|
||||
if ( $average_rating>0 ) :
|
||||
return '<div class="star-rating" title="'.sprintf(__('Rated %s out of 5', 'woothemes'), $average_rating).'"><span style="width:'.($average_rating*$star_size).'px"><span class="rating">'.$average_rating.'</span> '.__('out of 5', 'woothemes').'</span></div>';
|
||||
else :
|
||||
return '';
|
||||
endif;
|
||||
|
|
|
@ -93,6 +93,7 @@ Yes you can! Join in on our GitHub repository :) https://github.com/woothemes/wo
|
|||
* Added basic rss feeds for products and product categories
|
||||
* Added functions which show tax/vat conditionally
|
||||
* Fixed variations - Incorrectly used instead $product_custom_fields of $parent_custom_fields
|
||||
* Made use of transients to store average ratings and improve performance
|
||||
|
||||
= 1.1.3 - 27/10/2011 =
|
||||
* Improved Force SSL Setting - now forces https urls for enqueued scripts and styles
|
||||
|
|
|
@ -515,8 +515,10 @@ function woocommerce_clean( $var ) {
|
|||
**/
|
||||
function woocommerce_add_comment_rating($comment_id) {
|
||||
if ( isset($_POST['rating']) ) :
|
||||
global $post;
|
||||
if (!$_POST['rating'] || $_POST['rating'] > 5 || $_POST['rating'] < 0) $_POST['rating'] = 5;
|
||||
add_comment_meta( $comment_id, 'rating', $_POST['rating'], true );
|
||||
add_comment_meta( $comment_id, 'rating', esc_attr($_POST['rating']), true );
|
||||
delete_transient( esc_attr($post->ID) . '_woocommerce_average_rating' );
|
||||
endif;
|
||||
}
|
||||
add_action( 'comment_post', 'woocommerce_add_comment_rating', 1 );
|
||||
|
|
Loading…
Reference in New Issue