Skip Navigation

[Resolved] Multiple equations adding custom fields to populate different custom fields

This support ticket is created 3 years, 10 months ago. 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by Shane 3 years, 10 months ago.

Assisted by: Shane.

Author
Posts
#1668335
Screen Shot 2020-06-18 at 9.25.52 AM.png

Tell us what you are trying to do? I currently have code that adds up multiple custom fields and posts the value in another custom field. This equation calculates the yearly total. As a new project, I need to calculate weekly totals using different custom fields without messing up the other addition problem. My code for both equations is below:

/** Toolset custom fields for adding yearly sales **/

add_action('cred_save_data', 'sum_monthly_sales',10,2);
function sum_monthly_sales($post_id, $form_data)
{
$sum_field = 'yearly-sales';
$forms = array( 15813 );
// if a specific form
if ( in_array( $form_data['id'], $forms ) )
{
$value1 = get_post_meta($post_id, 'wpcf-january-sales', true);
$value2 = get_post_meta($post_id, 'wpcf-february-sales', true);
$value3 = get_post_meta($post_id, 'wpcf-march-sales', true);
$value4 = get_post_meta($post_id, 'wpcf-april-sales', true);
$value5 = get_post_meta($post_id, 'wpcf-may-sales', true);
$value6 = get_post_meta($post_id, 'wpcf-june-sales', true);
$value7 = get_post_meta($post_id, 'wpcf-july-sales', true);
$value8 = get_post_meta($post_id, 'wpcf-august-sales', true);
$value9 = get_post_meta($post_id, 'wpcf-september-sales', true);
$value10 = get_post_meta($post_id, 'wpcf-october-sales', true);
$value11 = get_post_meta($post_id, 'wpcf-november-sales', true);
$value12 = get_post_meta($post_id, 'wpcf-december-sales', true);
$res = $value1 + $value2 + $value3 + $value4 + $value5 + $value6 + $value7 + $value8 + $value9 + $value10 + $value11 + $value12;
update_post_meta( $post_id, 'wpcf-' . $sum_field, $res );
}
}

/** END - Toolset custom fields for adding yearly sales **/

/** Toolset custom fields for adding weekly sales**/

add_action('cred_save_data', 'sum_weekly_sales',10,2);
function sum_weekly_sales($post_id, $form_data)
{
$sum_field2 = 'contest-total';
$forms2 = array( 34183 );
// if a specific form
if ( in_array( $form_data['id'], $forms2 ) )
{
// Sales
$wk1 = get_post_meta($post_ID, 'wpcf-july-1-12', true);
$wk2 = get_post_meta($post_ID, 'wpcf-july-13-19', true);
$wk3 = get_post_meta($post_ID, 'wpcf-july-20-26', true);
$wk4 = get_post_meta($post_ID, 'wpcf-july-17-august-2', true);
$wk5 = get_post_meta($post_ID, 'wpcf-august-3-9', true);
$wk6 = get_post_meta($post_ID, 'wpcf-august-10-16', true);
$wk7 = get_post_meta($post_ID, 'wpcf-august-17-23', true);
$wk8 = get_post_meta($post_ID, 'wpcf-august-24-30', true);

$contest_total = $wk1 + $wk2 + $wk3 + $wk4 + $wk5 + $wk6 + $wk7 + $wk8;
update_post_meta( $post_ID, 'wpcf-' . $sum_field2, $contest_total );
}
}

/** END - Toolset custom fields for adding weekly sales **/

Is there any documentation that you are following? I needed help with this previously so this is my previous support post where we figured out the first equation.

https://toolset.com/forums/topic/adding-custom-fields-together-into-custom-field-that-displays-the-total-sum/

Is there a similar example that we can see? hidden link

What is the link to your site? hidden link

#1668737

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Mike,

Thank you for getting in touch.

I'm assuming each of these fields store the values that is relevant to their weeks for e.g
$wk1 = get_post_meta($post_ID, 'wpcf-july-1-12', true);

This field will already have the total of week 1 in it and you're just totalling up the values of the weeks correct?

In this case what you have here should work.

// Sales
$wk1 = get_post_meta($post_ID, 'wpcf-july-1-12', true);
$wk2 = get_post_meta($post_ID, 'wpcf-july-13-19', true);
$wk3 = get_post_meta($post_ID, 'wpcf-july-20-26', true);
$wk4 = get_post_meta($post_ID, 'wpcf-july-17-august-2', true);
$wk5 = get_post_meta($post_ID, 'wpcf-august-3-9', true);
$wk6 = get_post_meta($post_ID, 'wpcf-august-10-16', true);
$wk7 = get_post_meta($post_ID, 'wpcf-august-17-23', true);
$wk8 = get_post_meta($post_ID, 'wpcf-august-24-30', true);

$contest_total = $wk1 + $wk2 + $wk3 + $wk4 + $wk5 + $wk6 + $wk7 + $wk8;
update_post_meta( $post_ID, 'wpcf-' . $sum_field2, $contest_total );

Can you let me know exactly what happens when you run the code ?

Thanks,
Shane

#1668801
Screen Shot 2020-06-18 at 9.06.27 AM.png

Correct, I am simply adding up the weekly totals into a complete contest total sales field. Your code looks identical to mine but I still dropped in your code to make sure. I'm using a CRED form for each week to update the associated weeks custom field. When I run it, the CRED form works properly and updates the weekly sales but the total contest sales field does not update. I noticed a javascript error in console when I run it so I was able to quickly get a screenshot of it (attached).

As mentioned in the first post, I also have a monthly calculation so I thought that this new calculation was causing a conflict. I just wanted to make sure you were aware of that. The plan was also to figure out the difference between the weekly sales and a weekly quota but one thing at a time, right? 🙂

Let me know if you have any other questions to diagnose this.

#1668853

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Mike,

The code is the same as yours.

Would you mind allowing me to have admin access to the site as well as a link to the page where you are running this so that I can test out the code to see whats wrong ?

Thanks,
Shane

#1668983

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Mike,

I see the issue now. The problem is that these are User fields but you are currently using the functions to get post fields. The correct function to use is get_user_meta()

Here is the corrected code.


$wk1 = get_user_meta($post_ID, 'wpcf-july-1-12', true);
$wk2 = get_user_meta($post_ID, 'wpcf-july-13-19', true);
$wk3 = get_user_meta($post_ID, 'wpcf-july-20-26', true);
$wk4 = get_user_meta($post_ID, 'wpcf-july-17-august-2', true);
$wk5 = get_user_meta($post_ID, 'wpcf-august-3-9', true);
$wk6 = get_user_meta($post_ID, 'wpcf-august-10-16', true);
$wk7 = get_user_meta($post_ID, 'wpcf-august-17-23', true);
$wk8 = get_user_meta($post_ID, 'wpcf-august-24-30', true);
 
$contest_total = $wk1 + $wk2 + $wk3 + $wk4 + $wk5 + $wk6 + $wk7 + $wk8;
update_user_meta( $post_ID, 'wpcf-' . $sum_field2, $contest_total );

Please try this and let me know if it helps.
Thanks,
Shane

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.