Skip Navigation

[Cerrado] creating childs of childs and calculate average ratings

This support ticket is created hace 7 años, 10 meses. There's a good chance that you are reading advice that it now obsolete.

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
- 10:00 – 19:00 10:00 – 19:00 10:00 – 19:00 10:00 – 19:00 10:00 – 19:00 -
- - - - - - -

Supporter timezone: Europe/Madrid (GMT+01:00)

This topic contains 1 respuesta, has 2 mensajes.

Last updated by Caridad hace 7 años, 10 meses.

Assigned support staff: Caridad.

Autor
Mensajes
#292015

hi Forum,

I Need help again.

this is waht I have:

A nice cred Form to Post a Burger 🙂

Image, some Details, Ratings for 10 categorys.
every Rating category is a custom field.

the first Thing I Need is:

a way to add the 10 numeric results to a final number of Points
and store them in a new custom field: Overall-Points.

every category can have numeric values between 0-10
so the Maximum number of Points will be 100.

the second Thing I Need:

once the first Review ist postet. I like to have a button under the Review to add a second Review for this burger. and the result should be under the first Review. so same thing just as a child ?

but here is the tricky Thing.

there should be a General Overall Rating. that Shows the average:

REVIEW 1
Rating Category 1 = 10
Rating Category 2 = 5
Rating Category 2 = 1
Rating Category 2 = 5
...
POINTS 1 = 21

REVIEW 2
Rating Category 1 = 6
Rating Category 2 = 3
Rating Category 2 = 9
Rating Category 2 = 5
...
POINTS 2 = 23

OVERALL REVIEW (average)
Rating Category 1 = 8
Rating Category 2 = 4
Rating Category 2 = 5
Rating Category 2 = 5
...
POINTS 1 = 22

any ideas ?
can some one help?

because using PHP inside the views is not possible.
and it would be nice to store the results in a custom field.
so I can search and sort by this final Points result.

#292456

1) For your first number, total points, you can use a CRED hook called 'cred_save_data':

https://toolset.com/documentation/user-guides/cred-api/#csd

So, if your CRED form ID is 123 you can do something like this:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
// if a specific form
if ($form_data['id']==123) {
 
// get the individual ratings
$rating1 = get_post_meta( $post_id, 'wpcf-rating1', true ); 
$rating2 = get_post_meta( $post_id, 'wpcf-rating2', true ); 
$rating3 = get_post_meta( $post_id, 'wpcf-rating3', true ); 
$rating4 = get_post_meta( $post_id, 'wpcf-rating4', true ); 
$rating5 = get_post_meta( $post_id, 'wpcf-rating5', true ); 
$rating6 = get_post_meta( $post_id, 'wpcf-rating6', true ); 
$rating7 = get_post_meta( $post_id, 'wpcf-rating7', true ); 
$rating8 = get_post_meta( $post_id, 'wpcf-rating8', true ); 
$rating9 = get_post_meta( $post_id, 'wpcf-rating9', true ); 
$rating10 = get_post_meta( $post_id, 'wpcf-rating10', true );

// add them up and save
$total = $rating1+$rating2+$rating3+$rating4+$rating5+$rating6+$rating7+$rating8+$rating9+$rating10; 
update_post_meta($post_id, 'wpcf-total-rating', $total);

}
}

This code is calculating a total for each review.

2) To calculate a grand total you can use a similar approach, but you would store it in the options table instead:

add_action('cred_save_data', 'another_save_data_action',20,2);
function another_save_data_action($post_id, $form_data) {
global $wpdb;

// if a specific form
if ($form_data['id']==123) {
 
// Sum totals
$totals = $wpdb->get_row("SELECT COUNT(*) as num, SUM(meta_value) as total $wpdb->postmeta WHERE meta_key = 'wpcf-total-rating'");

// calculate average
$average = round( $totals->total / $totals->num, 2);

// save
update_option('total-average', $average);

}
}

3) You will need a custom shortcode to display the grand total.
For example:

add_shortcode('total-average', 'total_average_shortcode');
function total_average_shortcode() {
    return get_option( 'total-average' );
}

This will allow you put the following code somewhere:

<h2>Average Ratings: [total-average]</h2>

Please let me know if you are satisfied with my reply and any other questions you may have.

Regards,
Caridad

El debate ‘[Cerrado] creating childs of childs and calculate average ratings’ está cerrado y no admite más respuestas.