Skip Navigation

[Resolved] Creating a site with rating system

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Author
Posts
#2660507

Hello Toolset team,

I want to create a rating system and for testing the functionality I created the same CPT (“company” and “company-review”) and same fields as mentioned in this post:

https://toolset.com/forums/topic/save-child-post-field-average-and-count-to-parent-post-field/

Then I added the following code to my function.php

function update_company_review($post_id, $form_data) {
// if specific form, change ID to the CRED "Review" ID
if ($form_data['id'] == 722) {
// 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-company-rating', true);
if ($ratings) {
$sum += $ratings;
$num++;
}
}
$average = ($num > 0) ? $sum / $num : 0;

// You can keep or adjust the rounding logic as needed
$res = round($average * 2) / 2; // Rounds to nearest 0.5

update_post_meta($parent_post, 'wpcf-company-ratings-average', $res);
update_post_meta($parent_post, 'wpcf-company-ratings-total', $num);
}
}
add_action('cred_submit_complete', 'update_company_review', 10, 2);

After I have done this I added a new rating with link “Create new” but the fields in the parent post Company 1 have not changed. Could it be that I miss another code? I have gone through the related post mentioned in the above link and also trough the links and sublinks there. Now I am not sure what additional code is needed, is it a Form's hook or shortcode …

It would be great if you can support me on this case and I think this is also beneficial for many other users of Toolset.

Thanks in advance and best whishes
Andreas

#2660861

Hi Andreas,

Thank you for contacting us and I'd be happy to assist.

To troubleshoot and suggest the next steps, I'll need to see exactly how this form is set up in the admin area.

Can you please share temporary admin login details, along with the link to the page with this form?

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.

regards,
Waqar

#2661609

Thank you for sharing the access details.

Reviewing the form and the relevant fields, I understand that the form ID and the field slugs used in the custom code snippet are not updated, as per your website.

Can you please replace your custom code snippet with this updated one:
( note the changes in the field slugs and the target form's ID '214' for the form 'Formular für company-reviews' )


function update_company_review($post_id, $form_data) {
	// if specific form, change ID to the CRED "Review" ID
	if ($form_data['id'] == 214) {
		// 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-companyrating', true);
			if ($ratings) {
				$sum += $ratings;
				$num++;
			}
		}
		$average = ($num > 0) ? $sum / $num : 0;
	
		// You can keep or adjust the rounding logic as needed
		$res = round($average * 2) / 2; // Rounds to nearest 0.5

		update_post_meta($parent_post, 'wpcf-companyratingsaverage', $res);
		update_post_meta($parent_post, 'wpcf-companyratingstotal', $num);
	}
}
add_action('cred_submit_complete', 'update_company_review', 10, 2);

I hope this helps and please let me know if you need further assistance.

#2661725

Hi Waqar,

thank you for your advice. I added the code above into function.php but unfortunately nothing changes when adding a new review.

Can you please check again.
Kind regards
Andreas

#2662671

Thanks for writing back.

I recreated the whole set up on my test website and the following code worked as expected, after some further adjustments:


function update_company_review($post_id, $form_data) {
    // if specific form, change ID to the CRED "Review" ID
    if ($form_data['id'] == 214) {
        // 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-companyrating', true);

            if ($ratings) {
                $sum += $ratings;
                $num++;
            }
        }
        $average = ($num > 0) ? $sum / $num : 0;
     
        // You can keep or adjust the rounding logic as needed
        $res = round($average * 2) / 2; // Rounds to nearest 0.5
 
        update_post_meta($parent_post, 'wpcf-companyratingsaverage', $res);
        update_post_meta($parent_post, 'wpcf-companyratingstotal', $sum);
        
    }
}
add_action('cred_submit_complete', 'update_company_review', 10, 2);

This should do the trick.

#2663651

Hi Waqar,

yes this worked well, thank you 🙂

Now I also tried to integrate the review-moderating function that "Company ratings average" and "Company ratings total" only get updated after I approved a new review. Because without this additional function the two fields also get updated before I approve a review. I found in this post

https://toolset.com/forums/topic/parent-child-review-ratings-system/

the necessary code and changed it to my slugs. But I could not even store the code in my function.php. Can you also have a look on this and tell me where the error is?

I tried to add the following code:

function func_on_review_publish($new_status, $old_status, $post) {

if($old_status=='pending' and $new_status=='publish') {

$parent_id = toolset_get_related_post($post->ID,'company-company-review');

// get parent total rating
$ratings = get_post_meta( $parent_id, 'wpcf-company-rating',true);
if(empty($ratings)) {
$ratings = 1;

// get current rating
$avg = get_post_meta($post->ID, 'wpcf-ratings',true);
}else{
$old_ratings = $ratings;
$avg = get_post_meta( $parent_id, 'wpcf-company-ratings-average',true);
$ratings = $ratings+1;
$current_rating = get_post_meta($post->ID, 'wpcf-ratings',true);
$avg = ((($avg*$old_ratings) + $current_rating)/$ratings);

}
update_post_meta( $parent_id, 'wpcf-average-rating', $avg );
update_post_meta( $parent_id, 'wpcf-company-ratings-total', $ratings );

}
}
add_action( 'transition_post_status', 'func_on_review_publish', 10, 3 );

New threads created by Waqar and linked to this one are listed below:

https://toolset.com/forums/topic/split-creating-a-site-with-rating-system-part-2/

#2663965

Thanks for the update and glad that it is working.

For your new question, I've created a separate ticket and will follow up on that shortly.
( ref: https://toolset.com/forums/topic/split-creating-a-site-with-rating-system-part-2/ )

You're welcome to mark this ticket as resolved and start a new one, for each new question or concern.

#2663999

Thank you Waqar for solving my request and your great support!