7
votes

I have a product vendor woocommerce setup on a wordpress site. People can sign up and add their own products, which are just custom post types, etc and other people who buy these products can review them. The reviews are part of WordPress' comment template for the custom post type Product. I'm using the following code to display single reviews (wordpress comments) from people who've bought the products(custom post types):

The main part that I wanted working was the star rating bit that's added through the woocommerce plugin:

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

The full code:

<?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;
}
?>

This code was placed on a page and using the plugin Exec PHP it worked.

I added it to another page I created (regular wordpress page) and removed 'number' => 4, from the array so that ALL reviews (comments) would show. This didn't work. So I copied the code, character for character, and this still didn't work.

So it was displaying the rating, which is just comment meta, on their "admin" page but not on their "feedback" page.

Now I've loaded the website up and it's stopped displaying the ratings on either page.

Can anyone help shed some light on this?

1
the above will display only comments made by the current logged in user. exec php is also out of date and could be a issue (i dont really know the plugin, but it could be). What i would do is create a new page template for this so you can code your php in that. But you need to figure out how to return the posts you are looking for, if its related to a particular post, use post_id in your args array, etc. Google creating wp page templates, you'll be surprised at how easy it actually is!David
Thanks man, Just used a page template to create a password reset page. Will give it a go as you're right, will handle PHP much better than the plugin. I'll let you know how it goes.Gregory

1 Answers

2
votes
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);
}

Here WP_Comment_Query may be more useful as it has lot of arguments.