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

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

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();
      }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745003061a4605614.html

相关推荐

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

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

    21小时前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信