[Plugin: WP ecommerce] Restrict product rating to logged in users
-
Hi all,
That’s probably a piece of cake for any developer but I am not proficient enough in php…
I am currently setting up wp ecommerce (using the mojo theme from splashingpiels) and found out that users that are not logged in can rate a product. Ideally only users that have bought a product should be able to rate it, however I want to keep it simple and only allow logged in users to rate the products.
WordPress provides the reference function
if ( is_user_logged_in()
that can be used to achieve the desired function. This will also involve:
1. an if/else statement
2. a smart copy/paste of the relevant parts of the code below
3. the insertion of a new message__( 'Sorry, you must be logged in to rate a product!', 'sp' )
to inform users.if ( ! function_exists( 'sp_product_rate' ) ) { function sp_product_rate() { global $wpdb; $response = ''; $nonce = $_POST['ajaxCustomNonce']; $rate = mysql_real_escape_string( trim( $_POST['rate'] ) ); $id = mysql_real_escape_string( trim($_POST['id'] ) ); $ip = $_SERVER['REMOTE_ADDR']; $current_time = time(); if ( ! wp_verify_nonce( $nonce, 'ajax_custom_nonce' ) ) { die ( 'Busted!' ); } $sql = "SELECT COUNT(id) FROM {$wpdb->prefix}wpsc_product_rating WHERE ipnum = '$ip' AND productid = '$id'"; $get_result = $wpdb->get_var( $sql ); if ( $get_result != 0 ) { $sql = "SELECT AVG(rated) FROM {$wpdb->prefix}wpsc_product_rating WHERE productid = '$id'"; $current_rating = $wpdb->get_var( $sql ); $current_rating = floor( $current_rating ); $response .= '<span class="star-rating-control">'; for ( $i = 1; $i < 6; $i++ ) { $checked = ''; if ( $current_rating >= 1 ) { $checked = 'star-rating-on'; } $response .= '<div class="wpec-star-rating rater-0 star star-rating-applied star-rating-readonly ' . $checked . '">'; $response .= '<a title="'.$i.'">'.$i.'</a></div>'; $current_rating = $current_rating - 1; } $response .= '</span><p class="message">' . __( 'Sorry, you already rated!', 'sp' ) . '</p>'; echo $response; } else { $sql = "INSERT INTO {$wpdb->prefix}wpsc_product_rating (ipnum,productid,rated,time) VALUES ('$ip','$id','$rate','$current_time')"; $wpdb->query( $sql ); $sql = "SELECT AVG(rated) FROM {$wpdb->prefix}wpsc_product_rating WHERE productid = '$id'"; $current_rating = $wpdb->get_var( $sql ); $current_rating = floor( $current_rating ); $response .= '<span class="star-rating-control">'; for ( $i = 1; $i < 6; $i++ ) { $checked = ''; if ($current_rating >= 1) { $checked = 'star-rating-on'; } $response .= '<div class="wpec-star-rating rater-0 star star-rating-applied star-rating-readonly ' . $checked . '">'; $response .= '<a title="'.$i.'">'.$i.'</a></div>'; $current_rating = $current_rating - 1; } $response .= '</span><p class="message">' . __( 'Thanks for rating!', 'sp' ) . '</p>'; echo $response; } exit; } }
- The topic ‘[Plugin: WP ecommerce] Restrict product rating to logged in users’ is closed to new replies.