The effects of alcohol on sustanon 250 leucine for – real weight loss & bodybuilding benefits?
Prevent other custo...
 
Share:
Notifications
Clear all

Limited Support

Our team is currently on holiday, so support will be limited during this period. Response times may be slower than usual, and some inquiries may be delayed.
We appreciate your patience and understanding, and we’ll resume our usual support by the end of August.

 

Prevent other custom rating fields from reducing the product rating

8 Posts
2 Users
0 Reactions
88 Views
Posts: 9
Topic starter
(@zo-zo-lee)
Active Member
Joined: 2 weeks ago

I found a solution that seems to work with custom code to force woocommerce to look only at my custom rating_product key

/**
 * Force WooCommerce to use only the main 'rating' meta and ignore 'size_fit'
 */
add_filter('woocommerce_product_get_average_rating', 'custom_woocommerce_average_rating', 20, 2);

function custom_woocommerce_average_rating($average, $product) {
    // Get all comments/ratings for the product
    $args = array(
        'post_id' => $product->get_id(),
        'status' => 'approve',
        'meta_key' => 'rating_product', // Only get comments with your custom rating field
    );
    
    $comments = get_comments($args);
    
    if (!$comments) return $average;
    
    $total = 0;
    $count = 0;
    
    foreach ($comments as $comment) {
        $rating = get_comment_meta($comment->comment_ID, 'rating_product', true);
        if ($rating) {
            $total += (float) $rating;
            $count++;
        }
    }
    
    return ($count > 0) ? ($total / $count) : $average;
}
Reply
Page 2 / 2
Share: