1

I'm trying to generate a shortcode that gives me the option to display the reviews/ratings of a WooCommerce product anywhere on the single product page.

So far, the reviews are displayed by default in the reviews tab. I have already deactivated this tab. So I want to list the reviews further down below the product description where I add the shortcode.

Important: I don't want to enter the id of the product manually in the short code, it should be automatically dragged from the current showing product.

Here is my code:

add_shortcode( 'product_reviews', 'display_product_reviews' );
function display_product_reviews( $atts ){
    $atts = shortcode_atts( array(
        'id' => get_the_id(),
    ), $atts, 'product_reviews' );

    global $product;

    if ( ! is_a( $product, 'WC_Product') )
        $product = wc_get_product($atts['id']);

    $comments = get_comments( 'post_id=' . $atts['id'] );
    
   if ( ! $comments ) return '';
    
   $html .= '<div class="abc"><div id="reviews"><ol class="list">';
    
   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;
}

On single product page, I add this shortcode: [product_reviews]

Unfortunately, something is not working here...

I tried different code variations without success.

1
  • I have tested it and its working. Do you have any reviews ? Commented May 28, 2024 at 10:50

1 Answer 1

1

You can try to use the following improved code version of your shortcode:

add_shortcode( 'product_reviews', 'display_product_reviews' );
function display_product_reviews( $atts ){
    $atts = shortcode_atts( array(
        'id' => get_the_id(),
    ), $atts, 'product_reviews' );

    global $product, $post;

    if ( ! is_a( $product, 'WC_Product') ) {
        $product = wc_get_product($atts['id']);
        $post    = get_post($atts['id']);
    }
    
    ob_start(); // start buffering

    comments_template();
    
    return ob_get_clean(); // Return buffered content
}

Code goes in function.php file of your child theme (or in a plugin). Tested and works.


Important note:

If you are using a block theme and If you use single product Blocks, maybe you should use the product reviews Block, instead of a custom shortcode.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you very much. I add your code to functions.php via WP-Code. I activate the Snippet and put the following Short-Code into the single product template: [product_reviews] Save it but it seems not to work. At the top of the product page I see the summary: 1 Rating (5 stars) But I don't see the review it self with the text at the point, where I add the shortcode. What did I do wrong? Many thanks, Lars
I think comments_template() is only working for posts, not for products, isn't it? But I'm not really sure ... sorry
Note that comments_template() is used by WooCommerce itself to display the product reviews... see github.com/woocommerce/woocommerce/blob/release/9.0/plugins/… … I tested my code on Storefront theme and the shortcode works displaying the same content as the "reviews" product tab.
Ok thanks. I use the SwiftStore blocks theme wordpress.org/themes/swiftstore. Is there a way how I can debug it? Other shortcodes on same page/template works. Many thanks for your help.
If you are using a block theme and If you use single product Blocks, maybe you should use the product reviews Block, instead of a custom shortcode.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.