Dear Toolset team,
you supported me already by setting up a rating system where users can rate a company – this works perfectly fine – thank you for this!
Now, additionally I want to integrate to the existing company rating form a rating to rate the employer. So the idea is that a user simply can fill in in the known company rating form besides the company rating the employer rating. So for achieving this I added to the existing CPT “company” two fields “employer-ratings-average” and “employer-ratings-total” and to the CPT company-review the field “employer-rating”. The function.php code I enhanced by this new fields, see here:
/**
* Route child employer ratings to parent ratings
*/
function update_company_review($post_id, $form_data) {
// if specific form, change ID to the CRED "Review" ID
if ($form_data['id'] == 50) {
// Get ID of Company Being Reviewed
$parent_post = $_POST['@company-company-review_parent'];
$reviews = toolset_get_related_posts($parent_post,'company-company-review','parent',999,0,array(),'post_object','child');
$sum = 0;
$num = 0;
foreach ($reviews as $review) {
$ratings = get_post_meta($review->ID, 'wpcf-employer-rating', true);
if ($ratings) {
$sum += $ratings;
$num++;
}
}
$average = ($num > 0) ? $sum / $num : 0;
// Rundung auf die nächste Zehntelstelle (0,1)
$res = round($average, 1);
update_post_meta($parent_post, 'wpcf-employer-ratings-average', $res);
update_post_meta($parent_post, 'wpcf-employer-ratings-total', $num);
}
}
add_action('cred_submit_complete', 'update_company_review', 10, 2);
But when I try to save it it shows me an error. Could you please have a look on it and tell me how to fix this?
Thank you and best wishes
Andreas