I have been working on this myself with very little coding experience.
So far I have found that you can add in another value but you need to update a few of the files in the woocommerce template. In the single-product-reviews.php
, just repeat the code from the <p>
to </p>
. See where I have added in the newrating
star select
if ( get_option('woocommerce_enable_review_rating') == 'yes' ) {
$comment_form['comment_field'] = '<p class="comment-form-rating"><label for="rating">' . __( '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>
add new code below:
<p class="comment-form-rating"><label for="newrating">' . __( 'New Rating', 'woocommerce' ) .'</label><select name="newrating" id="newrating">
<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>;
After saving this you should be able to see the new star field, or possible a dropdown select box as we haven’t defined newrating as a css style anywhere. You can do this is you like. The ratings field appears on the form, but it won’t do anything yet until you set the new comments_meta in the database.
Lets now create the new comment_meta to store this data for this NewRating field, which would be inside the /wp-content/plugins/woocommerce/includes/class-wc-comments.php
file. Find this code:
public function add_comment_rating( $comment_id ) {
if ( isset( $_POST['rating'] ) ) {
if ( ! $_POST['rating'] || $_POST['rating'] > 5 || $_POST['rating'] < 0 )
return;
add_comment_meta( $comment_id, 'rating', (int) esc_attr( $_POST['rating'] ), true );
$this->clear_transients( $comment_id );
}
and repeat this below:
if ( isset( $_POST['newrating'] ) ) {
if ( ! $_POST['newrating'] || $_POST['newrating'] > 5 || $_POST['newrating'] < 0 )
return;
add_comment_meta( $comment_id, 'newrating', (int) esc_attr( $_POST['newrating'] ), true );
$this->clear_transients( $comment_id );
}
}
The add_comment_meta function creates the field in the comments_meta table of WordPress and links it to the comment and post IDs. So now you have stored a second rating value in the database.
The next step would be to display the second rating when the reviews are shown. This is done by modifying the file:
/single-product/review.php
Add in a line near the top where you can see $rating
which is basically a copy and paste:
$newrating = intval( get_comment_meta( $comment->comment_ID, 'newrating', true ) );
And then after you see the if statement on around line 26, copy and paste this whole code and change the variable to newrating
<?php if ( $newrating && get_option( 'woocommerce_enable_review_rating' ) == 'yes' ) : ?>
<div itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating" class="star-rating" title="<?php echo sprintf(__( 'Rated %d out of 5', 'woocommerce' ), $newrating) ?>">
<span style="width:<?php echo ( intval( get_comment_meta( $GLOBALS['comment']->comment_ID, 'newrating', true ) ) / 5 ) * 100; ?>%"><strong itemprop="ratingValue"><?php echo intval( get_comment_meta( $GLOBALS['comment']->comment_ID, 'newrating', true ) ); ?></strong><?php _e( 'out of 5', 'woocommerce' ); ?></span>
</div>
<?php endif; ?>
This last section of code will print the newrating
if it exists
I am still playing around with trying to have a line of 10 or so review categories, and then working out the average of those to create the overall rating. I have only got as far as this so far.
Keep in mind this would all be erased if you upgrade your Woocommerce plugin, so use templates where you can. If i knew how I would make this a plugin, but i’m only a beginner to WP. Cheers