显示WooCommerce中客户的评论总数和平均评分 [英] Display total customers reviews and ratings average in WooCommerce

查看:373
本文介绍了显示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:

  1. 总商品评论数
  2. 产品评分计数数据,这些数据将用于:
    • 产品通过对html输出的评分进行计数
    • 产品评分平均产量html
  1. The total products reviews count
  2. The products ratings count data, that will be used in:
    • The products count by rating output html
    • The products rating average output 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.

用法

  1. 总客户评论:

  1. 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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆