WooCommerce 是一个功能强大的电子商务平台,它提供一个强大的评论系统,帮助企业建立信任和信誉。
虽然 WooCommerce 提供了默认的评论显示方式,但您可能希望对其进行自定义,以满足您的特定需求。本教程将指导您编辑单个产品页面上的 “x 客户评论 ”文本。
按照这些步骤,您可以定制产品评论显示,使其符合您的品牌美学,并为客户提供更具吸引力的购物体验。

PHP 代码段 1:更改 WooCommerce 单个产品页面中的 “x 客户评论 ”文本
此解决方案适用于只需将其重命名为其他内容,而无需添加平均评分等额外内容的用户。基本上,此解决方案将简单地 “翻译 ”此文本字符串:
_n( '%s customer review', '%s customer reviews', $review_count, 'woocommerce' )
_n() 是一个 WordPress 函数,用于国际化(i18n)。它根据给定的数字(即本例中 WooCommerce 产品评论的数量)选择字符串的单数或复数形式。
如果您想过滤 _n() 函数的输出,可以使用 WordPress 中的 gettext 过滤器。下面是您可以采用的方法:
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 代码段 2:在 WooCommerce 单个产品页面上用平均评分更改 “x 客户评论 ”文本
此解决方案要求我们访问 $product 全局以获取产品数据(当前产品的平均评分)。我的目标是移除 “x 客户评论 ”文本,并使用 “Rated 5 out of 5 ”来代替。
遗憾的是,据我所知,上述解决方案无法让我们访问 $post 或 $product 变量,因此我们需要覆盖 WooCommerce 中的 “single-product/rating.php ”模板文件。
但我不想在我的子主题中添加另一个文件,所以我会给你一个不同的解决方法–我们基本上是覆盖名为 “woocommerce_template_single_rating ”的可插拔函数!
因为它是可插入的,所以我们可以简单地重新定义它,然后告诉 WooCommerce 遵循我们的规则:
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>'; } }
顺便提一下,woocommerce_template_single_rating()函数包含的原始模板(‘single-product/rating.php’)包含这段代码:
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; ?