• Resolved stylise

    (@stylise)


    Hello. Is it possible to round up star ratings?

    For example if a product has 99 five star ratings, and 1 four star rating, the total star rating will be 4.5.

    I would like the ratings of 4.8 or higher to round up to 5 stars, 4.3 rounds up to 4.5, etc. This is how Amazon’s star rating system works. It would also make more sense when using the bayesian ranking product sorting method.

    Thanks in advance.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Gemini Labs

    (@geminilabs)

    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.

    Thread Starter stylise

    (@stylise)

    @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.

    Plugin Author Gemini Labs

    (@geminilabs)

    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?

    Thread Starter stylise

    (@stylise)

    @geminilabs That’s correct, I only want to adjust the star images.

    Plugin Author Gemini Labs

    (@geminilabs)

    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;
    });
    Thread Starter stylise

    (@stylise)

    @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;
    });
    Plugin Author Gemini Labs

    (@geminilabs)

    (Edited)

    • This reply was modified 1 week, 1 day ago by Gemini Labs.
    Plugin Author Gemini Labs

    (@geminilabs)

    @stylise Here is an optimized version of your code:

    add_filter('site-reviews/defaults/star-rating', function (array $values) {
    $minThreshold = 0.3; // adjust as needed
    $maxThreshold = 0.8; // adjust as needed
    $values = wp_parse_args($values, [
    'num_empty' => 0,
    'num_full' => 0,
    'num_half' => 0,
    'rating' => 0,
    ]);
    $fraction = round(fmod($values['rating'], 1), 10);
    $isHalved = ($fraction >= $minThreshold && $fraction < $maxThreshold);
    $maxStars = $values['num_empty'] + $values['num_full'] + $values['num_half'];
    $values['num_full'] = (int) $values['rating'] + ($fraction >= $maxThreshold ? 1 : 0);
    $values['num_half'] = $isHalved ? 1 : 0;
    $values['num_empty'] = $maxStars - $values['num_full'] - $values['num_half'];
    return $values;
    });
    Thread Starter stylise

    (@stylise)

    @geminilabs Thank you for optimizing the code! Tested and working.

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.