Php 在自定义报告中获取产品评论平均评级

Php 在自定义报告中获取产品评论平均评级,php,wordpress,woocommerce,product,hook-woocommerce,Php,Wordpress,Woocommerce,Product,Hook Woocommerce,在Woocommerce中,我试图以如下方式显示每个产品的报告(HTML表): 基于“”回答代码,通过以下方式获得产品平均评级: echo products_rating_average_html(); 如何分别获得每个产品的平均评分…例如,如果该产品有4或5个评论,则会获得它们的平均评分(每个产品) 我还想获得每个产品的评论评论,并将其放入表中…这将给出给定产品id的平均评级 $rating = get_post_meta( $product_id, '_wc_average_rating

在Woocommerce中,我试图以如下方式显示每个产品的报告(HTML表):

基于“”回答代码,通过以下方式获得产品平均评级:

echo products_rating_average_html();
如何分别获得每个产品的平均评分…例如,如果该产品有4或5个评论,则会获得它们的平均评分(每个产品)


我还想获得每个产品的评论评论,并将其放入表中…

这将给出给定产品id的平均评级

$rating = get_post_meta( $product_id, '_wc_average_rating', true );
你可以在产品的循环中使用它,这将给你一个原始的平均等级,而不需要任何html

要获取最新的注释,您可以使用下面的命令在functions.php中添加此代码,然后在希望显示最新注释的位置调用此操作,并将当前产品对象作为参数传递

function display_product_review($product_id) {

    $product = wc_get_product( $product_id );
    $comments = get_approved_comments( $product->id );

    $product_link = '/product/' . $product->post->post_name . "/#tab-reviews/";

    if ( !empty ($comments) ) {
        echo $comments[0]->comment_content . '<br><a href="'. $product_link . '">Read more reviews...</a>';
    } else {
        echo "There are no reviews for this product yet. Would you like to <a href='" . $product_link ."'>add your own review</a>?";       
    }
}

add_action('get_latest_review','display_product_review');
看看,您将看到您可以使用
WC\u Product
方法,如:

  • get\u average\u rating()
  • 获取评分计数()
下面是一个完整的示例:

1) 获取产品所需的相关数据(可自定义):

2) 表格中的可自定义显示:

echo '<table class="products-reviews-ratings"><tr>
    <th>'.__("ID").'</th>
    <th>'.__("Name").'</th>
    <th>'.__("Rating").'</th>
    <th>'.__("count").'</th>
    <th style="text-align:center">'.__("Reviews (author, date and content)").'</th>
</tr>';

foreach ($products_data as $product_id => $data){
    echo '<tr>
        <td>'.$product_id.'</td>
        <td>'.$data['name'].'</td>
        <td>'.$data['rating'].'</td>
        <td>'.$data['count'].'</td>
        <td style="text-align:center">';

    if( isset($data['reviews']) && $data['reviews'] > 0 ) {
        echo '<table>';

        // Loop through reviews
        foreach ($data['reviews'] as $review ){
            echo '<tr>
                <td><a href="mailto:'.$review->email.'">'.$review->author.'</a></td>
                <td>'.date( 'Y-m-d', $review->date ).'</td>
                <td>'.$review->content.'</td>
            </tr>';
        }
        echo '</table>';
    } else {
        _e('No reviews yet');
    }

    echo '</td></tr>';
}
echo '</table>';
echo'
“.”ID“.”
“。”名称“)”
“评级”
“计数”
“评论(作者、日期和内容)”。”
';
foreach($products\U data作为$product\U id=>$data){
回声'
“.$product_id”
“.$data['name']”
“.$data['rating']”
“.$data['count']”
';
如果(isset($data['reviews'])&&$data['reviews']>0){
回声';
//循环浏览评论
foreach($data['reviews']作为$review){
回声'
“.date('Y-m-d',$review->date)。”
“.$review->content”
';
}
回声';
}否则{
_e(“尚未进行审查”);
}
回声';
}
回声';
您将得到如下结果:


评论如何?如何获得它们你好抱歉我是新的你所说的产品对象是什么意思?我想获得我在网站上拥有的所有产品的数据如何实现这一点?嗨,我已经更新了答案,现在你必须只通过产品id。我通过产品id,如
do\u action('get\u latest\u review',145)产品/椰奶-zumra-17-18-fat-400-ml/#评论-70我仍然知道该产品还没有评论。是否要添加自己的审阅?
函数显示\u product\u审阅($product\u id){$product=wc\u get\u product($product\u id);echo“sdfsdfsfsd”。$product;}do\u操作('get\u latest\u review',145)什么都不回哇,老兄,我说不出话来了非常感谢你一直帮助我,我放弃了非常感谢你的完美回答
$products_data = []; // Initializing

$products = wc_get_products( ['limit' => -1] ); // Get all WC_Product objects

// Loop through products -- Preparing data to be displayed
foreach ( $products as $product ) {
    $product_id = $product->get_id();

    // 1. Product data as product name …
    $products_data[$product_id]['name'] = $product->get_name();


    // 2. Average rating and rating count
    $products_data[$product_id]['rating'] = (float) $product->get_average_rating();
    $products_data[$product_id]['count']  = (int) $product->get_rating_count();

    // 3. Reviews -- Loop though product reviews
    foreach( get_approved_comments( $product_id ) as $review ) {
        if( $review->comment_type === 'review' ) {
            $products_data[$product_id]['reviews'][] = (object) [
                'author'    => $review->comment_author,
                'email'     => $review->comment_author_email,
                'date'      => strtotime($review->comment_date),
                'content'   => $review->comment_content,
            ];
        }
    }
}
echo '<table class="products-reviews-ratings"><tr>
    <th>'.__("ID").'</th>
    <th>'.__("Name").'</th>
    <th>'.__("Rating").'</th>
    <th>'.__("count").'</th>
    <th style="text-align:center">'.__("Reviews (author, date and content)").'</th>
</tr>';

foreach ($products_data as $product_id => $data){
    echo '<tr>
        <td>'.$product_id.'</td>
        <td>'.$data['name'].'</td>
        <td>'.$data['rating'].'</td>
        <td>'.$data['count'].'</td>
        <td style="text-align:center">';

    if( isset($data['reviews']) && $data['reviews'] > 0 ) {
        echo '<table>';

        // Loop through reviews
        foreach ($data['reviews'] as $review ){
            echo '<tr>
                <td><a href="mailto:'.$review->email.'">'.$review->author.'</a></td>
                <td>'.date( 'Y-m-d', $review->date ).'</td>
                <td>'.$review->content.'</td>
            </tr>';
        }
        echo '</table>';
    } else {
        _e('No reviews yet');
    }

    echo '</td></tr>';
}
echo '</table>';