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; ?>