Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Inherit Review Ratings on WooCommerce Variable Products?

As a store owner, you know how important product reviews are for building trust and influencing purchasing decisions.

Let’s say you’ve been selling a few simple products, each with customer reviews:

  • Red T-shirt — 5 ratings  and 2 reviews
  • Blue T-shirt— 4 ratings and 1 review

Later, you combine them into a variable product called “T-shirt” with color variations, Red and Blue.

But here’s the problem.

Even though your old simple products have genuine reviews, your new variable product shows 0 reviews and no rating.

Fortunately, there’s a code snippet that allows your variable products to inherit ratings from grouped simple products, so you don’t lose the credibility your store has earned.

Solution: Inherit Review Ratings on WooCommerce Variable Products

The code aggregates ratings and reviews from older simple products and displays them on the new variable product.

add_filter( 'woocommerce_product_get_rating_counts', 'ts_inherit_ratings', 10, 2 ); 
add_filter( 'woocommerce_product_get_review_count', 'ts_inherit_ratings', 10, 2 );
add_filter( 'woocommerce_product_get_average_rating', 'ts_inherit_ratings', 10, 2 );

function ts_inherit_ratings( $value, $product ) {

    $map = [
        1005 => [195815, 195816], // 1005 = new variable/grouped product ID, 195815 & 195816 = old simple product IDs
    ];

    if ( array_key_exists( $product->get_id(), $map ) ) {

        $old_ids = $map[ $product->get_id() ];

        $rating_count = [];
        $review_count = 0;

        foreach ( $old_ids as $old_id ) {
            $old_product = wc_get_product( $old_id );
            if ( $old_product ) {
                foreach ( $old_product->get_rating_counts() as $rating => $count ) {
                    $rating_count[ $rating ] = ($rating_count[$rating] ?? 0) + $count;
                }
                $review_count += $old_product->get_review_count();
            }
        }

        if ( current_filter() == 'woocommerce_product_get_rating_counts' ) return $rating_count;
        if ( current_filter() == 'woocommerce_product_get_review_count' ) return $review_count;
        if ( current_filter() == 'woocommerce_product_get_average_rating' ) {
            $total = 0; 
            $count = 0;
            foreach ( $rating_count as $rating => $num ) { 
                $total += $rating * $num; 
                $count += $num; 
            }
            return $count ? round( $total / $count, 2 ) : 0;
        }
    }

    return $value;
}

Output

Once you add the code snippet to your site, your variable product will start displaying the combined average rating and total number of reviews inherited from its simple products.

The star rating shown on the variable product will reflect the average rating of all associated simple products defined in the code. The review count will show the sum of all individual reviews from those products.

Beyond ratings, there are other ways to enhance how your variations are presented. For example, adding custom fields to product variations in WooCommerce can provide extra details or options, helping customers make more informed choices.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Leave a Reply

Your email address will not be published. Required fields are marked *

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible.

Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

By using our site, you acknowledge that you have read and understood our Privacy Policy and Terms & Conditions.