• Resolved Brendan Shanahan

    (@brendanshanahan)


    Is there a way with WPDiscuz that I can have multiple rating values on a post (e.g. Quality, Performance, etc) and produce an automatically calculated overall rating. e.g. a 4 and a 5 on the two rating values would give an overall 4.5?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support gVectors Support

    (@gvectorssupport)

    Hi,

    You can create as many rating fields as you wish. More info here: https://wpdiscuz.com/docs/wpdiscuz-7/customization/custom-field-rating/

    ?produce an automatically calculated overall rating. e.g. a 4 and a 5 on the two rating values would give an overall 4.5?

    Regarding this point, wpDiscuz doesn’t have this feature.

    Thread Starter Brendan Shanahan

    (@brendanshanahan)

    Are the rating values available via REST API so that I could build my own total/average rating attribue? If not via API, is there another method you could suggest that I investigate?

    Thanks,

    Brendan

    Thread Starter Brendan Shanahan

    (@brendanshanahan)

    Update – I found the rating field names are prefixed with ‘wpdiscuz_post_rating_’ so I was able to create myself a shortcode using my 3 rating blocks of Range, Performance & Quality.

    Code below for future reference.

    // WPDISCUZ - Create average total rating
    function calculate_average_rating($post_id) {
    // Get the values of the custom fields as decimals
    $range = get_post_meta($post_id, 'wpdiscuz_post_rating_custom_field_66daac035fb55', true);
    $performance = get_post_meta($post_id, 'wpdiscuz_post_rating_custom_field_66daabe060bd9', true);
    $quality = get_post_meta($post_id, 'wpdiscuz_post_rating_custom_field_66daabbb7b585', true);

    // Convert the values to float (to ensure decimal handling)
    $range = floatval($range);
    $performance = floatval($performance);
    $quality = floatval($quality);

    // Calculate the average
    $total_fields = 3;
    $sum = $range + $performance + $quality;
    $average = $sum / $total_fields;

    return $average;
    }
    // Function to return the average rating for the current post
    function display_average_rating() {
    global $post;

    // Get the average rating for the current post
    $average_rating = calculate_average_rating($post->ID);

    // Return the average rating to display in the post content, formatted to 2 decimal places
    return 'Average Rating: ' . number_format($average_rating, 2); // Keep 2 decimal places
    }

    // Register the shortcode [average_rating]
    add_shortcode('average_rating', 'display_average_rating');
    Thread Starter Brendan Shanahan

    (@brendanshanahan)

    If anyone is interested, I expanded this into a shortcode that displays my average rating using the WPDiscuz styling.

    Notes:

    1 – I am not a developer, so use at your own risk. I am just sharing as these forums is where I find a lot of code snippets that help expand my understanding.

    2 – This uses 3 ratings (range, performance & quality) and their meta key. This will need to be updated as required

    3 – All 3 of my ratings are required, so I have simply taken the count (# ratings) from one of these and set this as my value for the average rating. More logic will be requried if you are calculating an average that is not this simply distributed.

    //
    // START | WPDISCUZ - Create average total rating
    //
    function calculate_average_rating($post_id) {
    // Get the values of the custom fields as decimals
    $range = get_post_meta($post_id, 'wpdiscuz_post_rating_custom_field_66daac035fb55', true);
    $performance = get_post_meta($post_id, 'wpdiscuz_post_rating_custom_field_66daabe060bd9', true);
    $quality = get_post_meta($post_id, 'wpdiscuz_post_rating_custom_field_66daabbb7b585', true);

    // Convert the values to float (to ensure decimal handling)
    $range = floatval($range);
    $performance = floatval($performance);
    $quality = floatval($quality);

    // Calculate the average
    $total_fields = 3;
    $sum = $range + $performance + $quality;
    $average = $sum / $total_fields;
    $average = round($average, 1);
    return $average;
    }
    // Function to return the average rating for the current post
    function display_average_rating() {
    global $post;

    // Get the average rating for the current post
    $average_rating = calculate_average_rating($post->ID);

    // Return the average rating to display in the post content, formatted to 2 decimal places
    return 'Average Rating: ' . number_format($average_rating, 1); // Keep 1 decimal places
    }

    // Retrieve the rating count meta value for the current post.
    function get_rating_count() {
    global $post;

    // Ensure we have a valid post object
    if (!$post || !isset($post->ID)) {
    return 0; // Return 0 if the post ID is not available
    }

    // Get the meta value for the key
    $rating_count = get_post_meta($post->ID, 'wpdiscuz_post_rating_count_custom_field_66daabbb7b585', true);

    // Return the rating count or 0 if the meta value is not found
    return $rating_count ? floatval($rating_count) : 0;
    }

    // Function to return the star rating for the current post
    function display_star_rating() {
    global $post;

    if (!$post || !isset($post->ID)) {
    return ''; // Return empty if post ID is not available
    }

    // Get the rating count
    $rating_count = get_rating_count();

    // Get the average rating for the current post
    $average_rating = calculate_average_rating($post->ID);

    // Round the average rating to 1 decimal place
    $average_rating = round($average_rating, 1);

    // Generate star rating HTML
    $full_stars = floor($average_rating); // Number of full stars
    $half_star = ($average_rating - $full_stars) >= 0.5 ? 1 : 0; // Half star if average is >= 0.5
    $empty_stars = 5 - $full_stars - $half_star; // Number of empty stars

    // SVG Star Definitions
    $full_star_svg = '<svg width="26" height="26" viewBox="0 0 24 24" fill="gold" xmlns="https://www.w3.org/2000/svg"><path d="M12 2.5L15.09 8.26L21 9.27L17 14.14L18.18 20.02L12 16.62L5.82 20.02L7 14.14L3 9.27L8.91 8.26L12 2.5Z"/></svg>';
    $empty_star_svg = '<svg width="26" height="26" viewBox="0 0 24 24" fill="lightgray" xmlns="https://www.w3.org/2000/svg"><path d="M12 2.5L15.09 8.26L21 9.27L17 14.14L18.18 20.02L12 16.62L5.82 20.02L7 14.14L3 9.27L8.91 8.26L12 2.5Z"/></svg>';
    $half_star_svg = '<svg width="26" height="26" viewBox="0 0 24 24" fill="gold" xmlns="https://www.w3.org/2000/svg"><path d="M22 9.24l-7.19-.62L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21 12 17.27 18.18 21l-1.63-7.03L22 9.24zM12 15.4V6.1l1.71 4.04 4.38.38-3.32 2.88 1 4.28L12 15.4z"/><path d="M22 9.24l-7.19-.62L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21 12 17.27 18.18 21l-1.63-7.03L22 9.24zM12 15.4V6.1l1.71 4.04 4.38.38-3.32 2.88 1 4.28L12 15.4z" fill="white" clip-rule="evenodd" fill-rule="evenodd" opacity="0.5"/></svg>';


    // Build the star rating HTML
    $html = '<div class="wpd-rating-wrap">';
    $html .= '<div class="wpd-rating-left"></div>';
    $html .= '<div class="wpd-rating-data">';
    $html .= '<div class="wpd-rating-value">';
    $html .= '<span class="wpdrv">' . $average_rating . '</span>';
    $html .= '<span class="wpdrc">' . $rating_count . '</span>';
    $html .= '<span class="wpdrt">reviews</span>';
    $html .= '</div>';
    $html .= '<div class="wpd-rating-title">Average</div>';
    $html .= '<div class="wpd-rating-stars">';
    $html .= str_repeat($full_star_svg, $full_stars); // Full stars
    $html .= str_repeat($half_star_svg, $half_star); // Half star
    $html .= str_repeat($empty_star_svg, $empty_stars); // Empty stars
    $html .= '</div>';
    $html .= '</div>';
    $html .= '<div class="wpd-rating-right"></div>';
    $html .= '</div>';

    // Return the HTML
    return $html;
    }

    // Register the shortcode [star_rating]
    add_shortcode('star_rating', 'display_star_rating');

    //
    // END | WPDISCUZ - Create average total rating
    //
    Plugin Support gVectors Support

    (@gvectorssupport)

    Thank you for taking the time to share the solution. We really appreciate it.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.