Page 1 of 3

Star Rating on Product Card?

Posted: Mon Feb 26, 2024 8:34 am
by lecarlb
I was wondering if there's an addon that support this feature. I would like to show the rating stars on the product cards.

Has anyone done this? If so please share how to do it.

Thank you.

Re: Star Rating on Product Card?

Posted: Mon Feb 26, 2024 9:41 am
by burt
Override the card component and add in a call to Star Rating class.
Should be pretty straightforward. Give it a go?

Re: Star Rating on Product Card?

Posted: Mon Feb 26, 2024 4:59 pm
by 14Steve14
burt wrote: Mon Feb 26, 2024 9:41 am Override the card component and add in a call to Star Rating class.
Should be pretty straightforward. Give it a go?
I thought about this also, and had no end of problems. I got the model number to show, but the star ratings just created a blank page. I may have to try again when I see the errors again. My problem was that I didnt really know how much of the code copied from one of the other areas where the rating show was required.

Re: Star Rating on Product Card?

Posted: Mon Feb 26, 2024 5:35 pm
by heatherbell
After making a copy of /templates/default/includes/components/product_card.php
Save it in:
templates/override/includes/components/product_card.php
Edit to add the following code into the card where you want, after the price looks OK.

Code: Select all

    <?php
    $review_ratings = [];
      $review_count = count($product->get('reviews'));
      if ($review_count > 0) {
        $review_ratings[] = new star_rating((float)(int)$product->get('review_rating'));
      }
      foreach ($review_ratings as $i => $rating) {
        echo '<div class="text-center mt-2 ' . $i . '">' . $rating . '</div>';
      }
    ?>
Beware that this will show Stars twice on Reviews Box Module if that is installed.
Screenshot 2024-02-26 173254.png

Re: Star Rating on Product Card?

Posted: Mon Feb 26, 2024 6:07 pm
by burt
heatherbell wrote: Mon Feb 26, 2024 5:35 pm Beware that this will show Stars twice on Reviews Box Module if that is installed.
This is something that needs to be solved at the Core Level to make changes to "product card" more straightforward.

I have the perfect solution which I'll commit in the 1.0.9.x series.

Re: Star Rating on Product Card?

Posted: Tue Feb 27, 2024 4:03 am
by lecarlb
Thanks @heatherbell @burt

Is there a way to show the review count or number of reviews?

I've attached an example. Changing the color of the stars is not important but may be considered.

Also, how will the above code work on a 1.0.7.18 shop?

Re: Star Rating on Product Card?

Posted: Tue Feb 27, 2024 7:13 am
by heatherbell
lecarlb wrote: Tue Feb 27, 2024 4:03 am Is there a way to show the review count or number of reviews?
Yes, the code posted already shows that the count of reviews is defined so add that to the div.
Screenshot 2024-02-27 070328.png

Re: Star Rating on Product Card?

Posted: Tue Feb 27, 2024 7:23 am
by lecarlb
so add that to the div.
Not sure what that means.

Thank you for the quick response.

Re: Star Rating on Product Card?

Posted: Tue Feb 27, 2024 8:02 am
by heatherbell
lecarlb wrote: Tue Feb 27, 2024 7:23 am Not sure what that means.

Code: Select all

<div class="text-center mt-2 ' . $i . '">' . $rating . '&nbsp;(' . $review_count . ')</div>

Re: Star Rating on Product Card?

Posted: Tue Feb 27, 2024 8:15 am
by lecarlb
That works thank you.

Would the code be different in a 1.0.7.18 shop? I can confirm that code works in a 1.0.8.4 shop.