Average rating for multiple criterion
-
Hi,
As you suggested in my early query I am able to create multiple criterion for review form. But how to get the average rating (overall rating) of these criterion.Please help me
Regards
HasanThe page I need help with: [log in to see the link]
-
This was answered here (#2): https://www.ads-software.com/support/topic/multi-set-reviews/#post-12350061
- This reply was modified 4 years, 7 months ago by Gemini Labs.
Hi,
I tried as explained there but getting following error
There has been a critical error on your website.Learn more about debugging in WordPress.
I modified the above code as per my requirement and put it in function.php
please check it this method is correctDid you expand the code block and copy/paste exactly as given?
Please put the code here that you attempted to use. Make sure to surround any code you paste here in code tags.
Hi,
please find the code belowadd_filter('site-reviews/config/forms/submission-form', function ($config) { $config['personnel_rating'] = [ 'label' => __('Select a rating for the personnel', 'my-listing'), 'required' => true, 'type' => 'rating', ]; $config['service_rating'] = [ 'label' => __('Select a rating for the service', 'my-listing'), 'required' => true, 'type' => 'rating', ]; return $config; }); /** * Customises the order of the fields used in the Site Reviews submission form. * @param array $order * @return array */ add_filter('site-reviews/submission-form/order', function ($order) { // The $order array contains the field keys returned below. // Simply add any custom field keys that you have added and change them to the desired order. return [ 'rating', 'personnel_rating', // this is a custom field key 'service_rating', // this is a custom field key 'title', 'content', 'name', 'email', 'terms', ]; }); /** * Modifies the submission form field rules * Paste this in your active theme's functions.php file. * @param array $rules * @return array */ add_filter('site-reviews/validation/rules', function ($rules) { $rules['personnel_rating'] = 'required'; $rules['service_rating'] = 'required'; return $rules; }); /** * Triggered immediately after a review is saved * If you are uploading files, this is where you would save them to the database and attach them to the review * Paste this in your active theme's functions.php file. * @param \GeminiLabs\SiteReviews\Review $review * @param \GeminiLabs\SiteReviews\Commands\CreateReview $command * @return void */ add_action('site-reviews/review/created', function ($review, $command) { // You may access the global $_POST and $_FILES here. }, 10, 2); /** * Displays custom fields in the Review's "Details" metabox * Paste this in your active theme's functions.php file. * @param array $metabox * @param \GeminiLabs\SiteReviews\Review $review * @return array */ add_filter('site-reviews/metabox/details', function ($metabox, $review) { $ratingKeys = ['personnel_rating', 'service_rating']; // change these as needed foreach ($review->custom as $key => $value) { if (in_array($key, $ratingKeys)) { $value = glsr_star_rating($value); // render the stars from the rating value } if (is_array($value)) { $value = implode(', ', $value); } $metabox[$key] = $value; } return $metabox; }, 10, 2); /** * Set the default values of the custom fields here * Paste this in your active theme's functions.php file. * @param \GeminiLabs\SiteReviews\Review $review * @return \GeminiLabs\SiteReviews\Review */ add_filter('site-reviews/review/build/before', function ($review) { $review->custom = wp_parse_args($review->custom, [ 'personnel_rating' => 0, 'service_rating' => 0, ]); return $review; }); /** * Manually set the primary rating of the review when it is created. * The rating will be the rounded average of the custom rating fields. * * @param array $values * @return array */ add_filter('site-reviews/create/review-values', function ($values) { $ratings = [ intval(glsr_get($values, 'custom.personnel_rating', 0)), intval(glsr_get($values, 'custom.service_rating', 0)), ]; $ratingsSum = array_sum($ratings); $ratingsCount = count($ratings); $values['rating'] = round($ratingsSum / $ratingsCount); return $values; });
The code you pasted does not cause any critical errors (or any other errors) on my website.
If you are putting this inside your theme’s functions.php file, make sure the PHP file begins with
<?php
- This reply was modified 4 years, 7 months ago by Gemini Labs.
Hi,
It is working but not showing the correct value of rating in my case it is taking the last criterion rating as overall rating.However i was looking average rating of individual criterion and overall rating of all criterion.1. In order for the review rating to reflect the overall rating of your custom rating fields, you need to make sure to hide the rating field in the form as explain here: https://www.ads-software.com/support/topic/multi-set-reviews/#post-12350061
2. The summary will only show the average rating of the main review rating. It is not possible to show the average rating of custom fields in the summary shortcode.
- The topic ‘Average rating for multiple criterion’ is closed to new replies.