Change number of Reviews on WooCommerce products

Since version 2.7.0 it’s possible to add reviews to the WooCommerce product snippets. As per default the plugin only integrates 5 reviews into the snippet. Here is how you can extend this number.

Why only five reviews?

5 is just a random number. I could have chosen 10 or even more. However the thing is that there must be a limitation as it might be that some web-shop owners have way more reviews.

Let’s say you have thousands of reviews. It doesn’t make much sense to integrate them all. For two reasons:

  1. It could make your product page load slower because it takes some time go load 1000 reviews from the database. Of course every snippet is cached but in some cases it could be slow on the first page impression.
  2. Your product page loads slow because there is lot of snippet data on the page (that is not visible to the end user but readable by search engines). It doesn’t matter if a user is on a fast Internet connection. However it could slow down page load on mobile devices. Especially if there is a slow internet connection.

What if I want more?

Of course there is a hook for this. This means you can change not only the number of reviews you want to integrate, you can also change what reviews are loaded.

Here is an example. The code below is a custom WordPress plugin that changes what comments are loaded. You can download the plugin here. Don’t forget to upload it to your WordPress installation via FTP and activate it from the plugins menu.

If you want to load 20 instead of 5 reviews (which are basically just comments), a custom plugin could look like this:

<?php
/*
Plugin Name: snip - WooCommerce review args
Plugin URI: https://rich-snippets.io/woocommerce-review-args-hook/
Description: Changes arguments on WooCommerce reviews
Version: 0.1.0
License: License: GPLv2 or later
*/

if ( ! defined( 'ABSPATH' ) ) {
	exit;
} // Exit if accessed directly

add_filter( 'wpbuddy/rich_snippets/woocommerce/reviews/args', function ( $args ) {

	/**
	 * To load 20 instead of 5 reviews for WooCommerce
	 */
	$args['number'] = 20;

	return $args;
} );

What if I want to sort order?

Basically you can change nearly everything with the example above. Take a look at the WP_Comment_Query documentation to learn more what query options are available.

Here is an example that changes the order from descending to ascending. Meaning that the reviews will load the oldest reviews first:

<?php
/*
Plugin Name: snip - WooCommerce review args
Plugin URI: https://rich-snippets.io/woocommerce-review-args-hook/
Description: Changes arguments on WooCommerce reviews
Version: 0.1.0
License: License: GPLv2 or later
*/

if ( ! defined( 'ABSPATH' ) ) {
	exit;
} // Exit if accessed directly

add_filter( 'wpbuddy/rich_snippets/woocommerce/reviews/args', function ( $args ) {

	$args['order'] = 'ASC';

	return $args;
} );