我想通过产品ID或其他任何内容获取woocommerce产品评论,并希望将其显示在我创建的模板中。
答案 0 :(得分:15)
Woocommerce通过常规wordpress评论进行评论。
简单的方法是获得'post_type'=>的评论'产品',将获得原始评论数据。为了显示正确的woocommerce评论,您需要应用回调函数,这就是'callback'=> 'woocommerce_comments'。
整个事情是:
<?php
$args = array ('post_type' => 'product');
$comments = get_comments( $args );
wp_list_comments( array( 'callback' => 'woocommerce_comments' ), $comments);
?>
如果您想按产品ID获取评论,则需要更改$ args:
$args = array ('post_id' => 123);
要自定义它,请查看参考:
http://codex.wordpress.org/Function_Reference/wp_list_comments http://codex.wordpress.org/get_comments
'callback'=&gt; 'woocommerce_comments'函数使用位于plugins / woocomerce / templates / single-product / review.php中的模板
答案 1 :(得分:5)
只需尝试此操作即可在产品页面的comments_template();
模板中显示评论。它的工作。
有关详细信息,请访问here
答案 2 :(得分:4)
<div id="reviews">
<div id="comments">
<h2><?php
if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' && ( $count = $product->get_rating_count() ) )
printf( _n( '%s review for %s', '%s reviews for %s', $count, 'woocommerce' ), $count, get_the_title() );
else
_e( 'Reviews', 'woocommerce' );
?></h2>
<?php if ( have_comments() ) : ?>
<ol class="commentlist">
<?php wp_list_comments( apply_filters( 'woocommerce_product_review_list_args', array( 'callback' => 'woocommerce_comments' ) ) ); ?>
</ol>
<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) :
echo '<nav class="woocommerce-pagination">';
paginate_comments_links( apply_filters( 'woocommerce_comment_pagination_args', array(
'prev_text' => '←',
'next_text' => '→',
'type' => 'list',
) ) );
echo '</nav>';
endif; ?>
<?php else : ?>
<p class="woocommerce-noreviews"><?php _e( 'There are no reviews yet.', 'woocommerce' ); ?></p>
<?php endif; ?>
</div>
<?php if ( get_option( 'woocommerce_review_rating_verification_required' ) === 'no' || wc_customer_bought_product( '', get_current_user_id(), $product->id ) ) : ?>
<div id="review_form_wrapper">
<div id="review_form">
<?php
$commenter = wp_get_current_commenter();
$comment_form = array(
'title_reply' => have_comments() ? __( 'Add a review', 'woocommerce' ) : __( 'Be the first to review', 'woocommerce' ) . ' “' . get_the_title() . '”',
'title_reply_to' => __( 'Leave a Reply to %s', 'woocommerce' ),
'comment_notes_before' => '',
'comment_notes_after' => '',
'fields' => array(
'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Name', 'woocommerce' ) . ' <span class="required">*</span></label> ' .
'<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30" aria-required="true" /></p>',
'email' => '<p class="comment-form-email"><label for="email">' . __( 'Email', 'woocommerce' ) . ' <span class="required">*</span></label> ' .
'<input id="email" name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30" aria-required="true" /></p>',
),
'label_submit' => __( 'Submit', 'woocommerce' ),
'logged_in_as' => '',
'comment_field' => ''
);
if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' ) {
$comment_form['comment_field'] = '<p class="comment-form-rating"><label for="rating">' . __( 'Your Rating', 'woocommerce' ) .'</label><select name="rating" id="rating">
<option value="">' . __( 'Rate…', 'woocommerce' ) . '</option>
<option value="5">' . __( 'Perfect', 'woocommerce' ) . '</option>
<option value="4">' . __( 'Good', 'woocommerce' ) . '</option>
<option value="3">' . __( 'Average', 'woocommerce' ) . '</option>
<option value="2">' . __( 'Not that bad', 'woocommerce' ) . '</option>
<option value="1">' . __( 'Very Poor', 'woocommerce' ) . '</option>
</select></p>';
}
$comment_form['comment_field'] .= '<p class="comment-form-comment"><label for="comment">' . __( 'Your Review', 'woocommerce' ) . '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>';
comment_form( apply_filters( 'woocommerce_product_review_comment_form_args', $comment_form ) );
?>
</div>
</div>
<?php else : ?>
<p class="woocommerce-verification-required"><?php _e( 'Only logged in customers who have purchased this product may leave a review.', 'woocommerce' ); ?></p>
<?php endif; ?>
<div class="clear"></div>
答案 3 :(得分:1)
是的先生,我已经尝试过我最好的水平,在默认标签的底部显示评论,但我不能这么做很多天,最后我找到了显示特定产品评论的方法。它工作得很好。令人敬畏的Sir Awesome。
但更重要的是在single-product.php上动态显示产品的评论 //动态获取产品ID并按ID显示(评论)。
global $product;
$id = $product->id;
echo $id.",";
$args = array ('post_type' => 'product', 'post_id' => $id);
$comments = get_comments( $args );
wp_list_comments( array( 'callback' => 'woocommerce_comments' ), $comments);
只需将此代码粘贴到single-product.php文件中即可查看结果。它会显示你想要的。
答案 4 :(得分:1)
在woocommerce/templates/single-product/tabs/tabs.php
如果您在声明后print_r()
上运行$tabs
,您会看到输出的一部分如下:
[reviews] => Array (
[title] => Reviews (3)
[priority] => 30
[callback] => comments_template
)
这意味着回调函数是:comments_template()
。
由于此函数不接受$post_id
参数,因此需要在循环中调用。
但是,该函数确实接受了要使用的模板文件的参数,因此要回答OP关于如何使用自定义模板文件的直接问题 - 您可以使用如下函数:
comments_template( string $file = '/comments.php', bool $separate_comments = false );
答案 5 :(得分:0)
你可能会因为你刚刚发布一段时间而对你的基山来说有点迟了,但也许它会帮助别人。
我们刚开发了一个免费插件,用产品ID和短代码显示woocommerce评论。它还插入了产品的Schema。
查看它,它是免费的:https://shopitpress.com/plugins/sip-reviews-shortcode-woocommerce/
答案 6 :(得分:0)
为了获得任何内置功能,我尝试了很多,但我没有。然后我写了自己的查询。它可能会对你有所帮助:
public class FindingBusStop extends android.app.Application {
@Override
public void onCreate() {
super.onCreate();
Parse.enableLocalDatastore(getApplicationContext());
ParseObject.registerSubclass(BusStop.class);
}
答案 7 :(得分:0)
最近我一直在努力,我想出了这个解决方案,可以让您在单个页面上输出所有产品的评论。
//Display all product reviews
if (!function_exists('display_all_reviews')) {
function display_all_reviews(){
$args = array(
'status' => 'approve',
'type' => 'review'
);
// The Query
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
// Comment Loop
if ( $comments ) {
echo "<ol>";
foreach ( $comments as $comment ): ?>
<?php if ( $comment->comment_approved == '0' ) : ?>
<p class="meta waiting-approval-info">
<em><?php _e( 'Thanks, your review is awaiting approval', 'woocommerce' ); ?></em>
</p>
<?php endif; ?>
<li itemprop="reviews" itemscope itemtype="http://schema.org/Review" <?php comment_class(); ?> id="li-review-<?php echo $comment->comment_ID; ?>">
<div id="review-<?php echo $comment->comment_ID; ?>" class="review_container">
<div class="review-avatar">
<?php echo get_avatar( $comment->comment_author_email, $size = '50' ); ?>
</div>
<div class="review-author">
<div class="review-author-name" itemprop="author"><?php echo $comment->comment_author; ?></div>
<div class='star-rating-container'>
<div itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating" class="star-rating" title="<?php echo esc_attr( get_comment_meta( $comment->comment_ID, 'rating', true ) ); ?>">
<span style="width:<?php echo get_comment_meta( $comment->comment_ID, 'rating', true )*22; ?>px"><span itemprop="ratingValue"><?php echo get_comment_meta( $comment->comment_ID, 'rating', true ); ?></span> <?php _e('out of 5', 'woocommerce'); ?></span>
<?php
$timestamp = strtotime( $comment->comment_date ); //Changing comment time to timestamp
$date = date('F d, Y', $timestamp);
?>
</div>
<em class="review-date">
<time itemprop="datePublished" datetime="<?php echo $comment->comment_date; ?>"><?php echo $date; ?></time>
</em>
</div>
</div>
<div class="clear"></div>
<div class="review-text">
<div itemprop="description" class="description">
<?php echo $comment->comment_content; ?>
</div>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
</li>
<?php
endforeach;
echo "</ol>";
} else {
echo "This product hasn't been rated yet.";
}
}
}
将上述函数添加到您的functions.php文件中。 之后,您可以通过以下任何方式在您想要的主题上使用此功能:
<?php echo display_all_reviews(); ?>
我还在https://www.majas-lapu-izstrade.lv/get-woocommerce-customer-reviews-from-all-products-display-average-and-all-ratings-in-a-histogram-without-a-plugin/处创建了一个教程,其中还包括输出平均产品评分,所有产品的所有评分的总数以及所有产品评论直方图的功能。