WooCommerce: Edit “x customer reviews” String @ Single Product Page

WooCommerce is a powerful ecommerce platform that offers a robust review system to help businesses build trust and credibility.

While WooCommerce provides a default review display, you may want to customize it to fit your specific needs. This tutorial will guide you through the process of editing the “x customer reviews” text on a single product page.

By following these steps, you can tailor your product review display to match your brand’s aesthetic and provide a more engaging shopping experience for your customers.

PHP Snippet 1: Change “x customer reviews” Text @ WooCommerce Single Product Page

This solution is for those who only need to rename it to something else, without adding extra content such as the average rating etc. Basically, this solution will simply “translate” this text string:

_n( '%s customer review', '%s customer reviews', $review_count, 'woocommerce' )

_n() is a WordPress function used for internationalization (i18n). It selects the singular or plural form of a string based on a given number, i.e. the number of WooCommerce product reviews in this case.

If you want to filter the output of the _n() function, you can use the gettext filter in WordPress. Here’s how you can approach it:

/**
 * @snippet       Translate/Rename "x customer reviews" Woo String
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 9
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'gettext', 'bbloomer_rename_customer_reviews', 9999, 3 );

function bbloomer_rename_customer_reviews( $translation, $text, $domain ) {

    if ( $text === '%s customer review' ) {
        return '%s Genuine Product Review';
    }

    if ( $text === '%s customer reviews' ) {
        return '%s Genuine Product Reviews';
    }

    return $translation;

}, 10, 3);

PHP Snippet 2: Change “x customer reviews” Text With Average Rating @ WooCommerce Single Product Page

This solution requires we get access to the $product global in order to get product data (the average rating for the current product). My goal is to remove the “x customer reviews” text, and use “Rated 5 out of 5” instead.

Unfortunately the solution above doesn’t let us access the $post or $product variable as long as I know, so we need to override the ‘single-product/rating.php‘ template file within WooCommerce.

But I don’t want to add yet another file to my child theme, so I’ll give you a different workaround – we basically override the pluggable function called ‘woocommerce_template_single_rating‘!

Because it’s pluggable, we can simply redefine it and tell WooCommerce to follow our rules:

/**
 * @snippet       Override woocommerce_template_single_rating()
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 9
 * @community     https://businessbloomer.com/club/
 */

function woocommerce_template_single_rating() {
   global $product;
	$rating_count = $product->get_rating_count();
	$review_count = $product->get_review_count();
	$average = $product->get_average_rating();
	if ( $review_count > 0 ) {
		echo '<div class="woocommerce-product-rating">';
		echo wc_get_rating_html( $average, $rating_count );
		echo '<a href="#reviews" class="woocommerce-review-link" rel="nofollow"><span>' . sprintf( __( 'Rated %s out of 5', 'woocommerce' ), $average ) . '</span></a>';
		echo '</div>';
	}
}

FYI, the original template (‘single-product/rating.php‘), which is included by the woocommerce_template_single_rating() function, contains this code:

global $product;

if ( ! wc_review_ratings_enabled() ) {
	return;
}

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

if ( $rating_count > 0 ) : ?>

	<div class="woocommerce-product-rating">
		<?php echo wc_get_rating_html( $average, $rating_count ); // WPCS: XSS ok. ?>
		<?php if ( comments_open() ) : ?>
			<?php //phpcs:disable ?>
			<a href="#reviews" class="woocommerce-review-link" rel="nofollow">(<?php printf( _n( '%s customer review', '%s customer reviews', $review_count, 'woocommerce' ), '<span class="count">' . esc_html( $review_count ) . '</span>' ); ?>)</a>
			<?php // phpcs:enable ?>
		<?php endif ?>
	</div>

<?php endif; ?>

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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