Programmatically – Create Comment with Star Rating – WP/ WooCommerce Review

Programmatically – Create Comment with Star Rating – WP/ WooCommerce Review

In this Tech Post We will see How we can Insert WordPress Comment with star rating or how to create Woocommerce Product reviews Programmatically.

Requirements for posting star rating or reviews in WordPress or WooCommerce product programmatically or with coding is below:

  • Post ID where you want to show rating or reviews
  • Review poster name and email address.
  • And then We will get Comment ID after inserting comment to that post.
  • with that comment ID we will add meta data to that comment ID that will store our star rating value.

Complete Code for Posting Star Rating to WordPress or Woocommrce

$data = array(
        'comment_post_ID' => 50, // product id you want to add review
        'comment_author' => 'ntAfzal', // author name
        'comment_author_email' => 'info@ntafzal.com', // author email
        'comment_author_url' => 'https://ntafzal.com/',
        'comment_content' => 'Here is testing review comment',
        'comment_type' => 'review', // add type review
        'comment_parent' => 0, // If you want to reply a comment then add parent review Id
        'user_id' => 0, // add user id if you want to attach it with user
        'comment_author_IP' => '',
        'comment_agent' => '',
        'comment_date' => '2021-11-11 12:00:00',
        'comment_approved' => 0, // use 1 for approved
    );
     $comment_id = wp_insert_comment($data); // insert comment
update_comment_meta( $comment_id, 'rating', 5 );

Related Posts

Add Your Comment


Comments

LDtBTjEP
LDtBTjEP | Posted on 30 January 2024

YHpEtreuZDxV