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;
}