2
votes

I am building a custom product page with WYSIWYG tool for WooCommerce. For CRO sake I want to include the reviews towards the bottom of the page.

I already disabled the tabs (reviews, description) and I'll add custom content in the middle and reviews at the bottom.

I already found a way to display reviews (credit: https://www.businessbloomer.com/woocommerce-display-product-reviews-custom-page-shortcode/)

add_shortcode( 'product_reviews', 'bbloomer_product_reviews_shortcode' );
 
function bbloomer_product_reviews_shortcode( $atts ) {
    
   if ( empty( $atts ) ) return '';
 
   if ( ! isset( $atts['id'] ) ) return '';
       
   $comments = get_comments( 'post_id=' . $atts['id'] );
    
   if ( ! $comments ) return '';
    
   $html .= '<div class="woocommerce-tabs"><div id="reviews"><ol class="commentlist">';
    
   foreach ( $comments as $comment ) {   
      $rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
      $html .= '<li class="review">';
      $html .= get_avatar( $comment, '60' );
      $html .= '<div class="comment-text">';
      if ( $rating ) $html .= wc_get_rating_html( $rating );
      $html .= '<p class="meta"><strong class="woocommerce-review__author">';
      $html .= get_comment_author( $comment );
      $html .= '</strong></p>';
      $html .= '<div class="description">';
      $html .= $comment->comment_content;
      $html .= '</div></div>';
      $html .= '</li>';
   }
    
   $html .= '</ol></div></div>';
    
   return $html;
} 

Problem now Now I'd like to edit the shortcode so it's possible to pull /display also the total avg product rating (and possibly a form, but for that I can just like to a different review submission page).

1
Please enter [product_reviews] in the WYSIWYG tool. to call your shortcode. please refer this URL wpbeginner.com/wp-tutorials/how-to-add-a-shortcode-in-wordpressBhautik
Sorry I am not sure I understand your solution. Adding the shortcode is going to do what I am doing already - displaying the reviews for a product by its ID. I'd like to "replicate" or close to it, the woocommerce reviews functionality using a shortcode. Meaning not only a rating (as the code from businessbloomer.com does) but also avg rating etc.DaveyBear
can you share how you use this shortcode in WYSIWYG. you have to pass product id like this [product_reviews id="your product id"]Bhautik
Yes this is how I was using it already, that's why I didn't understand your solution. I had already used the shortcode in that wayDaveyBear

1 Answers

1
votes

for count total average you have to count the total number of reviews and calculate the total number of ratings. then divided ratings/reviews. check my below code

add_shortcode( 'product_reviews', 'product_reviews_shortcode' );

function product_reviews_shortcode( $atts ) {
    
   if ( empty( $atts ) ) return '';
 
   if ( ! isset( $atts['id'] ) ) return '';
       
   $comments = get_comments( 'post_id=' . $atts['id'] );
   
   $total_comments = count( $comments );
   
   if ( ! $comments ) return '';
   
   $total_rating = 0;

   $html .= '<div class="woocommerce-tabs"><div id="reviews"><ol class="commentlist">';
    
   foreach ( $comments as $comment ) {   
      $rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
      $total_rating = $total_rating + $rating;
      $html .= '<li class="review">';
      $html .= get_avatar( $comment, '60' );
      $html .= '<div class="comment-text">';
      if ( $rating ) $html .= wc_get_rating_html( $rating );
      $html .= '<p class="meta"><strong class="woocommerce-review__author">';
      $html .= get_comment_author( $comment );
      $html .= '</strong></p>';
      $html .= '<div class="description">';
      $html .= $comment->comment_content;
      $html .= '</div></div>';
      $html .= '</li>';
   }
    
   $html .= '</ol></div></div>';
    
    if( $total_rating > 0 ) {
       $total_average = $total_rating / $total_comments;
       $total_average =  number_format($total_average, 2, '.', '');
    }

   return $html;
}