I want to get woocommerce reviews by product id and display it in a template

后端 未结 8 1629
谎友^
谎友^ 2020-12-18 02:04

I want to fetch woocommerce product reviews by product id or anything else and want to display it in a template created by me.

相关标签:
8条回答
  • 2020-12-18 02:45

    I have been recently having the same struggle and I came up with this solution that allows you to output reviews from all products on a single page.

    //Display all product 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 the above function to your functions.php file. After this you can use this function on your theme wherever you would like by calling it like this:

    <?php echo display_all_reviews(); ?>
    

    I also created a tutorial here https://www.majas-lapu-izstrade.lv/get-woocommerce-customer-reviews-from-all-products-display-average-and-all-ratings-in-a-histogram-without-a-plugin/ that also includes functions to output average product rating, total count of all ratings from all products and all product review histogram.

    0 讨论(0)
  • 2020-12-18 02:52

    Woocommerce made a reviews out of regular wordpress comments.

    The simple way is to get a comments of 'post_type' => 'product', that will get raw comment data. In order to display proper woocommerce review out of that you need to apply a callback function witch is 'callback' => 'woocommerce_comments'.

    The whole thing is:

    <?php
        $args = array ('post_type' => 'product');
        $comments = get_comments( $args );
        wp_list_comments( array( 'callback' => 'woocommerce_comments' ), $comments);
    ?>
    

    If you want to get the comment by product ID then you need to change that $args:

    $args = array ('post_id' => 123); 
    

    To customize it more check out that references:

    http://codex.wordpress.org/Function_Reference/wp_list_comments http://codex.wordpress.org/get_comments

    The 'callback' => 'woocommerce_comments' function uses a template located in plugins/woocomerce/templates/single-product/review.php

    0 讨论(0)
提交回复
热议问题