confusedcart Posted December 12, 2008 Share Posted December 12, 2008 I'd like to get the average rating stars from product reviews to display on the product_info page as on Amazon.com. How does one do this?? Any ideas?? It should help up sales and increase confidence if there's an average star review for each product. :blink: Quote Link to comment Share on other sites More sharing options...
Kanie Posted December 13, 2008 Share Posted December 13, 2008 I will explain quickly how to get the average rating, but where and how you display these variables is up to you. 1) In product_info.php locate: $reviews_query = tep_db_query("select count(*) as count from " . TABLE_REVIEWS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'"); Replace it with: $reviews_query = tep_db_query("select count(*) as count, sum(reviews_rating) as total from " . TABLE_REVIEWS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'"); 2) Locate: if ($reviews['count'] > 0) { Directly after this line paste: $average_rating = round(($reviews['total'] / $reviews['count']), 2); The variable $average_rating now contains the textual average rating for the product. You can change the "2" to the amount of decimals you wish to round to. How you use and display this variable is entirely up to you. Getting this variable to become star images takes a little more work, but it's not impossible. Quote Link to comment Share on other sites More sharing options...
confusedcart Posted December 13, 2008 Author Share Posted December 13, 2008 I will explain quickly how to get the average rating, but where and how you display these variables is up to you. 1) In product_info.php locate: $reviews_query = tep_db_query("select count(*) as count from " . TABLE_REVIEWS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'"); Replace it with: $reviews_query = tep_db_query("select count(*) as count, sum(reviews_rating) as total from " . TABLE_REVIEWS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'"); 2) Locate: if ($reviews['count'] > 0) { Directly after this line paste: $average_rating = round(($reviews['total'] / $reviews['count']), 2); The variable $average_rating now contains the textual average rating for the product. You can change the "2" to the amount of decimals you wish to round to. How you use and display this variable is entirely up to you. Getting this variable to become star images takes a little more work, but it's not impossible. Thanks!!! I did some late night searching.. This contrib does it with the stars http://addons.oscommerce.com/info/1438 Quote Link to comment Share on other sites More sharing options...
Kanie Posted December 13, 2008 Share Posted December 13, 2008 Yeah, I saw that contribution but it appears it doesn't account for half ratings, but rather just rounds the average up/down to a whole number. That approach is fine, of course, but with with a little extra code added to the example I provided above you could display 3.5 stars for a more accurate rating guide. Quote Link to comment Share on other sites More sharing options...
confusedcart Posted December 13, 2008 Author Share Posted December 13, 2008 Yeah, I saw that contribution but it appears it doesn't account for half ratings, but rather just rounds the average up/down to a whole number. That approach is fine, of course, but with with a little extra code added to the example I provided above you could display 3.5 stars for a more accurate rating guide. I noticed that now.. thanks :) Quote Link to comment Share on other sites More sharing options...
alexiscruz007 Posted March 23, 2016 Share Posted March 23, 2016 You can used too: $reviews_query = tep_db_query("select count(*) as count, round((avg(reviews_rating)),1) as total from " . TABLE_REVIEWS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'"); echo RATING . ' ' . $reviews['total']; Quote Link to comment Share on other sites More sharing options...
milerwan Posted May 21, 2018 Share Posted May 21, 2018 (edited) On 13/12/2008 at 12:30 PM, Kanie said: Yeah, I saw that contribution but it appears it doesn't account for half ratings, but rather just rounds the average up/down to a whole number. That approach is fine, of course, but with with a little extra code added to the example I provided above you could display 3.5 stars for a more accurate rating guide. I have made the extra code. 1. In includes/functions/html_output.php file, find : // review stars function tep_draw_stars($rating = 0) { $stars = str_repeat('<span class="fa fa-star"></span>', (int)$rating); $stars .= str_repeat('<span class="fa fa-star-o"></span>', 5-(int)$rating); return $stars; } And replace by : // review stars function tep_draw_stars($rating = 0) { if ( (substr($rating, 2) <= 2) ) { $stars = str_repeat('<span class="fa fa-star"></span>', (int)$rating); $stars .= str_repeat('<span class="fa fa-star-o"></span>', 5-(int)$rating); } elseif ( (substr($rating, 2) >= 3) and (substr($rating, 2) <= 7) ) { $stars = str_repeat('<span class="fa fa-star"></span>', (int)$rating); $stars .= str_repeat('<span class="fa fa-star-half-o"></span>', 1); $stars .= str_repeat('<span class="fa fa-star-o"></span>', 4-(int)$rating); } else { $stars = str_repeat('<span class="fa fa-star"></span>', (int)round($rating)); $stars .= str_repeat('<span class="fa fa-star-o"></span>', 5-(int)round($rating)); } return $stars; } Now we have to modify "round($average['average'])" sentences for decimal use in product_info.php and product_reviews.php files. 2. In product_info.php file, find : $products_name .= '<span class="small">' . sprintf(REVIEWS_TEXT_AVERAGE_SHORT, tep_draw_stars(tep_output_string_protected(round($average['average']))), tep_output_string_protected($average['count'])) . '</span>'; And replace by : $products_name .= '<span class="small">' . sprintf(REVIEWS_TEXT_AVERAGE_SHORT, tep_draw_stars(tep_output_string_protected(round($average['average'], 1))), tep_output_string_protected($average['count'])) . '</span>'; 3. In product_reviews.php file, find : echo '<div class="col-sm-8 text-center alert alert-success" itemprop="AggregateRating" itemscope itemtype="http://schema.org/AggregateRating"><meta itemprop="ratingValue" content="' . max(1, (int)round($average['average'])) . '" /><meta itemprop="bestRating" content="5" />' . sprintf(REVIEWS_TEXT_AVERAGE, tep_output_string_protected($average['count']), tep_draw_stars(tep_output_string_protected(round($average['average'])))) . '</div>'; And replace by : echo '<div class="col-sm-8 text-center alert alert-success" itemprop="AggregateRating" itemscope itemtype="https://schema.org/AggregateRating"><meta itemprop="ratingValue" content="' . round($average['average'], 1) . '" /><meta itemprop="bestRating" content="5" />' . sprintf(REVIEWS_TEXT_AVERAGE, tep_output_string_protected($average['count']), tep_draw_stars(tep_output_string_protected(round($average['average'], 1)))) . ' (' . round($average['average'], 1) . '/5)</div>'; Enjoy ! Edited May 21, 2018 by milerwan Quote Osc v2.3.4 BS "custom" PHP 7.3 compatible (710 modified files => o_O') Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.