• Resolved gkartikey

    (@gkartikey)


    Hi,

    I wanted to know how I can add a count of the number of reviews alongside Star Rating on the Shop page and Product Page?

Viewing 11 replies - 1 through 11 (of 11 total)
  • Hi,
    It’s the function of WooCommerce. If your site does not have it, your theme has disabled that function.
    You can reuse it by adding this to file functions.php of your theme:
    For archive page:

    add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
    if ( ! function_exists( 'woocommerce_template_loop_rating' ) ) {
    
    	/**
    	 * Display the average rating in the loop.
    	 */
    	function woocommerce_template_loop_rating() {
    		wc_get_template( 'loop/rating.php' );
    	}
    }

    For product single page:

    add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10 );
    if ( ! function_exists( 'woocommerce_template_single_rating' ) ) {
    
    	/**
    	 * Output the product rating.
    	 */
    	function woocommerce_template_single_rating() {
    		if ( post_type_supports( 'product', 'comments' ) ) {
    			wc_get_template( 'single-product/rating.php' );
    		}
    	}
    }

    Best regards

    Thread Starter gkartikey

    (@gkartikey)

    Hi,

    I am using a child theme, so I pasted in the child theme’s functions.php, and it still does not show a count of rating. So, help me with how I should go about it?

    I just don’t know what you did with your childtheme so it’s hard to check.
    Please try changing the name of function:

    add_action( 'woocommerce_after_shop_loop_item_title', 'gkartikey_woocommerce_template_loop_rating', 5 );
    if ( ! function_exists( 'gkartikey_woocommerce_template_loop_rating' ) ) {
    
    	/**
    	 * Display the average rating in the loop.
    	 */
    	function gkartikey_woocommerce_template_loop_rating() {
    		wc_get_template( 'loop/rating.php' );
    	}
    }
    add_action( 'woocommerce_single_product_summary', 'gkartikey_woocommerce_template_single_rating', 10 );
    if ( ! function_exists( 'gkartikey_woocommerce_template_single_rating' ) ) {
    
    	/**
    	 * Output the product rating.
    	 */
    	function gkartikey_woocommerce_template_single_rating() {
    		if ( post_type_supports( 'product', 'comments' ) ) {
    			wc_get_template( 'single-product/rating.php' );
    		}
    	}
    }
    
    Thread Starter gkartikey

    (@gkartikey)

    Oh no, I am using Flatsome’s child theme, so what should be the solution then?

    Okay ignore the code above.
    Just go to folder plugins/woocommerce/templates/single-product/ to copy file rating.php and replace the same file in themes/flatsome-child/woocommerce/single-product/. This will display the number of reviews after star rating in product single page.

    Thread Starter gkartikey

    (@gkartikey)

    Hi,

    My bad, there was an option to show review count in UX builder for the product page.
    But, I still need to know how I can add the review count (4) like this on the archive/shop page.

    Just add this to file functions.php:

    add_filter( 'woocommerce_product_get_rating_html', function ( $html, $rating, $count ) {
    	global $product;
    	if ( $html && is_archive() && $product) {
    		$html .= sprintf( '<span>(%s)</span>', $product->get_rating_count() );
    	}
    
    	return $html;
    }, 10, 3 );

    But you have to style it yourself.

    Thread Starter gkartikey

    (@gkartikey)

    Hi,

    How to show it inline with the stars showing?
    Help me out in this as well.
    It is now showing below the rating.

    Okay modify code above like this:

    add_filter( 'woocommerce_product_get_rating_html', function ( $html, $rating, $count ) {
    	global $product;
    	if ( $html && is_archive() && $product) {
    		$html .= sprintf( '<div class="gkartikey-product-rating-count">(%s)</div>', $product->get_rating_count() );
    	}
    
    	return $html;
    }, 10, 3 );

    And use a custom css plugin to add this css and make it run only on archive page:

    .star-rating{
    float:left;
    }
    .gkartikey-product-rating-count{
        height: 1em;
        line-height: 1em;
        margin: 10px 0;
        display: inline-block;
    }
    .price-wrapper .price {
        clear: both;
    }
    Thread Starter gkartikey

    (@gkartikey)

    Hi,

    I used Code Snippets plugin to place the CSS, and now there is a huge gap between the review star rating and the review count.
    How to fix that?
    Or please share me your mail I’ll share the link there.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Add Count of the number of reviews alongside Star Rating’ is closed to new replies.