Support » Plugin: WooCommerce » Woocommerce always show stars – even with no reviews

  • Goal: All products should display rating stars even when there are no reviews yet.

    Found this snippet and it works great in my shop loop, plugins, and customized themes.

    Problem is, it does not work on single product pages – tried with Storefront, Beaver Builder Child Theme, and a few other themes.

    Any help would be greatly appreciated 🙂

    
    // Woocommerce rating stars always
    	add_filter('woocommerce_product_get_rating_html', 'your_get_rating_html', 10, 2);
    
    	function your_get_rating_html($rating_html, $rating) {
      	if ( $rating > 0 ) {
        	$title = sprintf( __( 'Rated %s out of 5', 'woocommerce' ), $rating );
      	} else {
        	$title = 'Not yet rated';
        	$rating = 0;
      	}
    
      	$rating_html  = '<div class="star-rating" title="' . $title . '">';
      	$rating_html .= '<span style="width:' . ( ( $rating / 5 ) * 100 ) . '%"><strong class="rating">' . $rating . '</strong> ' . __( 'out of 5', 'woocommerce' ) . '</span>';
      	$rating_html .= '</div>';
      	return $rating_html;
    	}
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • The same question, I hope to find a solution.

    For the single product page, unfortunately this filter is called after the test to check that the rating is not 0, so too late for this purpose. It will be necessary to make a custom template. Copy
    plugins/woocommerce/templates/single-product/review-rating.php
    to
    themes/your-child-theme-name/woocommerce/single-product/review-rating.php
    and rewrite the ‘if’ statement in the copy.

    For single product page you should change the if statement in ” themes/your-child-theme-name/woocommerce/single-product/rating.php”

    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly
    }
    global $product;
    if ( 'no' === get_option( 'woocommerce_enable_review_rating' ) ) {
    	return;
    }
    $rating_count = $product->get_rating_count();
    $review_count = $product->get_review_count();
    $average      = $product->get_average_rating();
    if ( $rating_count > 0 ) : ?>

    ——

    if ( $rating_count > 0 ) : ?> change to if ( $rating_count >= 0 ) : ?>

    • This reply was modified 3 years, 8 months ago by nzecheru.

    Came accross the same issue. No need to edit any core file. For single product page and/or product loop sections, add the following function to the end of your theme’s functions.php file :

    // always display rating stars
    function filter_woocommerce_product_get_rating_html( $rating_html, $rating, $count ) { 
        $rating_html  = '<div class="star-rating">';
        $rating_html .= wc_get_star_rating_html( $rating, $count );
        $rating_html .= '</div>';
    
        return $rating_html; 
    };  
    add_filter( 'woocommerce_product_get_rating_html', 'filter_woocommerce_product_get_rating_html', 10, 3 ); 
    • This reply was modified 3 years, 7 months ago by Ali Ozinan.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Woocommerce always show stars – even with no reviews’ is closed to new replies.