0

I need to add additional fields to a Woocommerce product rating system like this: enter image description here

I opened this file: /wp-content/plugins/woocommerce/templates/single-product-reviews.php and saw the following lines:

$comment_form = array(
        'title_reply' => $title_reply,
        'comment_notes_before' => '',
        'comment_notes_after' => '',
        'fields' => array(
            'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Name', 'woocommerce' ) . '</label> ' . '<span class="required">*</span>' .
                        '<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' ) . '</label> ' . '<span class="required">*</span>' .
                        '<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30" aria-required="true" /></p>',
        ),
        'label_submit' => __( 'Submit Review', 'woocommerce' ),
        'logged_in_as' => '',
        'comment_field' => ''
    );

and this

$comment_form['comment_field'] = '<p class="comment-form-rating"><label for="rating">' . __( 'Rating', 'woocommerce' ) .'</label><select name="rating" id="rating">
            <option value="">'.__( 'Rate&hellip;', '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>';

Is there any way I can add another select inputs?

5

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&hellip;', '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&hellip;', '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

3
  • I managed to achieve what I needed by creating a custom rating system plugin. Anyway, thanks for your answer, it may help me later on. – Daniel Patilea Mar 17 '14 at 8:58
  • Are you able to share you answer too? Im having trouble changing the styles to suit the stars, I can only get mine to show the drop down type select box. – Justice_ Mar 17 '14 at 9:12
  • I believe i may have to edit the single-product.js file – Justice_ Mar 17 '14 at 9:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.