Add stars rating and average to product "Review" item menu in Woocommerce

Viewed 4117

I'm trying to add the following: "4.5 / 5 (5 reviews)" but without the "" to the review tab for all WooCommerce products.

I've accomplished to add the stars and some text, but I cannot figure out how to make the text into "4.5 / 5 (5 reviews)". Here's the code I'm using. Just to be clear, I want to keep the stars.

add_filter( 'woocommerce_product_reviews_tab_title', 'add_stars_to_review_tab', 98 );
function add_stars_to_review_tab( $title ) {
    global $product;

    $average_rating = $product->get_average_rating();
    $rating_count = $product->get_rating_count();
    $review_count = $product->get_review_count();

    if( ! empty($average_rating) && $average_rating > 0 )
        $title = '<div>' . $title . '</div>
        <div class="stars">' . wc_get_rating_html($average_rating) . ' based on ' . $rating_count. ' reviews' . '</div>';

    return $title;
}
2 Answers

Update 2: The following code will display something like "4.5/5 (5 reviews)" below the review item menu title and stars in Woocommerce single product pages tabs:

add_filter( 'woocommerce_product_reviews_tab_title', 'add_stars_to_review_tab', 98 );
function add_stars_to_review_tab( $title ) {
    global $product;

    $average_rating = $product->get_average_rating();
    $rating_count = $product->get_rating_count();
    $review_count = $product->get_review_count();

    if( ! empty($average_rating) && $average_rating > 0 ) {
        return '<div>' . $title . '</div>' . wc_get_rating_html($average_rating) . sprintf(
            '<div class="stars">%s / 5 (%s %s)</div>',
            $average_rating,
            $review_count,
            _n( "review", "reviews", $review_count, "woocommerce" )
        );
    }
    return $title;
}

Code goes in function.php file of your active child theme (active theme). Tested and works.

enter image description here

You can add star rating and average to product "Review" item in WooCommerce with just few clicks.

Rating Score

I recently found TrustFeed reviews for WooCommerce, it is a new plugin that helped me to replace the basic and boring WooCommerce reviews forms with an advanced and dynamic rating and reviews solution that actually inspire shoppers to buy more!

It comes with special features to help honest online businesses to collect and showcase customer reviews to boost sales, conversions, trust and social proof.

Some special features are:

  • Create custom satisfaction surveys
    • Star Ratings
    • Dynamic Tags
    • Text Reviews
    • More coming…
  • Send Mail After Purchase emails to encourage shoppers to leave reviews.
  • Collect reviews at scale.
  • Showcase reviews on key conversion points of your website.
  • Display your reviews in an useful, responsive and customizable format.
  • Moderate and manage feedback/reviews.
  • Create a mini-site to increase your SEO with User Generated Content
  • Customize widget’s look and feel.
  • Learn from data driven insights

The plugin is free and can be dowloaded via official WordPress plugin repository:

https://wordpress.org/plugins/trustfeed-reviews-and-customer-feedback-for-woocommerce/

  • To learn more about TrustFeed you can visit www.trustfeed.co
  • To view the admin panel, just login at https://admin.trustfeed.co and create a free account.
Related