user10524745
user10524745

Reputation:

Building a List of all Submitted WooCommerce Reviews

I am trying to create a list of all approved WooCommerce reviews by using a query and I am trying to display that list on a page by using a shortcode.

The main point with this is to show the customer name, email and the date of when the review was submitted. Here is the code and what I've tried so far:

function list_reviews() {  
    $customer_details_from_reviews = $wpdb->get_results("SELECT wpc.comment_author,wpc.comment_author_email,wpc.comment_date,wpc.comment_content,wpcm.meta_value AS rating FROM `" . $wpdb->prefix . "comments` AS wpc INNER JOIN  `" . $wpdb->prefix . "commentmeta` AS wpcm ON wpcm.comment_id = wpc.comment_id AND wpcm.meta_key = 'rating' WHERE wpc.comment_post_id = '" . $p_id . "' ");
}
add_shortcode('allreviews', 'list_reviews');

Nothing shows on the pate when using [allreviews]. If anyone can help with this I would really appreciate it.

Upvotes: 1

Views: 1680

Answers (2)

John McClane
John McClane

Reputation: 187

Was stuck with the exact same issue for a week I guess and managed to cme up with a solution that will output all product reviews on a single page.

//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');

The function above also includes the output of reviews themselves. And I also managed to create a couple of additional functions like getting and displaying average review rating from all products as well as displaying a histogram of all product ratings. I wrote a tutorial with a demo link here: How to Get WooCommerce customer reviews from all products, display average and all ratings in a histogram without a plugin

Upvotes: 0

kashalo
kashalo

Reputation: 3562

First in your function you are just query the database without looping through the results and printing them, second there is already built in function in Wordpress which will help you to get the comment from the database without writing custom query which is always wise choice to follow WordPress Standard if possible .

so to get the comments using get_comments() function your code should look like this:

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');

for more information about the get_comments() function you can read the following WordPress Codex

Edited:

to print the information in Table your code should look like this:

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');

Upvotes: 1

Related Questions