Support » Plugin: YITH WooCommerce Advanced Reviews » How to display Empty Stars when 0 reviews

  • Hi there!

    Great plugin you’ve made! Thank you for it!

    Please may you tell how to make visible Empty Starts on a Single Product and Product Category pages ?

    I’ve found this code and it works when editing 1473 line of your plugin code (file: class.yith-woocommerce-advanced-reviews.php ):

    public function get_product_rating_html ( $rating_html, $rating ) {
                global $product;
                $rating_html = '';
    
                $rating = $this->get_average_rating ( $product->id );
                $noreview = '';
                if ( ICL_LANGUAGE_CODE=='en' ){ $noreview = "No reviews yet";}
                else { $noreview = '<div class="star-rating" title="' . sprintf ( __ ( 'Rated %s out of 5', 'yith-woocommerce-advanced-reviews' ), $rating ) . '">';}
    
                if ( $rating > 0 ) {
    
                    $rating_html = '<div class="star-rating" title="' . sprintf ( __ ( 'Rated %s out of 5', 'yith-woocommerce-advanced-reviews' ), $rating ) . '">';
    
                    $rating_html .= '<span style="width:' . ( ( $rating / 5 ) * 100 ) . '%"><strong class="rating">' . $rating . '</strong> ' . __ ( 'out of 5', 'yith-woocommerce-advanced-reviews' ) . '</span>';
    
                    $rating_html .= '</div>';
                }else{
                    $rating_html = '<div>'.$noreview.'</div>';
                }
    
                return $rating_html;
            }

    But it disappear on every update of your plugin and it also not making visible empty stars on a Single product page – only on a Product Category pages.

    May you please share any correct code which I can insert in my theme functions.php to maintain the visibility of empty stars on a Single Product Page and Product Category Page ?

    Greatly Appreciate you help!
    With Regards!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author YITHEMES

    (@yithemes)

    Hello there,
    hope you are doing well

    To achieve what you need in category pages, you have to add the following code to the functions.php file of your active theme:

    remove_filter( 'woocommerce_product_get_rating_html', array( YITH_WooCommerce_Advanced_Reviews::get_instance(), 'get_product_rating_html' ), 99 );
    
    if ( ! function_exists( 'get_product_rating_html' ) ) {
    	function get_product_rating_html( $rating_html, $rating ) {
    		global $product;
    		$rating_html = '';
    
    		if ( ! $product ) {
    			return $rating_html;
    		}
    
    		$rating = YITH_WooCommerce_Advanced_Reviews::get_instance()->get_average_rating( yit_get_prop( $product, 'id' ) );
    
    		if ( $rating >= 0 ) {
    			$rating_html  = '<div class="star-rating" title="' . sprintf( __( 'Rated %s out of 5', 'yith-woocommerce-advanced-reviews' ), $rating ) . '">';
    			$rating_html .= '<span style="width:' . ( ( $rating / 5 ) * 100 ) . '%"><strong class="rating">' . $rating . '</strong> ' . __( 'out of 5', 'yith-woocommerce-advanced-reviews' ) . '</span>';
    			$rating_html .= '</div>';
    		}
    
    		return $rating_html;
    	}
    
    	add_filter( 'woocommerce_product_get_rating_html', 'get_product_rating_html', 10, 2 );
    }

    To get these changes also in your product pages, you will need to override a plugin template. We will tell you the steps you have to follow to achieve this:
    1. Copy the ywar-rating.php (found in yith-woocommerce-advanced-reviews/templates) template into your theme in the following path: wp-content/themes/your_active_theme/woocommerce
    2. Open this template from your theme and replace the line 23 with the following code: if ( $rating_count >= 0 ) : ?>

    Let me know any news, please.

    Have a nice day!

    • This reply was modified 1 year, 11 months ago by YITHEMES.
    Thread Starter ptm282192

    (@ptm282192)

    Awesome, thank you, everything is working as required!

    Just one moment – is it possible to add any changes in the code you provided for the functions.php file to make visible stars and a reviews count near it – ⭐⭐⭐⭐⭐ (12 reviews) on a product category page (just the same as it showing on a single product page) ?

    If you can share the changes to make in the functions.php code you provided earlier it would be really great!

    Thank you a lot with all your help!
    Regards

    Thread Starter ptm282192

    (@ptm282192)

    Awesome, thank you, everything is working as required!

    Just one moment – is it possible to add any changes in the code you provided for the functions.php file to make visible stars and a reviews count near it – ⭐⭐⭐⭐⭐ (12 reviews) on a product category page (just the same as it showing on a single product page) ?

    If you can share the changes to make in the functions.php code you provided earlier it would be really great!

    Thank you a lot with all your help!
    Regards

    Plugin Author YITHEMES

    (@yithemes)

    Hello there,

    hope you are doing well!

    In order to display a review count near the review stars also in your category pages, you have to replace the code (in functions.php file) we sent you previously with the following one:

    remove_filter( 'woocommerce_product_get_rating_html', array( YITH_WooCommerce_Advanced_Reviews::get_instance(), 'get_product_rating_html' ), 99 );
    
    if ( ! function_exists( 'get_product_rating_html' ) ) {
    	function get_product_rating_html( $rating_html, $rating ) {
    		global $YWAR_AdvancedReview;
    		global $product;
    		$rating_html = '';
    
    		$product_id = yit_get_prop( $product, 'id' );
    		$review_count = count( $YWAR_AdvancedReview->get_product_reviews( $product_id ) );
    
    		if ( ! $product ) {
    			return $rating_html;
    		}
    
    		$rating = YITH_WooCommerce_Advanced_Reviews::get_instance()->get_average_rating( yit_get_prop( $product, 'id' ) );
    
    		if ( $rating >= 0 ) {
    			$rating_html  = '<div class="star-rating" title="' . sprintf( __( 'Rated %s out of 5', 'yith-woocommerce-advanced-reviews' ), $rating ) . '">';
    			$rating_html .= '<span style="width:' . ( ( $rating / 5 ) * 100 ) . '%"><strong class="rating">' . $rating . '</strong> ' . __( 'out of 5', 'yith-woocommerce-advanced-reviews' ) . '</span>';
    
    			if ( comments_open() ) {
    				?>
    				<a href="" class="woocommerce-review-link" rel="nofollow">(<?php printf( _n( '%s customer review', '%s customer reviews', $review_count, 'yith-woocommerce-advanced-reviews' ), '<span itemprop="reviewCount" class="count">' . $review_count . '</span>' ); ?>)</a>
    				<?php
    			}
    
    			$rating_html .= '</div>';
    		}
    
    		return $rating_html;
    	}
    
    	add_filter( 'woocommerce_product_get_rating_html', 'get_product_rating_html', 10, 2 );
    }

    Let us know any news, please.

    Have a nice day!

    Thread Starter ptm282192

    (@ptm282192)

    Thank you!
    Strange that not every product showing the Review count, some of the products does not show on category page, don’t you know why that may happen?
    Regards

    Ruben

    (@rubenvankempen)

    Work fine on shop-page.
    Is this also possible for a productpage?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to display Empty Stars when 0 reviews’ is closed to new replies.