Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get_comment_meta() of rating from Woocommerce Plugin?

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?

like image 753
Gregory Avatar asked Jul 29 '14 15:07

Gregory


1 Answers

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.

like image 97
mujuonly Avatar answered Sep 30 '22 08:09

mujuonly