How to Get aggregateRating Schema / Google Structured Data for Woocommerce Reviews

If you’re theme is not utilizing Woocommerce’s built in Schema functionality, or you are seeing an error in Search Console or Google’s Structured Data Testing Tools that you need to have aggregateRating when you have one or more reviews, here’s what you need to fix this:

<?php $rating = get_post_meta( get_the_ID(), '_wc_average_rating', true );
if (!empty($rating) && $rating >= 1) {
$review_count = $product->get_review_count(); ?>
<div itemprop="aggregateRating" itemscope itemtype="">
<meta itemprop="ratingValue" content="<?php echo $rating; ?>">
<meta itemprop="reviewCount" content="<?php echo $review_count; ?>">

<?php } ?>

= 1) {

. I put mine immediately following where that div opens.

To explain the code…

First, with $rating we simply grab the meta data for the product. This is stored by Woocommerce in the wp_postmeta database field after the first review is left for the product, and updated as more reviews come in. It’s the average rating value of all the reviews.

Then we create a container, i.e. that <div itemprop... code. This states that this information pertains to aggregateRating.

The next to meta fields set the required information. We show the $rating here, which again is the average value of all reviews / ratings.

Then we get the number of reviews on the next line (reviews are just default WP comments with a 1 – 5 star rating scale for Woocommerce.)

Hope that is useful!

Up Next: How to Get Memberful to Work with Cached WordPress Sites