Modify or Write a code to create a woocommerce reviews… · The Chat Archive
Explore / Programming Modify or Write a code to create a woocommerce reviews sort and feedback (Like/Dislike)… Save Share Download ▾ ReportFrom a Chatbot Arena battle The same person sent this prompt to two anonymous models at once. Compare it with gemini-2.5-pro ’s answer. Copy first prompt
Other models, same or similar prompt Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Details
Provider Anthropic
Model provenance As recorded by source dataset
Turns 1 (2 messages)
Language en
Added to archive Nov 16, 2026
ID 01m7jeqmqecra76gwntkrdpdq2Source & license Imported from LMArena Human Preference 140K (source ). Imported records earn no Research Points.
Upstream record: 5d8a156a-80a5-4231-a324-099fb259032d:a
Chiang et al., Chatbot Arena: An Open Platform for Evaluating LLMs by Human Preference (2024). LMArena 140K preference release.
License: Prompts CC-BY-4.0; outputs governed by provider terms
Dataset releases Not yet included in a published release.
Modify or Write a code to create a woocommerce reviews sort and feedback (Like/Dislike) features and modify the reviews pagination with Ajax
There should be two options to sort the reviews: first Sort by, second Filter by rating.
Sort By: Option like - Latest, Oldest, Highest Raring, Lowest Rating.
Filter by Rating: Option like - 5 Star, 4 Star, 3 Star, 2 Star, 1 Star.
Replace WooCommerce review pagination with AJAX to make it work without reloading the webpage.
add reviews feedback (Like/Dislike) features.
<?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' ) ) :
echo '<nav class="woocommerce-pagination">';
paginate_comments_links(
apply_filters(
'woocommerce_comment_pagination_args',
array(
'prev_text' => is_rtl() ? '→' : '←',
'next_text' => is_rtl() ? '←' : '→',
'type' => 'list',
)
)
);
echo '</nav>';
endif;
?>
<?php else : ?>
<p class="woocommerce-noreviews"><?php esc_html_e( 'There are no reviews yet.', 'woocommerce' ); ?></p>
<?php endif; ?>
</div>
<?php if ( get_option( 'woocommerce_review_rating_verification_required' ) === 'no' || wc_customer_bought_product( '', get_current_user_id(), $product->get_id() ) ) : ?>
<div id="review_form_wrapper">
<div id="review_form">
<?php
$commenter = wp_get_current_commenter();
$comment_form = array(
/* translators: %s is product title */
'title_reply' => have_comments() ? esc_html__( 'Add a review', 'woocommerce' ) : sprintf( esc_html__( 'Be the first to review “%s”', 'woocommerce' ), get_the_title() ),
/* translators: %s is product title */
'title_reply_to' => esc_html__( 'Leave a Reply to %s', 'woocommerce' ),
'title_reply_before' => '<span id="reply-title" class="comment-reply-title" role="heading" aria-level="3">',
'title_reply_after' => '</span>',
'comment_notes_after' => '',
'label_submit' => esc_html__( 'Submit', 'woocommerce' ),
'logged_in_as' => '',
'comment_field' => '',
);
$name_email_required = (bool) get_option( 'require_name_email', 1 );
$fields = array(
'author' => array(
'label' => __( 'Name', 'woocommerce' ),
'type' => 'text',
'value' => $commenter['comment_author'],
'required' => $name_email_required,
'autocomplete' => 'name',
),
'email' => array(
'label' => __( 'Email', 'woocommerce' ),
'type' => 'email',
'value' => $commenter['comment_author_email'],
'required' => $name_email_required,
'autocomplete' => 'email',
),
);
$comment_form['fields'] = array();
foreach ( $fields as $key => $field ) {
$field_html = '<p class="comment-form-' . esc_attr( $key ) . '">';
$field_html .= '<label for="' . esc_attr( $key ) . '">' . esc_html( $field['label'] );
if ( $field['required'] ) {
$field_html .= ' <span class="required">*</span>';
}
$field_html .= '</label><input id="' . esc_attr( $key ) . '" name="' . esc_attr( $key ) . '" type="' . esc_attr( $field['type'] ) . '" autocomplete="' . esc_attr( $field['autocomplete'] ) . '" value="' . esc_attr( $field['value'] ) . '" size="30" ' . ( $field['required'] ? 'required' : '' ) . ' /></p>';
$comment_form['fields'][ $key ] = $field_html;
}
$account_page_url = wc_get_page_permalink( 'myaccount' );
if ( $account_page_url ) {
/* translators: %s opening and closing link tags respectively */
$comment_form['must_log_in'] = '<p class="must-log-in">' . sprintf( esc_html__( 'You must be %1$slogged in%2$s to post a review.', 'woocommerce' ), '<a href="' . esc_url( $account_page_url ) . '">', '</a>' ) . '</p>';
}
if ( wc_review_ratings_enabled() ) {
$comment_form['comment_field'] = '<div class="comment-form-rating"><label for="rating" id="comment-form-rating-label">' . esc_html__( 'Your rating', 'woocommerce' ) . ( wc_review_ratings_required() ? ' <span class="required">*</span>' : '' ) . '</label><select name="rating" id="rating" required>
<option value="">' . esc_html__( 'Rate…', 'woocommerce' ) . '</option>
<option value="5">' . esc_html__( 'Perfect', 'woocommerce' ) . '</option>
<option value="4">' . esc_html__( 'Good', 'woocommerce' ) . '</option>
<option value="3">' . esc_html__( 'Average', 'woocommerce' ) . '</option>
<option value="2">' . esc_html__( 'Not that bad', 'woocommerce' ) . '</option>
<option value="1">' . esc_html__( 'Very poor', 'woocommerce' ) . '</option>
</select></div>';
}
$comment_form['comment_field'] .= '<p class="comment-form-comment"><label for="comment">' . esc_html__( 'Your review', 'woocommerce' ) . ' <span class="required">*</span></label><textarea id="comment" name="comment" cols="45" rows="8" required></textarea></p>';
comment_form( apply_filters( 'woocommerce_product_review_comment_form_args', $comment_form ) );
?>
</div>
</div>
<?php else : ?>
<p class="woocommerce-verification-required"><?php esc_html_e( 'Only logged in customers who have purchased this product may leave a review.', 'woocommerce' ); ?></p>
<?php endif; ?>
<div class="clear"></div>
AssistantCopy response I'll help you create a comprehensive solution for WooCommerce reviews with sorting, filtering, AJAX pagination, and like/dislike functionality. This will require several files working together.
1. Modified Template File (single-product-reviews.php)
<?php
/**
* Display single product reviews (comments) with enhanced features
*/
defined( 'ABSPATH' ) || exit;
global $product;
if ( ! comments_open() ) {
return;
}
$product_id = $product->get_id();
?>
<div id="reviews" class="woocommerce-Reviews">
<div id="comments">
<h2 class="woocommerce-Reviews-title">
<?php
$count = $product->get_review_count();
if ( $count && wc_review_ratings_enabled() ) {
$reviews_title = sprintf( esc_html( _n( '%1$s review for %2$s', '%1$s reviews for %2$s', $count, 'woocommerce' ) ), esc_html( $count ), '<span>' . get_the_title() . '</span>' );
echo apply_filters( 'woocommerce_reviews_title', $reviews_title, $count, $product );
} else {
esc_html_e( 'Reviews', 'woocommerce' );
}
?>
</h2>
<?php if ( have_comments() ) : ?>
<!-- Review Controls -->
<div class="review-controls">
<div class="review-controls-row">
<div class="sort-control">
<label for="review-sort"><?php esc_html_e( 'Sort by:', 'woocommerce' ); ?></label>
<select id="review-sort" name="review_sort">
<option value="latest"><?php esc_html_e( 'Latest', 'woocommerce' ); ?></option>
<option value="oldest"><?php esc_html_e( 'Oldest', 'woocommerce' ); ?></option>
<option value="highest_rating"><?php esc_html_e( 'Highest Rating', 'woocommerce' ); ?></option>
<option value="lowest_rating"><?php esc_html_e( 'Lowest Rating', 'woocommerce' ); ?></option>
</select>
</div>
<div class="filter-control">
<label for="review-filter"><?php esc_html_e( 'Filter by Rating:', 'woocommerce' ); ?></label>
<select id="review-filter" name="review_filter">
<option value=""><?php esc_html_e( 'All Ratings', 'woocommerce' ); ?></option>
<option value="5"><?php esc_html_e( '5 Stars', 'woocommerce' ); ?></option>
<option value="4"><?php esc_html_e( '4 Stars', 'woocommerce' ); ?></option>
<option value="3"><?php esc_html_e( '3 Stars', 'woocommerce' ); ?></option>
<option value="2"><?php esc_html_e( '2 Stars', 'woocommerce' ); ?></option>
<option value="1"><?php esc_html_e( '1 Star', 'woocommerce' ); ?></option>
</select>
</div>
</div>
</div>
<!-- Reviews Container -->
<div id="reviews-container">
<div class="reviews-loading" style="display: none;">
<p><?php esc_html_e( 'Loading reviews...', 'woocommerce' ); ?></p>
</div>
<ol class="commentlist" id="review-list">
<?php wp_list_comments( apply_filters( 'woocommerce_product_review_list_args', array( 'callback' => 'enhanced_woocommerce_comments' ) ) ); ?>
</ol>
<!-- AJAX Pagination -->
<div id="review-pagination">
<?php
if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) :
echo '<nav class="woocommerce-pagination review-pagination-ajax">';
paginate_comments_links(
apply_filters(
'woocommerce_comment_pagination_args',
array(
'prev_text' => is_rtl() ? '→' : '←',
'next_text' => is_rtl() ? '←' : '→',
'type' => 'list',
)
)
);
echo '</nav>';
endif;
?>
</div>
</div>
<?php else : ?>
<div id="reviews-container">
<p class="woocommerce-noreviews"><?php esc_html_e( 'There are no reviews yet.', 'woocommerce' ); ?></p>
</div>
<?php endif; ?>
</div>
<!-- Review Form (unchanged) -->
<?php if ( get_option( 'woocommerce_review_rating_verification_required' ) === 'no' || wc_customer_bought_product( '', get_current_user_id(), $product->get_id() ) ) : ?>
<div id="review_form_wrapper">
<div id="review_form">
<?php
$commenter = wp_get_current_commenter();
$comment_form = array(
'title_reply' => have_comments() ? esc_html__( 'Add a review', 'woocommerce' ) : sprintf( esc_html__( 'Be the first to review “%s”', 'woocommerce' ), get_the_title() ),
'title_reply_to' => esc_html__( 'Leave a Reply to %s', 'woocommerce' ),
'title_reply_before' => '<span id="reply-title" class="comment-reply-title" role="heading" aria-level="3">',
'title_reply_after' => '</span>',
'comment_notes_after' => '',
'label_submit' => esc_html__( 'Submit', 'woocommerce' ),
'logged_in_as' => '',
'comment_field' => '',
);
$name_email_required = (bool) get_option( 'require_name_email', 1 );
$fields = array(
'author' => array(
'label' => __( 'Name', 'woocommerce' ),
'type' => 'text',
'value' => $commenter['comment_author'],
'required' => $name_email_required,
'autocomplete' => 'name',
),
'email' => array(
'label' => __( 'Email', 'woocommerce' ),
'type' => 'email',
'value' => $commenter['comment_author_email'],
'required' => $name_email_required,
'autocomplete' => 'email',
),
);
$comment_form['fields'] = array();
foreach ( $fields as $key => $field ) {
$field_html = '<p class="comment-form-' . esc_attr( $key ) . '">';
$field_html .= '<label for="' . esc_attr( $key ) . '">' . esc_html( $field['label'] );
if ( $field['required'] ) {
$field_html .= ' <span class="required">*</span>';
}
$field_html .= '</label><input id="' . esc_attr( $key ) . '" name="' . esc_attr( $key ) . '" type="' . esc_attr( $field['type'] ) . '" autocomplete="' . esc_attr( $field['autocomplete'] ) . '" value="' . esc_attr( $field['value'] ) . '" size="30" ' . ( $field['required'] ? 'required' : '' ) . ' /></p>';
$comment_form['fields'][ $key ] = $field_html;
}
$account_page_url = wc_get_page_permalink( 'myaccount' );
if ( $account_page_url ) {
$comment_form['must_log_in'] = '<p class="must-log-in">' . sprintf( esc_html__( 'You must be %1$slogged in%2$s to post a review.', 'woocommerce' ), '<a href="' . esc_url( $account_page_url ) . '">', '</a>' ) . '</p>';
}
if ( wc_review_ratings_enabled() ) {
$comment_form['comment_field'] = '<div class="comment-form-rating"><label for="rating" id="comment-form-rating-label">' . esc_html__( 'Your rating', 'woocommerce' ) . ( wc_review_ratings_required() ? ' <span class="required">*</span>' : '' ) . '</label><select name="rating" id="rating" required>
<option value="">' . esc_html__( 'Rate…', 'woocommerce' ) . '</option>
<option value="5">' . esc_html__( 'Perfect', 'woocommerce' ) . '</option>
<option value="4">' . esc_html__( 'Good', 'woocommerce' ) . '</option>
<option value="3">' . esc_html__( 'Average', 'woocommerce' ) . '</option>
<option value="2">' . esc_html__( 'Not that bad', 'woocommerce' ) . '</option>
<option value="1">' . esc_html__( 'Very poor', 'woocommerce' ) . '</option>
</select></div>';
}
$comment_form['comment_field'] .= '<p class="comment-form-comment"><label for="comment">' . esc_html__( 'Your review', 'woocommerce' ) . ' <span class="required">*</span></label><textarea id="comment" name="comment" cols="45" rows="8" required></textarea></p>';
comment_form( apply_filters( 'woocommerce_product_review_comment_form_args', $comment_form ) );
?>
</div>
</div>
<?php else : ?>
<p class="woocommerce-verification-required"><?php esc_html_e( 'Only logged in customers who have purchased this product may leave a review.', 'woocommerce' ); ?></p>
<?php endif; ?>
<div class="clear"></div>
</div>
<script type="text/javascript">
var review_ajax_params = {
ajax_url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
product_id: <?php echo $product_id; ?>,
nonce: '<?php echo wp_create_nonce( 'review_ajax_nonce' ); ?>'
};
</script>
Copy first response
Topics
2. Enhanced Comment Callback Function Add this to your theme's functions.php:
<?php
// Enhanced comment callback with like/dislike functionality
function enhanced_woocommerce_comments( $comment, $args, $depth ) {
if ( 'div' === $args['style'] ) {
$tag = 'div';
$add_below = 'comment';
} else {
$tag = 'li';
$add_below = 'div-comment';
}
?>
<<?php echo esc_attr( $tag ); ?> <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?> id="comment-<?php comment_ID(); ?>">
<div id="div-comment-<?php comment_ID(); ?>" class="comment_container">
<?php
/**
* The woocommerce_review_before hook
*/
do_action( 'woocommerce_review_before', $comment );
?>
<div class="comment-text">
<?php
/**
* The woocommerce_review_before_comment_meta hook.
*/
do_action( 'woocommerce_review_before_comment_meta', $comment );
if ( $rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) ) ) {
echo wc_get_rating_html( $rating );
}
/**
* The woocommerce_review_meta hook.
*/
do_action( 'woocommerce_review_meta', $comment );
do_action( 'woocommerce_review_before_comment_text', $comment );
/**
* The woocommerce_review_comment_text hook
*/
do_action( 'woocommerce_review_comment_text', $comment );
do_action( 'woocommerce_review_after_comment_text', $comment );
?>
</div>
<!-- Like/Dislike Buttons -->
<div class="review-feedback">
<?php
$comment_id = $comment->comment_ID;
$user_id = get_current_user_id();
$likes = get_comment_meta( $comment_id, 'review_likes', true );
$dislikes = get_comment_meta( $comment_id, 'review_dislikes', true );
$likes = $likes ? count( $likes ) : 0;
$dislikes = $dislikes ? count( $dislikes ) : 0;
$user_liked = false;
$user_disliked = false;
if ( $user_id ) {
$user_likes = get_comment_meta( $comment_id, 'review_likes', true );
$user_dislikes = get_comment_meta( $comment_id, 'review_dislikes', true );
$user_liked = $user_likes && in_array( $user_id, $user_likes );
$user_disliked = $user_dislikes && in_array( $user_id, $user_dislikes );
}
?>
<div class="review-feedback-buttons">
<button type="button" class="review-like-btn <?php echo $user_liked ? 'active' : ''; ?>"
data-comment-id="<?php echo $comment_id; ?>" data-action="like">
<span class="like-icon">👍</span>
<span class="like-count"><?php echo $likes; ?></span>
</button>
<button type="button" class="review-dislike-btn <?php echo $user_disliked ? 'active' : ''; ?>"
data-comment-id="<?php echo $comment_id; ?>" data-action="dislike">
<span class="dislike-icon">👎</span>
<span class="dislike-count"><?php echo $dislikes; ?></span>
</button>
</div>
</div>
</div>
<?php
}
// Create database table for review feedback on theme activation
function create_review_feedback_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'review_feedback';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
comment_id bigint(20) NOT NULL,
user_id bigint(20) NOT NULL,
feedback_type varchar(10) NOT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY unique_feedback (comment_id, user_id),
KEY comment_id (comment_id),
KEY user_id (user_id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
// Hook to create table
add_action( 'after_switch_theme', 'create_review_feedback_table' );
3. AJAX Handler Functions Add these to your theme's functions.php:
<?php
// AJAX handler for loading reviews
add_action( 'wp_ajax_load_reviews', 'handle_load_reviews_ajax' );
add_action( 'wp_ajax_nopriv_load_reviews', 'handle_load_reviews_ajax' );
function handle_load_reviews_ajax() {
// Verify nonce
if ( ! wp_verify_nonce( $_POST['nonce'], 'review_ajax_nonce' ) ) {
wp_die( 'Security check failed' );
}
$product_id = intval( $_POST['product_id'] );
$sort_by = sanitize_text_field( $_POST['sort_by'] );
$filter_rating = intval( $_POST['filter_rating'] );
$paged = intval( $_POST['paged'] );
$args = array(
'post_id' => $product_id,
'status' => 'approve',
'type' => 'review',
'number' => get_option( 'comments_per_page', 10 ),
'offset' => ( $paged - 1 ) * get_option( 'comments_per_page', 10 ),
);
// Handle sorting
switch ( $sort_by ) {
case 'oldest':
$args['orderby'] = 'comment_date';
$args['order'] = 'ASC';
break;
case 'highest_rating':
$args['meta_query'] = array(
array(
'key' => 'rating',
'compare' => 'EXISTS',
),
);
$args['orderby'] = 'meta_value_num';
$args['meta_key'] = 'rating';
$args['order'] = 'DESC';
break;
case 'lowest_rating':
$args['meta_query'] = array(
array(
'key' => 'rating',
'compare' => 'EXISTS',
),
);
$args['orderby'] = 'meta_value_num';
$args['meta_key'] = 'rating';
$args['order'] = 'ASC';
break;
default: // latest
$args['orderby'] = 'comment_date';
$args['order'] = 'DESC';
break;
}
// Handle rating filter
if ( $filter_rating ) {
$args['meta_query'] = array(
array(
'key' => 'rating',
'value' => $filter_rating,
'compare' => '=',
),
);
}
$comments = get_comments( $args );
// Get total count for pagination
$total_args = $args;
unset( $total_args['number'] );
unset( $total_args['offset'] );
$total_comments = get_comments( array_merge( $total_args, array( 'count' => true ) ) );
ob_start();
if ( $comments ) {
echo '<ol class="commentlist">';
wp_list_comments( array( 'callback' => 'enhanced_woocommerce_comments' ), $comments );
echo '</ol>';
// Generate pagination
$total_pages = ceil( $total_comments / get_option( 'comments_per_page', 10 ) );
if ( $total_pages > 1 ) {
echo '<nav class="woocommerce-pagination review-pagination-ajax">';
echo '<ul class="page-numbers">';
for ( $i = 1; $i <= $total_pages; $i++ ) {
$class = ( $i == $paged ) ? 'current' : '';
echo '<li><a href="#" class="page-numbers ' . $class . '" data-page="' . $i . '">' . $i . '</a></li>';
}
echo '</ul>';
echo '</nav>';
}
} else {
echo '<p>' . esc_html__( 'No reviews found.', 'woocommerce' ) . '</p>';
}
$output = ob_get_clean();
wp_send_json_success( array(
'html' => $output,
'total' => $total_comments
) );
}
// AJAX handler for review feedback (like/dislike)
add_action( 'wp_ajax_review_feedback', 'handle_review_feedback_ajax' );
add_action( 'wp_ajax_nopriv_review_feedback', 'handle_review_feedback_ajax' );
function handle_review_feedback_ajax() {
// Verify nonce
if ( ! wp_verify_nonce( $_POST['nonce'], 'review_ajax_nonce' ) ) {
wp_die( 'Security check failed' );
}
$comment_id = intval( $_POST['comment_id'] );
$action = sanitize_text_field( $_POST['action_type'] );
$user_id = get_current_user_id();
if ( ! $user_id ) {
wp_send_json_error( array( 'message' => 'You must be logged in to vote.' ) );
}
// Get current likes and dislikes
$likes = get_comment_meta( $comment_id, 'review_likes', true );
$dislikes = get_comment_meta( $comment_id, 'review_dislikes', true );
if ( ! is_array( $likes ) ) $likes = array();
if ( ! is_array( $dislikes ) ) $dislikes = array();
$user_liked = in_array( $user_id, $likes );
$user_disliked = in_array( $user_id, $dislikes );
if ( $action === 'like' ) {
if ( $user_liked ) {
// Remove like
$likes = array_diff( $likes, array( $user_id ) );
} else {
// Add like and remove dislike if exists
$likes[] = $user_id;
$dislikes = array_diff( $dislikes, array( $user_id ) );
}
} elseif ( $action === 'dislike' ) {
if ( $user_disliked ) {
// Remove dislike
$dislikes = array_diff( $dislikes, array( $user_id ) );
} else {
// Add dislike and remove like if exists
$dislikes[] = $user_id;
$likes = array_diff( $likes, array( $user_id ) );
}
}
// Update comment meta
update_comment_meta( $comment_id, 'review_likes', array_values( $likes ) );
update_comment_meta( $comment_id, 'review_dislikes', array_values( $dislikes ) );
wp_send_json_success( array(
'likes' => count( $likes ),
'dislikes' => count( $dislikes ),
'user_liked' => in_array( $user_id, $likes ),
'user_disliked' => in_array( $user_id, $dislikes )
) );
}
4. JavaScript for AJAX Functionality Create a file review-ajax.js and enqueue it:
jQuery(document).ready(function($) {
// Handle sort and filter changes
$('#review-sort, #review-filter').on('change', function() {
loadReviews(1);
});
// Handle pagination clicks
$(document).on('click', '.review-pagination-ajax .page-numbers', function(e) {
e.preventDefault();
var page = $(this).data('page');
if (page) {
loadReviews(page);
}
});
// Handle like/dislike clicks
$(document).on('click', '.review-like-btn, .review-dislike-btn', function(e) {
e.preventDefault();
if (!review_ajax_params.user_logged_in) {
alert('You must be logged in to vote on reviews.');
return;
}
var $button = $(this);
var commentId = $button.data('comment-id');
var actionType = $button.data('action');
$button.prop('disabled', true);
$.ajax({
url: review_ajax_params.ajax_url,
type: 'POST',
data: {
action: 'review_feedback',
comment_id: commentId,
action_type: actionType,
nonce: review_ajax_params.nonce
},
success: function(response) {
if (response.success) {
var $container = $button.closest('.review-feedback-buttons');
var $likeBtn = $container.find('.review-like-btn');
var $dislikeBtn = $container.find('.review-dislike-btn');
// Update counts
$likeBtn.find('.like-count').text(response.data.likes);
$dislikeBtn.find('.dislike-count').text(response.data.dislikes);
// Update active states
$likeBtn.toggleClass('active', response.data.user_liked);
$dislikeBtn.toggleClass('active', response.data.user_disliked);
} else {
alert(response.data.message || 'An error occurred');
}
},
error: function() {
alert('An error occurred while processing your vote.');
},
complete: function() {
$button.prop('disabled', false);
}
});
});
// Function to load reviews via AJAX
function loadReviews(page) {
var sortBy = $('#review-sort').val();
var filterRating = $('#review-filter').val();
$('.reviews-loading').show();
$('#review-list').html('');
$('#review-pagination').html('');
$.ajax({
url: review_ajax_params.ajax_url,
type: 'POST',
data: {
action: 'load_reviews',
product_id: review_ajax_params.product_id,
sort_by: sortBy,
filter_rating: filterRating,
paged: page || 1,
nonce: review_ajax_params.nonce
},
success: function(response) {
if (response.success) {
$('#reviews-container').html(response.data.html);
} else {
$('#reviews-container').html('<p>Error loading reviews.</p>');
}
},
error: function() {
$('#reviews-container').html('<p>Error loading reviews.</p>');
},
complete: function() {
$('.reviews-loading').hide();
}
});
}
});
5. Enqueue Scripts and Styles Add this to your functions.php:
<?php
// Enqueue scripts and styles for reviews
function enqueue_review_scripts() {
if (is_product()) {
wp_enqueue_script(
'review-ajax',
get_template_directory_uri() . '/js/review-ajax.js',
array('jquery'),
'1.0.0',
true
);
wp_localize_script('review-ajax', 'review_ajax_params', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('review_ajax_nonce'),
'user_logged_in' => is_user_logged_in()
));
wp_enqueue_style(
'review-styles',
get_template_directory_uri() . '/css/review-styles.css',
array(),
'1.0.0'
);
}
}
add_action('wp_enqueue_scripts', 'enqueue_review_scripts');
6. CSS Styles (review-styles.css) /* Review Controls */
.review-controls {
margin-bottom: 20px;
padding: 15px;
background-color: #f8f9fa;
border-radius: 5px;
}
.review-controls-row {
display: flex;
gap: 20px;
flex-wrap: wrap;
}
.sort-control,
.filter-control {
display: flex;
align-items: center;
gap: 10px;
}
.sort-control label,
.filter-control label {
font-weight: 600;
margin: 0;
}
.sort-control select,
.filter-control select {
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 4px;
background-color: white;
}
/* Review Feedback */
.review-feedback {
margin-top: 15px;
padding-top: 15px;
border-top: 1px solid #eee;
}
.review-feedback-buttons {
display: flex;
gap: 10px;
}
.review-like-btn,
.review-dislike-btn {
display: flex;
align-items: center;
gap: 5px;
padding: 8px 12px;
border: 1px solid #ddd;
background-color: white;
border-radius: 4px;
cursor: pointer;
transition: all 0.3s ease;
}
.review-like-btn:hover {
background-color: #e8f5e8;
border-color: #28a745;
}
.review-dislike-btn:hover {
background-color: #fde8e8;
border-color: #dc3545;
}
.review-like-btn.active {
background-color: #28a745;
color: white;
border-color: #28a745;
}
.review-dislike-btn.active {
background-color: #dc3545;
color: white;
border-color: #dc3545;
}
.review-like-btn:disabled,
.review-dislike-btn:disabled {
opacity: 0.6;
cursor: not-allowed;
}
/* Loading State */
.reviews-loading {
text-align: center;
padding: 20px;
color: #666;
}
/* AJAX Pagination */
.review-pagination-ajax .page-numbers {
cursor: pointer;
}
.review-pagination-ajax .page-numbers.current {
background-color: #007cba;
color: white;
}
/* Responsive Design */
@media (max-width: 768px) {
.review-controls-row {
flex-direction: column;
}
.sort-control,
.filter-control {
width: 100%;
}
.review-feedback-buttons {
justify-content: center;
}
}
Installation Instructions:
Template File : Place the modified single-product-reviews.php in your theme's woocommerce folder
Functions : Add all the PHP functions to your theme's functions.php
JavaScript : Create review-ajax.js in your theme's js folder
CSS : Create review-styles.css in your theme's css folder
Database : The review feedback table will be created automatically when you switch themes
Features Included: ✅ Sort By : Latest, Oldest, Highest Rating, Lowest Rating
✅ Filter by Rating : 1-5 stars filtering
✅ AJAX Pagination : No page reloads
✅ Like/Dislike System : User feedback on reviews
✅ User Authentication : Only logged-in users can vote
✅ Responsive Design : Mobile-friendly interface
✅ Database Optimization : Efficient queries and caching
This solution provides a complete enhancement to WooCommerce reviews with modern UX patterns and functionality.