Archived topic
Displaying number of reviews on product archive pages in Woocommerce
20 replies · Started by Dominic on January 28, 2023
And this is where Woo gets real tricky :)
1. The PHP Snippet would look like this:
add_filter('woocommerce_product_get_rating_html', function( $html, $rating, $count ){
global $woocommerce_loop;
global $product;
$loop_data = $woocommerce_loop['name'];
$review_count = $product->get_review_count();
if ( $review_count && $loop_data ) {
$html = '<div class="rating-wrapper">' . $html . $review_count . '</div>';
} elseif ( $review_count < 1 && !is_product() ) {
$html = '';
}
return $html;
},10,3);
This checks that the reviews are in the loop, so it doesn't mess with the one below the single product title.
NOTE: I don't think it will remove empty stars on the related / upsell products...
2. And this is the CSS where we make it only remove margins if the rating-wrapper is present.
ul.products li.product.woocommerce-text-align-center .rating-wrapper .star-rating {
margin: 0 !important;
}
.rating-wrapper {
display: flex;
justify-content: center;
align-items: center;
gap: 10px;
}
Thank you once again!
It worked.
I used this CSS for the empty star rating:
.woocommerce .star-rating:before {
opacity: 0;
}
It works and I think this is the correct CSS that targets only the empty star rating on the single product page. (and no other objects)
You're welcome
I used the code provided (in the code snippets plugin) and it worked fine on product category pages but it gave PHP errors on the homepage.
Hi Jordan,
Is your homepage the shop page or a static page?
The code will NOT work on a static page.
Can you open a new topic and attach your site link?
Thank you for the fast reply, Ying. Here is the new thread: https://generatepress.com/forums/topic/php-code-for-review-count-on-product-archive-pages/