建立所有已提交的WooCommerce评论的列表

时间:2018-10-18 15:16:27

标签: woocommerce

我试图通过使用查询创建所有已批准的WooCommerce评论的列表,并且试图通过使用简码在页面上显示该列表。

主要目的是显示客户名称,电子邮件和提交评论的日期。这是代码,以及到目前为止我已经尝试过的内容:

SELECT * from dbo.TABLE
left join table_insp insp on TABLE.FORM_NO =  insp.FORM_no
where (
        (O_NMBR is not null) 
        OR (O_DRCTN is not null) 
        or (O_INCHES is not null) 
        or (o_comments is not null)
      )
  and insp.COMPLETED_DATE >= '10-01-2018' 

使用command.ExecuteNonQuery();时没有任何表情。如果有人可以提供帮助,我将非常感激。

2 个答案:

答案 0 :(得分:0)

首先在函数中,您只是查询数据库而没有循环搜索结果并打印结果,其次,Wordpress中已经内置了函数,它将帮助您从数据库中获取注释,而无需编写自定义查询,这总是很明智的如果可能,选择遵循WordPress标准。

因此要使用get_comments()函数获取注释,您的代码应如下所示:

function list_reviews()
{
    $args = array(
        'post_type' => 'product', //Post type 
        'status' => "approve", // Status you can also use 'hold', 'spam', 'trash', 


    );
    $comments = get_comments($args);

    foreach ($comments as $comment) {
        echo "Customer Name " . $comment->comment_author . " Email: " . $comment->comment_author_email . " Date " . $comment->comment_date . "<br>";
    }


}
add_shortcode('allreviews', 'list_reviews');

有关get_comments()函数的更多信息,您可以阅读以下WordPress Codex

已编辑:

要在表中打印信息,您的代码应如下所示:

function list_reviews()
{
    $args = array(
        'post_type' => 'product', //Post type 
        'status' => "approve", // Status you can also use 'hold', 'spam', 'trash', 
    );
    $comments = get_comments($args);
    ?>
<table>
    <tr>
        <th>Customer Name</th>
        <th>Email</th>
        <th>Date</th>
    </tr>
    <?php
foreach ($comments as $comment) {

    ?>
    <tr>
        <td>
            <?php echo $comment->comment_author ?>
        </td>
        <td>
            <?php echo $comment->comment_author_email ?>
        </td>
        <td>
            <?php echo $comment->comment_date ?>
        </td>
    </tr>
    <?php

}
?>
</table>
<?php

}
add_shortcode('allreviews', 'list_reviews');

答案 1 :(得分:0)

我猜一个星期都遇到了完全相同的问题,并设法找到一种解决方案,该解决方案可以在单个页面上输出所有产品评论。

//Display all reviews
if (!function_exists('display_all_reviews')) {
function display_all_reviews(){
    $args = array(
       'status' => 'approve',
       'type' => 'review'
    );

    // The Query
    $comments_query = new WP_Comment_Query;
    $comments = $comments_query->query( $args );

    // Comment Loop
    if ( $comments ) {
        echo "<ol>";
        foreach ( $comments as $comment ): ?>
        <?php if ( $comment->comment_approved == '0' ) : ?>
            <p class="meta waiting-approval-info">
                <em><?php _e( 'Thanks, your review is awaiting approval', 'woocommerce' ); ?></em>
            </p>
            <?php endif;  ?>
            <li itemprop="reviews" itemscope itemtype="http://schema.org/Review" <?php comment_class(); ?> id="li-review-<?php echo $comment->comment_ID; ?>">
                <div id="review-<?php echo $comment->comment_ID; ?>" class="review_container">
                    <div class="review-avatar">
                        <?php echo get_avatar( $comment->comment_author_email, $size = '50' ); ?>
                    </div>
                    <div class="review-author">
                        <div class="review-author-name" itemprop="author"><?php echo $comment->comment_author; ?></div>
                        <div class='star-rating-container'>
                            <div itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating" class="star-rating" title="<?php echo esc_attr( get_comment_meta( $comment->comment_ID, 'rating', true ) ); ?>">
                                <span style="width:<?php echo get_comment_meta( $comment->comment_ID, 'rating', true )*22; ?>px"><span itemprop="ratingValue"><?php echo get_comment_meta( $comment->comment_ID, 'rating', true ); ?></span> <?php _e('out of 5', 'woocommerce'); ?></span>

                                    <?php
                                        $timestamp = strtotime( $comment->comment_date ); //Changing comment time to timestamp
                                        $date = date('F d, Y', $timestamp);
                                    ?>
                            </div>
                            <em class="review-date">
                                <time itemprop="datePublished" datetime="<?php echo $comment->comment_date; ?>"><?php echo $date; ?></time>
                            </em>
                        </div>
                    </div>
                    <div class="clear"></div>
                    <div class="review-text">
                        <div itemprop="description" class="description">
                            <?php echo $comment->comment_content; ?>
                        </div>
                        <div class="clear"></div>
                    </div>
                <div class="clear"></div>           
            </div>
        </li>

        <?php 
        endforeach;
        echo "</ol>";
    } else {
        echo "This product hasn't been rated yet.";
    }
}
}

add_shortcode('allreviews', 'display_all_reviews');

上面的功能还包括评论本身的输出。 我还设法创建了两个附加功能,例如获取和显示所有产品的平均评论评分以及显示所有产品评分的直方图。我在此处编写了带有演示链接的教程:How to Get WooCommerce customer reviews from all products, display average and all ratings in a histogram without a plugin