Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Dynamically set the rating to the average of your custom rating fields.
- * !IMPORTANT: make sure to remove the default rating field in your custom form for this to work.
- * Paste this in your active theme's functions.php file.
- * @param array $values
- * @return array
- */
- add_filter('site-reviews/create/review-values', function ($values, $request) {
- if (!empty($values['rating'])) {
- return $values; // don't set the overall rating if it already has a value
- }
- $fields = (array) get_post_meta($request->request->form, '_fields', true);
- $ratings = [];
- foreach ($fields as $field) {
- if ('rating' === glsr_get($field, 'type')) {
- $ratings[] = glsr_get($values, 'custom.'.glsr_get($field, 'name'), 0);
- }
- }
- $ratings = array_filter($ratings); // remove empty custom rating values;
- if (empty($ratings)) {
- return $value; // don't set the overall rating if there are no custom rating fields
- }
- $ratingsCount = count($ratings);
- $ratingsSum = array_sum($ratings);
- $values['rating'] = round($ratingsSum / $ratingsCount);
- return $values;
- }, 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement