Month: July 2024

  • WooCommerce Show Reviews in Separate Standalone Page

    WooCommerce Show Reviews in Separate Standalone Page

    Hi Folks, it is me again. Sometimes we want to show all reviews (perhaps only 5 ⭐⭐⭐⭐⭐) in a separate page for our end users. Obviously to make our customers we have many great 5 star reviews.

    I could not find a perfect solution for this. So, I made a quick shortcode function to acheive this.

    Lets start, shall we?

    Following codes can be put into your child theme > functions.php file

    First we create our shortcode

    add_shortcode( 'woo_reviews', 'show_reviews' );

    Then we define our shortcode function

    function show_reviews( $atts ) {
    
    $atts = shortcode_atts( array(
    'rating' => '',
    'product' => '',
    ), $atts, 'woo_reviews' );
    
    // set up meta query to filter by rating if specified
    $meta_query = array();
    if ( $atts['rating'] ) {
    $meta_query[] = array(
    'key' => 'rating',
    'value' => $atts['rating'],
    'compare' => '='
    );
    }
    
    $comments_query = new WP_Comment_Query( array(
    'post_type' => array( 'product' ),
    'post__in' => $atts['product'],
    'meta_query' => $meta_query,
    ) );
    
    // Output buffer to capture wp_list_comments
    ob_start();
    
    if ( ! empty( $comments_query->comments ) ) {
    echo '<div class="woocommerce">';
    echo '<div id="reviews">';
    echo '<div id="comments">';
    echo '<ol class="commentlist">';
    wp_list_comments( apply_filters( 'woocommerce_product_review_list_args', array( 'callback' => 'woocommerce_comments' ) ), $comments_query->comments );
    echo '</ol>';
    echo '</div>';
    echo '</div>';
    echo '</div>';
    } else {
    echo '<p>No reviews found.</p>';
    }
    
    return ob_get_clean();
    }

    Oh by the way you can do following things as well

    [woo_reviews rating="5"]
    [woo_reviews product="33,34"]