我一直在努力让用户能够在前端发布对 WooCommerce 产品评论的回复。默认情况下,您只能在管理仪表板中执行此操作。
我将此代码添加到主题的
functions.php
中,它在每个评论下方添加了一个reply
按钮。
// functions.php - displays a reply button on every product review, so you could post a reply to them on front-end
add_action('woocommerce_review_comment_text', 'add_reply_button_to_wc_review', 20);
function add_reply_button_to_wc_review()
{
$post_id = get_the_ID();
$comment_id = get_comment_ID();
//get the setting configured in the admin panel under settings discussions "Enable threaded (nested) comments levels deep"
$max_depth = get_option('thread_comments_depth');
//add max_depth to the array and give it the value from above and set the depth to 1
$default = array(
'add_below' => 'comment',
'respond_id' => 'respond',
'reply_text' => __('Reply'),
'login_text' => __('Log in to Reply'),
'depth' => 1,
'before' => '',
'after' => '',
'max_depth' => $max_depth
);
comment_reply_link($default, $comment_id, $post_id);
}
我在每条评论下方都添加了一个回复按钮,它起作用了。当您点击
Reply
按钮时,Comment Form
将显示在该评论下方,您可以写下您的回复并发布。只是,Rating field
里的Comment form
有问题。
即使您只想回复另一条评论,也必须对产品进行评分。我不想在你回复的时候打分。
问题是关于一个名为
Comment form
(发布评论/回复的简单表格)的部分:
如图所示,WooCommerce 的评论表单有评分部分(图中绿色部分)。下面的代码将评分部分添加到评论表单:
// Code responsible for adding RATING SECTION (from WooCommerce default template) - woocommerce/single-product-reviews.php
if ( wc_review_ratings_enabled() ) {
$comment_form['comment_field'] = '<div class="comment-form-rating"><label for="rating">' . esc_html__( 'Your rating', 'woocommerce' ) . ( wc_review_ratings_required() ? ' <span class="required">*</span>' : '' ) . '</label><select name="rating" id="rating" required>
<option value="">' . esc_html__( 'Rate…', 'woocommerce' ) . '</option>
<option value="5">' . esc_html__( 'Perfect', 'woocommerce' ) . '</option>
<option value="4">' . esc_html__( 'Good', 'woocommerce' ) . '</option>
<option value="3">' . esc_html__( 'Average', 'woocommerce' ) . '</option>
<option value="2">' . esc_html__( 'Not that bad', 'woocommerce' ) . '</option>
<option value="1">' . esc_html__( 'Very poor', 'woocommerce' ) . '</option>
</select></div>';
}
评分字段用于在您要评论产品时对产品进行评分。但是当您想回复另一条评论时,评分没有意义!当您想回复另一条评论时,我希望删除评论表的评分部分。只有当你想写评论时才需要评级部分。
但我不知道如何告诉 WordPress 在显示评论表单时区分评论和回复评论。当我点击评论下方的回复按钮时,它会显示相同的回复评论表单。在这种情况下我不想要评级部分(当只是回复另一条评论时):
我不知道该做什么。我试图在
single-product-reviews.php
模板(上面显示的代码)中操作有关评级部分的代码,但没有成功。
如果您使用此代码,您可以区分评论和对评论的回复:
// woocommerce/single-product/review.php
if ($comment->comment_parent > 0) {
// It's a reply
} else {
// It's a review
}
但是它只对已经发布的评论有用。当我回复评论时,我无法使用它来更改评论表单并删除评级部分。如果它只是对评论的回复,我应该以某种方式告诉它忽略有关在
rating section
(如上所示)中添加single-product-reviews.php
的代码。
感谢任何帮助。