如何从Woocommerce Plugin获取评级的get_comment_meta()?

时间:2014-07-29 15:15:37

标签: wordpress wordpress-plugin woocommerce

我在wordpress网站上有一个产品供应商woocommerce设置。人们可以注册并添加自己的产品,这些产品只是自定义帖子类型等,购买这些产品的其他人可以查看它们。评论是WordPress的一部分'自定义帖子类型产品的评论模板。我使用以下代码显示购买产品的人(自定义帖子类型)的单一评论(wordpress评论):

我想要工作的主要部分是通过woocommerce插件添加的星级评分位:

        echo('Rating: <div class="star-rating" itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating"><span style="width:' . ( get_comment_meta( $comment->comment_ID, 'rating', true ) / 5 ) * 100 . '%"><strong itemprop="ratingValue">' . get_comment_meta( $comment->comment_ID, 'rating', true ) . '</strong></span></div><br />');

完整代码:

<?php 
if ( is_user_logged_in() ) {
$user_id = get_current_user_id();
$args = array(
    'orderby' => 'date',
    'post_type' => 'product',
    'number' => '4',
    'post_author' => $user_id
);
$comments = get_comments($args);
foreach($comments as $comment) :
    echo '<div>'; 
    echo('Review By: ' . $comment->comment_author . '<br />');
    echo('Product: ' . '<a href="' . post_permalink($comment->ID)
 . '">' . $comment->post_title . '</a>' . '<br />');
    echo('Date: ' . $comment->comment_date . '<br />');
    echo('Rating: <div class="star-rating" itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating"><span style="width:' . ( get_comment_meta( $comment->comment_ID, 'rating', true ) / 5 ) * 100 . '%"><strong itemprop="ratingValue">' . get_comment_meta( $comment->comment_ID, 'rating', true ) . '</strong></span></div><br />');
    echo('Review: ' . $comment->comment_content );
    echo '</div>';
endforeach;
}
?>

这段代码放在一个页面上并使用它运行的插件Exec PHP。

我将它添加到我创建的另一个页面(常规wordpress页面)并删除     &#39;数&#39; =&GT; 4, 从数组中,以便显示所有评论(评论)。这没有用。所以我复制了代码,角色的角色,这仍然没有用。

所以它在他们的&#34; admin&#34;上显示评级,这只是评论元。页面,但不是他们的&#34;反馈&#34;页。

现在我已经加载了网站,并且它已停止在任一页面上显示评分。

任何人都可以帮忙解释一下吗?

1 个答案:

答案 0 :(得分:0)

global $wpdb;

$args = apply_filters('product_reviews_args', array(
    'status' => 'all',
    'orderby' => 'comment_ID',
    'order' => 'ASC',
    'post_type' => 'product',
));

$stars = apply_filters('product_reviews_ratings', array(3, 4)); // change the star rating needed here
if (!empty($stars)) {
    $args['meta_query'] = array(array('key' => 'rating', 'value' => $stars));
}


$comment_query = new WP_Comment_Query;
$comments = $comment_query->query($args);

foreach ($comments as $comment) {

    $rating = get_comment_meta($comment_ID, 'rating', true);
    $verfied_customer = get_comment_meta($comment_ID, 'verified', true);
    $review_title = get_comment_meta($comment_ID, 'title', true);
}

此处WP_Comment_Query可能有很多参数,因此可能会更有用。