gpt4 book ai didi

php - 在 Woocommerce 中以编程方式添加带有评分的产品评论

转载 作者:行者123 更新时间:2023-12-04 02:57:45 25 4
gpt4 key购买 nike

标题说明了一切。我知道评论是 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' => '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);

最佳答案

带 key 'comment_post_ID'是您的评论将显示的地方,因此所需的产品 ID

那么你可以使用 update_comment_meta() 专用的 WordPress 功能来添加评级,例如:

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

因此,您的代码将类似于(其中 $product_id 是此评论的目标产品 ID):
$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 );

经过测试并按预期工作。

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



enter image description here

关于php - 在 Woocommerce 中以编程方式添加带有评分的产品评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52122275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com