Jump to content
Sign in to follow this  
confusedcart

Average Review Stars on product_info.php

Recommended Posts

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:

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites

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'];

Share this post


Link to post
Share on other sites
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. :smile:

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 ! :wink:

Edited by milerwan

Osc v2.3.4 BS "custom"

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×