Advertisement
eappereira

Function Rating WOO

Apr 22nd, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.16 KB | None | 0 0
  1. add_action('woocommerce_after_shop_loop_item', 'woo_rating_stars' );
  2.  
  3. function woo_rating_stars(){
  4.     global $wpdb;
  5.     global $post;
  6.     $count = $wpdb->get_var("
  7.    SELECT COUNT(meta_value) FROM $wpdb->commentmeta
  8.    LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
  9.    WHERE meta_key = 'rating'
  10.    AND comment_post_ID = $post->ID
  11.    AND comment_approved = '1'
  12.    AND meta_value > 0
  13. ");
  14.  
  15. $rating = $wpdb->get_var("
  16.    SELECT SUM(meta_value) FROM $wpdb->commentmeta
  17.    LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
  18.    WHERE meta_key = 'rating'
  19.    AND comment_post_ID = $post->ID
  20.    AND comment_approved = '1'
  21. ");
  22.  
  23. if ( $count > 0 ) {
  24.  
  25.     $average = number_format($rating / $count, 2);
  26.     echo '<div class="starwrapper" itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">';
  27.     echo '<span class="star-rating" title="'.sprintf(__('Classificado %s de 5', 'woocommerce'), $average).'"><span style="width:'.($average*16).'px"><span itemprop="ratingValue" class="rating">'.$average.'</span> </span></span>';
  28.  
  29.     echo '</div>';
  30.     }
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement