在Woocommerce中以编程方式添加带有评分的产品评论 [英] Add a product review with ratings programmatically in Woocommerce

查看:211
本文介绍了在Woocommerce中以编程方式添加带有评分的产品评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题说明了一切。我知道评论是Wordpress中的本机评论帖子类型。我已经包含了添加注释的代码。



但是问题是我不清楚如何给评论添加评级以及如何将其与特定产品绑定。当我使用comment_post_ID时,似乎没有将评论(评论)分配给正确的帖子。

  $ 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'=>'此处内容',
'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; zh-CN; 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);


解决方案

使用键'comment_post_ID '将显示您的评论,因此需要的产品ID



然后您可以使用


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

解决方案

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.

这篇关于在Woocommerce中以编程方式添加带有评分的产品评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆