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

后端 未结 8 1666
谎友^
谎友^ 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 "
      "; foreach ( $comments as $comment ): ?> comment_approved == '0' ) : ?>

    1. id="li-review-comment_ID; ?>">
      comment_author_email, $size = '50' ); ?>
      comment_ID, 'rating', true ); ?> comment_date ); //Changing comment time to timestamp $date = date('F d, Y', $timestamp); ?>
      comment_content; ?>
    2. "; } 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:

    
    

    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.

提交回复
热议问题