Search code examples
phpwoocommerceproductreviewmaxlength

Display some WooCommerce product reviews with limited content length randomly


I am making shortcode for reviews Based on WooCommerce: Display some reviews randomly on home page to put on other pages. How can i limit reviews by character numbers, like 100 or 200 characters. SO they will not be too long and fit any page.

I am using following code for reviews now

function get_random_five_stars_products_reviews( $atts ) {
    // Extract shortcode attributes
    extract( shortcode_atts( array(
        'limit' => 5, // number of reviews to be displayed by default
    ), $atts, 'woo_reviews' ) );

    $comments = get_comments( array(
        'status'      => 'approve',
        'post_status' => 'publish',
        'post_type'   => 'product',
        'meta_query'  => array( array(
            'key'     => 'rating',
            'value'   => '5',
        ) ),
    ) );

    shuffle($comments);

    $comments = array_slice( $comments, 0, $limit );

    $html = '<ul class="products-reviews">';
    foreach( $comments as $comment ) {
        $rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
        $html .= '<li class="review">';
        $html .= '<div>'.get_the_title( $comment->comment_post_ID ).'</div>';
        if ( $rating > 4 ) $html .= wc_get_rating_html( $rating );
        $html .= '<div>' .$comment->comment_content.'</div>';
        $html .= "<div>Posted By :".$comment->comment_author." On ".$comment->comment_date. "</div>";
        $html .= '</li>';
    }
    return $html . '</ul>';
}
add_shortcode('woo_reviews', 'get_random_five_stars_products_reviews');

Solution

  • To limit displayed 5 starts reviews with a content that doesn't exceed a specific length (here 100 characters) use the following:

    function get_random_five_stars_products_reviews( $atts ) {
        // Extract shortcode attributes
        extract( shortcode_atts( array(
            'limit'      => 5, // number of reviews to be displayed by default
            'length'     => 100, // Limit comments text content lenght
            'show_stars' => true, // Or "false" to hide
        ), $atts, 'woo_reviews' ) );
    
        $comments = get_comments( array(
            'status'      => 'approve',
            'post_status' => 'publish',
            'post_type'   => 'product',
            'meta_query'  => array( array(
                'key'     => 'rating',
                'value'   => '5',
            ) ),
        ) );
    
        shuffle($comments);
    
        $count = 0; // Initializing
        $html  = '<ul class="products-reviews">';
        
        foreach( $comments as $comment ) {
            $content = $comment->comment_content;
            if( strlen($content) <= $length && $count < $limit ) {
                $rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
                $html .= '<li class="review">';
                $html .= '<div>'.get_the_title( $comment->comment_post_ID ).'</div>';
                if ( $show_stars ) {
                    $html .= wc_get_rating_html( $rating );
                }
                $html .= '<div>' .$content. '</div>';
                $html .= "<div>Posted By :".$comment->comment_author." On ".$comment->comment_date. "</div>";
                $html .= '</li>';
                $count++; // Increase count
            }
    
            if ( $count == $limit ) {
                break; // Stop the loop
            }
        }
        return $html . '</ul>';
    }
    add_shortcode('woo_reviews', 'get_random_five_stars_products_reviews');
    

    Code goes in functions.php file of the active child theme (or active theme). It should works.