在 WooCommerce 中显示总客户评论和平均评分 [英] Display total customers reviews and ratings average in WooCommerce
问题描述
我想在首页显示顾客评论总数,我试过这种方法:
I want to display a total number of customer reviews on the homepage, I have tried this method:
<?php
$args = array(
'status' => 'approve',
'post_status' => 'publish',
'post_type' => 'product'
);
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
$count = get_comment_count($comments);
?>
<span class="total_reviews">
<?php echo $count['approved'] . ' reviews' ?>
</span>
但是没有像我想要的那样工作!例如,我有 4 条评论,而此代码仅显示 (1 条评论) 而不是 (4 条评论).
But isn't working like I want! for example, I have 4 reviews comments and this code show only (1 reviews) instead of (4 reviews).
关于平均值,我不知道它在主页上是如何工作的,我只知道如何使用下面的代码在单个产品页面上实现它:
About the average, I don't have any idea of how it's work on the homepage, I just know how to implement this on a single product page by using this code below:
$average = $product->get_average_rating();
但是,此代码仅适用于单个产品的平均评分,而不是我希望的所有评论的全球平均评分.
But, this code is only for a single product average ratings, not a global average of all reviews as I would like.
感谢任何帮助.
推荐答案
更新 (在没有评论时避免最后一个函数出错)
您会在下面找到 4 个可以为您提供的自定义函数:
Below you will find 4 custom functions that will give you:
- 产品评论总数
- 产品评分计数数据,这些数据将用于:
- 产品按评级输出 html 计数
- 产品评分平均输出 html
功能代码:
function get_total_reviews_count(){
return get_comments(array(
'status' => 'approve',
'post_status' => 'publish',
'post_type' => 'product',
'count' => true
));
}
function get_products_ratings(){
global $wpdb;
return $wpdb->get_results("
SELECT t.slug, tt.count
FROM {$wpdb->prefix}terms as t
JOIN {$wpdb->prefix}term_taxonomy as tt ON tt.term_id = t.term_id
WHERE t.slug LIKE 'rated-%' AND tt.taxonomy LIKE 'product_visibility'
ORDER BY t.slug
");
}
function products_count_by_rating_html(){
$star = 1;
$html = '';
foreach( get_products_ratings() as $values ){
$star_text = '<strong>'.$star.' '._n('Star', 'Stars', $star, 'woocommerce').'<strong>: ';
$html .= '<li class="'.$values->slug.'">'.$star_text.$values->count.'</li>';
$star++;
}
return '<ul class="products-rating">'.$html.'</ul>';
}
function products_rating_average_html(){
$stars = 1;
$average = 0;
$total_count = 0;
if( sizeof(get_products_ratings()) > 0 ) :
foreach( get_products_ratings() as $values ){
$average += $stars * $values->count;
$total_count += $values->count;
$stars++;
}
return '<p class="rating-average">'.round($average / $total_count, 1).' / 5 '. __('Stars average').'</p>';
else :
return '<p class="rating-average">'. __('No reviews yet', 'woocommerce').'</p>';
endif;
}
代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
用法
客户评论总数:
Total customers reviews:
echo '<p>'.__('Total reviews','woocommerce').': '.get_total_reviews_count().'</p>';
产品按评分列表计数:
The products count by ratings list:
echo products_count_by_rating_html();
产品评分平均值:
The products rating average:
echo products_rating_average_html();
这篇关于在 WooCommerce 中显示总客户评论和平均评分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!