Function to display custom star ratings
Mon May 05 2025 02:39:00 GMT+0000 (Coordinated Universal Time)
Saved by
@kevinalanks
// Function to display custom star ratings
function display_custom_star_rating($rating) {
$html = '<div class="star-rating">';
for ($i = 1; $i <= 5; $i++) {
if ($i <= $rating) {
$html .= '<span class="star filled"></span>';
} else {
$html .= '<span class="star"></span>';
}
}
$html .= '</div>';
return $html;
}
// Replace WooCommerce's default star ratings with our custom ones
function custom_woocommerce_review_display_rating() {
global $comment;
$rating = get_comment_meta($comment->comment_ID, 'rating', true);
if ($rating && wc_review_ratings_enabled()) {
echo display_custom_star_rating($rating);
}
}
// Remove WooCommerce's default star display
remove_action('woocommerce_review_before_comment_meta', 'woocommerce_review_display_rating', 10);
// Add our custom star display
add_action('woocommerce_review_before_comment_meta', 'custom_woocommerce_review_display_rating', 10);
content_copyCOPY
Comments