Is it possible to round up star ratings?
Site Reviews already does this.
For example if a product has 99 five star ratings, and 1 four star rating, the total star rating will be 4.5.
Not quite. Your example of 1 x 4-star ratings
and 99 x 5-star ratings
will return an average rating of 5.0.
You can test like this:
$counts = [
0, // 0-star ratings
0, // 1-star ratings
0, // 2-star ratings
0, // 3-star ratings
1, // 4-star ratings
99, // 5-star ratings
];
$results = [
'average' => glsr('Modules\Rating')->average($counts),
'ranking' => glsr('Modules\Rating')->ranking($counts),
'reviews' => array_sum($counts),
];
The result will be:
[
"average" => 5.0,
"ranking" => 4.8636363636364,
"reviews" => 100,
]
However, if you are talking about rounding up the Bayesian-calculated ranking number, then the result wouldn’t be a true Bayesian ranking value. The calculated Bayesian ranking values are used for sorting, not displaying.
@geminilabs Okay, that’s fair. My example was arbitrary in order to demonstrate a point.
I have a product that has a total rating of 4.9, but the star rating is 4.5. (https://ibb.co/YpYgBJV)
I would like the ratings of 4.8 or higher to round up to 5 stars, 4.3 rounds up to 4.5, etc.
For clarification, this has nothing to do with bayesian related sorting.
Ah, okay well if you want to manually adjust the calculated average rating number, you can do it with a code snippet like this:
add_filter('site-reviews/rating/average', function (float $average) {
if ($average < 4.8) {
return $average;
}
return 5;
});
But, I think you are not talking about the displayed rating number, but rather about the star images, correct?
@geminilabs That’s correct, I only want to adjust the star images.
Ok, well in that case you can do this:
add_filter('site-reviews/defaults/star-rating', function (array $values) {
$rating = $values['rating'] ?? 0;
if ($rating >= 4.8) {
$values['num_full'] = 5;
$values['num_half'] = 0;
}
return $values;
});
@geminilabs Thanks so much for pointing me in the right direction!
Here’s the full code for anybody who wants to do the same–
// Round up star rating images
add_filter('site-reviews/defaults/star-rating', function (array $values) {
$rating = $values['rating'] ?? 0;
// Apply conditions for different rating ranges
if ($rating >= 4.8) {
$values['num_full'] = 5;
$values['num_half'] = 0;
}
else if ($rating >= 4.3) {
$values['num_full'] = 4;
$values['num_half'] = 1;
}
else if ($rating >= 3.8) {
$values['num_full'] = 4;
$values['num_half'] = 0;
}
else if ($rating >= 3.3) {
$values['num_full'] = 3;
$values['num_half'] = 1;
}
else if ($rating >= 2.8) {
$values['num_full'] = 3;
$values['num_half'] = 0;
}
else if ($rating >= 2.3) {
$values['num_full'] = 2;
$values['num_half'] = 1;
}
else if ($rating >= 1.8) {
$values['num_full'] = 2;
$values['num_half'] = 0;
}
else if ($rating >= 1.3) {
$values['num_full'] = 1;
$values['num_half'] = 1;
}
else if ($rating >= 1) {
$values['num_full'] = 1;
$values['num_half'] = 0;
}
else {
$values['num_full'] = 0;
$values['num_half'] = 0;
}
return $values;
});