Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Woocommerce中以编程方式添加带有评级的产品评论_Php_Wordpress_Woocommerce_Comments_Rating - Fatal编程技术网

Php 在Woocommerce中以编程方式添加带有评级的产品评论

Php 在Woocommerce中以编程方式添加带有评级的产品评论,php,wordpress,woocommerce,comments,rating,Php,Wordpress,Woocommerce,Comments,Rating,标题说明了一切。我知道评论是Wordpress中的原生评论帖子类型。我已经包含了添加注释的代码 但问题是,我不清楚如何对评论进行评级,以及如何将其与特定产品联系起来。当我使用comment_post_ID时,它似乎没有将评论(review)分配给正确的帖子 $time = current_time('mysql'); $data = array( 'comment_post_ID' => 1, 'comment_author' => 'admin', 'co

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

但问题是,我不清楚如何对评论进行评级,以及如何将其与特定产品联系起来。当我使用comment_post_ID时,它似乎没有将评论(review)分配给正确的帖子

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

通过键
“comment\u post\u ID”
可以显示您的评论,因此需要产品ID

然后您可以使用专用的WordPress功能添加评级,如:

update_comment_meta( $comment_id, 'rating', 3 ); // The rating is an integer from 1 to 5
因此,您的代码如下所示(其中,
$product\u id
是本次审查的目标产品id):

$comment\u id=wp\u insert\u comment(数组)(
'comment_post_ID'=>37,//'LoicTheAztec',
'评论\u作者\u电子邮件'=>'loictheaztec@fantastic.com', //  '',
“评论内容”=>“此处内容”,
'注释类型'=>'',
'comment_parent'=>0,
“用户id”=>5,/”,
'评论\代理'=>'',
'comment_date'=>date('Y-m-d H:i:s'),
“已批准意见”=>1,
) );
//此处插入评级(从1到5的整数)
更新评论元($comment\u id,'rating',3);
测试并按预期工作

作者电子邮件用户ID必须是一些现有的


接受的答案不完整,因为问题包含如何将其与特定产品联系起来。此外,评论类型应作为评论填充。如果评论与产品无关,Yoast schema和其他变量不会填充所有变量,谷歌将向您发出关于schema标记的警告

将以下内容添加到已接受的答案中:

'comment_type'          => 'review'
此外,请与产品(您的
$post\u id
)联系,备注布尔值和数组

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

现在,谷歌接受了这一点,认为这是Woocommerce 3和2020年的魅力所在@loictheastec您可以根据需要编辑自己。

您的代码应该很好,您是否检查了向数据库插入注释的代码?因为
comment\u post\u ID
是将注释与任何类型的帖子关联起来的ID,非常感谢您的帮助和反馈。我非常感激,太好了!现在我的插件可以正常工作了!产品元丢失。但请记住,只有在创建产品时才应使用这种方法,否则计数就会开始不匹配。
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));