Additional WooCommerce Reviews dynamic data tags

Hi there,

I’ve been working on a design that I needed these for but since some people showed interest in this I’ll share the code to the function here.

Here’s the design I’ve tried to achieve:

Here’s the function:

function get_product_data() {
    static $product_data = null;
    if ($product_data === null) {
        $product_id = get_the_ID();
        $product = wc_get_product($product_id);
        if (!$product) return null;
        $product_data = [
            'product' => $product,
            'rating_counts' => $product->get_rating_counts(),
            'total_reviews' => array_sum($product->get_rating_counts()),
            'average_rating' => $product->get_average_rating()
        ];
    }
    return $product_data;
}

function get_product_review_percentage($star) {
    $data = get_product_data();
    if (!$data || $data['total_reviews'] === 0) return 0;
    $star_count = isset($data['rating_counts'][$star]) ? $data['rating_counts'][$star] : 0;
    return round(($star_count / $data['total_reviews']) * 100, 2);
}

function get_product_review_amount($star) {
    $data = get_product_data();
    if (!$data) return 0;
    return isset($data['rating_counts'][$star]) ? $data['rating_counts'][$star] : 0;
}

function get_total_product_reviews() {
    $data = get_product_data();
    return $data ? $data['total_reviews'] : 0;
}

function get_product_average_rating() {
    $data = get_product_data();
    return $data ? round($data['average_rating'], 1) : 0;
}

function get_product_average_rating_percentage() {
    $data = get_product_data();
    return $data ? round(($data['average_rating'] / 5) * 100, 2) : 0;
}

// Register the functions with Bricks
add_filter('bricks/dynamic_data/register_functions', function($functions) {
    $new_functions = [
        'get_product_review_percentage' => 'Get the percentage of reviews for a specific star rating',
        'get_product_review_amount' => 'Get the number of reviews for a specific star rating',
        'get_total_product_reviews' => 'Get the total number of reviews for the product',
        'get_product_average_rating' => 'Get the average rating for the product',
        'get_product_average_rating_percentage' => 'Get the average rating for the product as a percentage'
    ];

    foreach ($new_functions as $name => $description) {
        $functions[$name] = [
            'name' => $name,
            'label' => ucwords(str_replace('_', ' ', $name)),
            'description' => $description,
            'callback' => $name,
        ];
    }
    return $functions;
});

add_filter( 'bricks/code/echo_function_names', function() {
  return [
        'get_product_review_percentage',
        'get_product_review_amount',
        'get_total_product_reviews',
	  	'get_product_average_rating',
	  	'get_product_average_rating_percentage'
  ];
} );

Here’s the dynamic data tags and what they do:

  • {echo:get_product_review_percentage(STAR)} returns the amount of that star in percentage. You can use this in the progress bar so that you
  • {echo:get_product_review_amount(STAR)} returns the amount of reviews with that star
  • {echo:get_total_product_reviews} returns the total amount of reviews as a numeric value
  • {echo:get_product_average_rating} returns the average rating (1 decimal)
  • {echo:get_product_average_rating_percentage} the average rating as percentage, 4 = 80%. You can use this for the pie chart to get the cirle.

Instead of “STAR” you’d enter a numeric value like 5 to get the data for that star. Possible values are 1 to 5.

EDIT: Optimized the code by a LOT.

Hopefully this’ll help someone in the future!
Cheers

6 Likes

Thanks for sharing.

Please prefix the function names with your initials or something. Ideally, there should be a distinction between core functions and custom functions.

4 Likes

Thanks for sharing — will sure try it out !

Hi Sridhar, will change this up!
Thanks