woocommerce offtopic - Hide review form if user has left a comment

admin2025-04-19  0

I am trying to hide the comments form if a user has left a comment on that particular post, otherwise display the comment form.

I added a code snippet to a hook which hides the form, but reverts to the WordPress comment form. I'm unable to figure out how to hide that form! I would also be open to hiding that entire column if need be.

Thanks to @squints for his post

function hide_review_form( $review_form ) {
    global $current_user, $post;

$usercomment = get_comments(array('user_id' => $current_user->ID,'post_id' => $post->ID) ); 

if (! $usercomment) {
  return $review_form;
    }
}
add_filter( 'woocommerce_product_review_comment_form_args', 'hide_review_form' );

I am trying to hide the comments form if a user has left a comment on that particular post, otherwise display the comment form.

I added a code snippet to a hook which hides the form, but reverts to the WordPress comment form. I'm unable to figure out how to hide that form! I would also be open to hiding that entire column if need be.

Thanks to @squints for his post

function hide_review_form( $review_form ) {
    global $current_user, $post;

$usercomment = get_comments(array('user_id' => $current_user->ID,'post_id' => $post->ID) ); 

if (! $usercomment) {
  return $review_form;
    }
}
add_filter( 'woocommerce_product_review_comment_form_args', 'hide_review_form' );
Share Improve this question edited Nov 14, 2019 at 20:51 Shamsher Ali asked Nov 13, 2019 at 20:20 Shamsher AliShamsher Ali 11 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 0

You can't just do a simple If statement like this? ...

if ( count( $usercomment ) < 1 ) {
    comment_form();
};

I don't know why you would have to unset it.

Try below code which is return comment count of current user who posted comments on post. get_comment() have count parameter which returns only count of comment. in your code you are using $commentdata variable which is not defined. I have tested below code and it is working fine for me. let me know if this works for you.

global $current_user, $post;

if ( ! is_user_logged_in() ) {
    comment_form();
} else {

      $is_commented_count = get_comments(array('user_id' => $current_user->ID, 'post_id'=>$post->ID,'count' => true) );
      if($is_commented_count < 1) {
          comment_form();
      }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745003083a279348.html

最新回复(0)