Skip to content

Commit

Permalink
Option to limit reviews to purchasers. Closes woocommerce#4021
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Oct 31, 2013
1 parent 3c80c9d commit 19fcf30
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 83 deletions.
10 changes: 10 additions & 0 deletions includes/admin/settings/class-wc-settings-products.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,16 @@ public function get_settings( $current_section = '' ) {
'id' => 'woocommerce_review_rating_verification_label',
'default' => 'yes',
'type' => 'checkbox',
'checkboxgroup' => '',
'show_if_checked' => 'yes',
'autoload' => false
),

array(
'desc' => __( 'Only allow reviews from "verified owners"', 'woocommerce' ),
'id' => 'woocommerce_review_rating_verification_required',
'default' => 'no',
'type' => 'checkbox',
'checkboxgroup' => 'end',
'show_if_checked' => 'yes',
'autoload' => false
Expand Down
9 changes: 5 additions & 4 deletions includes/wc-customer-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ function woocommerce_customer_bought_product( $customer_email, $user_id, $produc
if ( sizeof( $emails ) == 0 )
return false;

$completed = get_term_by( 'slug', 'completed', 'shop_order_status' );
$completed = get_term_by( 'slug', 'completed', 'shop_order_status' );
$processing = get_term_by( 'slug', 'processing', 'shop_order_status' );

return $wpdb->get_var(
$wpdb->prepare( "
Expand All @@ -243,18 +244,18 @@ function woocommerce_customer_bought_product( $customer_email, $user_id, $produc
LEFT JOIN {$wpdb->postmeta} AS postmeta ON order_items.order_id = postmeta.post_id
LEFT JOIN {$wpdb->term_relationships} AS rel ON order_items.order_id = rel.object_ID
WHERE
rel.term_taxonomy_id = %d AND
rel.term_taxonomy_id IN ( %d, %d ) AND
itemmeta.meta_value = %s AND
itemmeta.meta_key IN ( '_variation_id', '_product_id' ) AND
postmeta.meta_key IN ( '_billing_email', '_customer_user' ) AND
(
postmeta.meta_value IN ( '" . implode( "','", array_unique( $emails ) ) . "' ) OR
(
postmeta.meta_value = %d AND
postmeta.meta_value = %s AND
postmeta.meta_value > 0
)
)
", $completed->term_taxonomy_id, $product_id, $user_id
", $completed->term_taxonomy_id, $processing->term_taxonomy_id, $product_id, $user_id
)
);
}
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
* Feature - WC_DELIMITER to customise the pipes for attributes
* Feature - Standardized, default credit card form for gateways to use if they support 'default_credit_card_form'.
* Feature - Coupon usage limits per user (using email + ID).
* Feature - Option to limit reviews to purchasers.
* Tweak - Added pagination to tax rate screens.
* Tweak - Added filter to check the 'Create account' checkbox on checkout by default.
* Tweak - Update CPT parameters for 'product_variation' and 'shop_coupon' to be no longer public.
Expand Down
157 changes: 78 additions & 79 deletions templates/single-product-reviews.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,92 +4,91 @@
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.0.3
* @version 2.1.0
*/
global $woocommerce, $product;

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit; // Exit if accessed directly

if ( ! comments_open() )
return;
?>
<?php if ( comments_open() ) : ?><div id="reviews"><?php

echo '<div id="comments">';

if ( get_option('woocommerce_enable_review_rating') == 'yes' && ( $count = $product->get_rating_count() ) ) {

echo '<h2>' . sprintf( _n('%s review for %s', '%s reviews for %s', $count, 'woocommerce'), $count, get_the_title() ) . '</h2>';

} else {

echo '<h2>'.__( 'Reviews', 'woocommerce' ).'</h2>';

}

$title_reply = '';

if ( have_comments() ) :

echo '<ol class="commentlist">';

wp_list_comments( apply_filters( 'woocommerce_product_review_list_args', array( 'callback' => 'woocommerce_comments' ) ) );

echo '</ol>';

if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : ?>
<div class="navigation">
<div class="nav-previous wc-backward"><?php previous_comments_link( __( 'Previous', 'woocommerce' ) ); ?></div>
<div class="nav-next wc-forward"><?php next_comments_link( __( 'Next', 'woocommerce' ) ); ?></div>
<div id="reviews">
<div id="comments">
<h2><?php
if ( get_option( 'woocommerce_enable_review_rating' ) == 'yes' && ( $count = $product->get_rating_count() ) )
printf( _n('%s review for %s', '%s reviews for %s', $count, 'woocommerce'), $count, get_the_title() );
else
_e( 'Reviews', 'woocommerce' );
?></h2>

<?php if ( have_comments() ) : ?>

<ol class="commentlist">
<?php wp_list_comments( apply_filters( 'woocommerce_product_review_list_args', array( 'callback' => 'woocommerce_comments' ) ) ); ?>
</ol>

<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : ?>
<div class="navigation">
<div class="nav-previous wc-backward"><?php previous_comments_link( __( 'Previous', 'woocommerce' ) ); ?></div>
<div class="nav-next wc-forward"><?php next_comments_link( __( 'Next', 'woocommerce' ) ); ?></div>
</div>
<?php endif; ?>

<?php else : ?>

<p class="woocommerce-noreviews"><?php _e( 'There are no reviews yet.', 'woocommerce' ); ?></p>

<?php endif; ?>
</div>

<?php if ( get_option( 'woocommerce_review_rating_verification_required' ) == 'no' || woocommerce_customer_bought_product( '', get_current_user_id(), $product->id ) ) : ?>

<div id="review_form_wrapper">
<div id="review_form">
<?php
$commenter = wp_get_current_commenter();

$comment_form = array(
'title_reply' => have_comments() ? __( 'Add a review', 'woocommerce' ) : __( 'Be the first to review', 'woocommerce' ).' &ldquo;' . get_the_title() . '&rdquo;',
'title_reply_to' => __( 'Leave a Reply to %s', 'woocommerce' ),
'comment_notes_before' => '',
'comment_notes_after' => '',
'fields' => array(
'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Name', 'woocommerce' ) . ' <span class="required">*</span></label> ' .
'<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' ) . ' <span class="required">*</span></label> ' .
'<input id="email" name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30" aria-required="true" /></p>',
),
'label_submit' => __( 'Submit', 'woocommerce' ),
'logged_in_as' => '',
'comment_field' => ''
);

if ( get_option('woocommerce_enable_review_rating') == 'yes' ) {
$comment_form['comment_field'] = '<p class="comment-form-rating"><label for="rating">' . __( 'Your 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>';
}

$comment_form['comment_field'] .= '<p class="comment-form-comment"><label for="comment">' . __( 'Your Review', 'woocommerce' ) . '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea>' . wp_nonce_field( 'woocommerce-comment_rating', '_wpnonce', true, false ) . '</p>';

comment_form( apply_filters( 'woocommerce_product_review_comment_form_args', $comment_form ) );
?>
</div>
<?php endif;

$title_reply = __( 'Add a review', 'woocommerce' );

else :

$title_reply = __( 'Be the first to review', 'woocommerce' ).' &ldquo;' . get_the_title() . '&rdquo;';

echo '<p class="noreviews">' . __( 'There are no reviews yet.', 'woocommerce' ) . '</p>';

endif;

$commenter = wp_get_current_commenter();

echo '</div><div id="review_form_wrapper"><div id="review_form">';

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

if ( get_option('woocommerce_enable_review_rating') == 'yes' ) {

$comment_form['comment_field'] = '<p class="comment-form-rating"><label for="rating">' . __( 'Your 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>';

}
</div>

$comment_form['comment_field'] .= '<p class="comment-form-comment"><label for="comment">' . __( 'Your Review', 'woocommerce' ) . '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea>' . wp_nonce_field( 'woocommerce-comment_rating', '_wpnonce', true, false ) . '</p>';
<?php else : ?>

comment_form( apply_filters( 'woocommerce_product_review_comment_form_args', $comment_form ) );
<p class="woocommerce-verification-required"><?php _e( 'Only logged in customers who have purchased this product may leave a review.', 'woocommerce' ); ?></p>

echo '</div></div>';
<?php endif; ?>

?><div class="clear"></div></div>
<?php endif; ?>
<div class="clear"></div>
</div>

0 comments on commit 19fcf30

Please sign in to comment.