Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Average Review Stars on product_info.php


confusedcart

Recommended Posts

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.

Link to comment
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

Link to comment
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.

Link to comment
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 :)

Link to comment
Share on other sites

  • 7 years later...
  • 2 years later...
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"
PHP 7.3 compatible (710 modified files => o_O')

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...