rapture89
rapture89

Reputation: 135

Show a list of product reviews for current user - Woocommerce

I've figured out in woocommerce, product reviews are stored within comments. So with that in mind I've added the following to my custom template for account within woocommerce.

<?php

$recent_comments = get_comments( array(
 'number'    => 2,
 'status'    => 'approve'
 ) );
 echo '<ul>';
 foreach($recent_comments as $c){
$the_comment = mb_strimwidth($c->comment_content, 0, 80, "...", "UTF-8");
echo '<li>';
    echo '<p>'.$the_comment.'</p>';
    echo '<span class="comment-source">'.$c->comment_author.'</span>';
    echo '<span class="time-ago">'.$c->comment_date_gmt.'</span>';
    $permalink = get_permalink( $c->comment_post_ID );
    echo '<a href="'.$permalink.'" class="post-link">'.$permalink.'</a>';
   echo '<l/i>';
  }
  echo '</ul>';
 ?>

 </div>

Which returns the comment content, source, author, post id, and permalink. I've checked the database and all these values match.

However I'm trying to take this one step further and show the product thumbnail and and product name.

How would I achieve this?

Upvotes: 2

Views: 2163

Answers (2)

Amit Kumar PRO
Amit Kumar PRO

Reputation: 1240

You also need to pass user_id to get comments from the current user

<?php
    $user_id = get_current_user_id();
    $recent_comments = get_comments( array(
    'number'    => -1,
    'status'    => 'approve',
    'user_id' => $user_id
    ) );

    echo '<ul>';
    foreach($recent_comments as $recent_comment):
      echo "<li>";
      echo wp_get_attachment_url( get_post_thumbnail_id($recent_comment->comment_post_ID) );
      echo '<a href="'.get_comment_link($recent_comment).'" target="_blank">'.get_the_title($recent_comment->comment_post_ID).'</a>';
      echo $recent_comment->comment_content;
      echo $recent_comment->comment_date;
      echo "</li>"
    endforeach;
?>

I hope this is useful for you.

Upvotes: 1

Dhanuka Nuwan
Dhanuka Nuwan

Reputation: 700

you have comment post ID no ? so using that you can get product thumbnail and the product name. Woocommerce products also working same as other post types in WP. So, I'm not gonna write full code here but the part you need.

to get product thumbnail (this will return the url of thumbnail) :

<?php echo wp_get_attachment_url( get_post_thumbnail_id($c->comment_post_ID) ); ?>

to get product title:

<?php $your_product = get_post($c->comment_post_ID);
$product_name = $your_product->post_title(); ?>

I hope this will help you.

Upvotes: 0

Related Questions