6

The title says it all. I know the reviews are the native comments post type in Wordpress. I have included the code to add a comment.

The problem is however I am unclear how to give the comment a rating and how to tie it to a particular product. When I use the comment_post_ID it does not seem to be assigning the comment (review) to the correct post.

$time = current_time('mysql');

$data = array(
    'comment_post_ID' => 1,
    'comment_author' => 'admin',
    'comment_author_email' => 'admin@admin.com',
    'comment_author_url' => 'http://',
    'comment_content' => 'content here',
    'comment_type' => '',
    'comment_parent' => 0,
    'user_id' => 1,
    'comment_author_IP' => '127.0.0.1',
    'comment_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)',
    'comment_date' => $time,
    'comment_approved' => 1,
);

wp_insert_comment($data);
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Rob Gelhausen
  • 475
  • 1
  • 5
  • 9
  • your code should be fine, did you check if you code inserting comment to database ?as the `comment_post_ID` is the one which associate the comment with any type of posts – kashalo Aug 31 '18 at 21:46

2 Answers2

8

With the key 'comment_post_ID' is where your comment will be shown, so desired product ID

Then you can use update_comment_meta() dedicated WordPress function to add a rating, like:

update_comment_meta( $comment_id, 'rating', 3 ); // The rating is an integer from 1 to 5

So your code will be like (where $product_id is the targeted product Id for this review):

$comment_id = wp_insert_comment( array(
    'comment_post_ID'      => 37, // <=== The product ID where the review will show up
    'comment_author'       => 'LoicTheAztec',
    'comment_author_email' => 'loictheaztec@fantastic.com', // <== Important
    'comment_author_url'   => '',
    'comment_content'      => 'content here',
    'comment_type'         => '',
    'comment_parent'       => 0,
    'user_id'              => 5, // <== Important
    'comment_author_IP'    => '',
    'comment_agent'        => '',
    'comment_date'         => date('Y-m-d H:i:s'),
    'comment_approved'     => 1,
) );

// HERE inserting the rating (an integer from 1 to 5)
update_comment_meta( $comment_id, 'rating', 3 );

Tested and works as intended.

The author email and the user ID need to be some existing ones.

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
5

The accepted answer is not complete as the question contains how to tie it to a particular product. Also comment type should be populated as review. If the review is not tied to the product, Yoast schema and other dont populate all variables and Google will give you Warning about the schema markup.

Add this to the accepted answer:

'comment_type'          => 'review'

Also, tie to the product (your $post_id), remark bool values and arrays.

if($comment_id) update_comment_meta($comment_id, 'rating', 5);
if($comment_id) update_comment_meta($comment_id, 'verified', 1);
    
if($comment_id) update_post_meta($post_id, '_wc_average_rating', '5.00');
if($comment_id) update_post_meta($post_id, '_wc_review_count', '1');
if($comment_id) update_post_meta($post_id, '_wc_rating_count', array(1));

Now Google accept this as a charm as of Woocommerce 3 and year 2020. @loictheastec you can edit yourself if you want.

Jonas Lundman
  • 1,390
  • 16
  • 18
  • Great! Now my plugin works as expected! Product meta was missing. But remember this approach only should be used when the product is created -as the counts otherwise start to missmatch. – cavameta Sep 10 '20 at 07:35
  • 1
    How do you handle this meta data as you add more reviews/ratings? – David Healey Jan 21 '22 at 00:38