Skip Navigation

[Resolved] Split: Creating a site with rating system – part 2

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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
#2663963

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 );

#2664627

Hi,

I reviewed the code snippet that you shared and noticed that the slugs of the custom fields were not correctly replaced.

You can remove the custom code that was added in response to the previous ticket ( ref: https://toolset.com/forums/topic/creating-a-site-with-rating-system/#post-2662671 ) and replace it with:


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-companyratingstotal',true);
		if(empty($ratings)) {
		$ratings = 1;

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

		}
		update_post_meta( $parent_id, 'wpcf-companyratingsaverage', $avg );
		update_post_meta( $parent_id, 'wpcf-companyratingstotal', $ratings );

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

In case you face any difficulty in saving the code in the active child theme's 'functions.php' file, please check the server's error logs and share any errors or warnings that you see.

Important note: The 1-1 code customization and its troubleshooting is beyond the scope of support that we provide over the forum. We are able to share the examples and pointers and for more personalized assistance, encourage to hire a professional from our list or recommended contractor:
https://toolset.com/contractors/

regards,
Waqar

#2664755

Hi Waqar,

unfortunately did code did not work. But I can understand that such problem resolving goes beyond the support here, so let's close this ticket.

I thank you anyway and wish you a good one
Andreas