Skip Navigation

[Resolved] If statement to display custom field or text

This support ticket is created 3 years, 9 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.

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by mikeS-30 3 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#1690115
Screen Shot 2020-07-07 at 9.56.15 AM.png

Tell us what you are trying to do? I have custom fields set up for a weekly quota, sales for a specific week and the difference. I already have a formula that subtracts weekly quota from sales and updates the difference custom field. At that point, I want to display the difference (as seen in the red text on the attachment) until they surpass the weekly quota. At that point, I simply want to display some text that says, "Great job!" or something like that. My code is as follows:

/** 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';
$sum_field3 = 'difference';
// if a specific form
if ( $form_data['id'] == 34183 || $form_data['id'] == 34200 )
{
// Sales
$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 );

//Quota
$weekly_quota = get_user_meta($post_id, 'wpcf-weekly-quota',true);

$weekly_difference = $weekly_quota - $wk1;
if ($weekly_difference > $weekly_quota) {
update_user_meta("Great Job! Add more sales for more changes to win!");
} else {
update_user_meta( $post_id, 'wpcf-' . $sum_field3, $weekly_difference );
}
}
}

Is there any documentation that you are following? Building off a previous ticket - https://toolset.com/forums/topic/multiple-equations-adding-custom-fields-to-populate-different-custom-fields/

Is there a similar example that we can see?

What is the link to your site? hidden link

#1690743

Hi, I'm not quite clear what you're asking about. There's a considerable amount of code here and I'm not sure exactly where it's breaking down. I can see that this update_user_meta doesn't specify which field to update:

update_user_meta("Great Job! Add more sales for more changes to win!");

But beyond that, your ticket title indicates you want some help with the "if" statement. For week 1, it seems like you would want to know if the weekly sales for week 1 was greater than or equal to the weekly quota:

if ($wk1 >= $weekly_quota) {
// great job! you met or exceeded the weekly quota for week 1
update_user_meta("Great Job! Add more sales for more changes to win!");
} else {
update_user_meta( $post_id, 'wpcf-' . $sum_field3, $weekly_difference );
}

Again, that first update_user_meta doesn't specify which field to update, so I'm not clear what you want to achieve here. Could you provide more details?

Also, it will be helpful to add some error log messages to pinpoint where this code is failing. If you're not familiar with error logs, I can show you how to turn them on temporarily. Go in your wp-config.php file and look for

define('WP_DEBUG', false);

Change it to:

define('WP_DEBUG', true);

Then add these lines, just after the WP_DEBUG line:

define('WP_DEBUG_LOG', dirname(__FILE__) . '/error_log.txt');
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
define('WP_DISABLE_FATAL_ERROR_HANDLER',true);

Then you can add error_log statements in your code to print to the error log file at error_log.txt file in your site's root directory.

$contest_total = $wk1 + $wk2 + $wk3 + $wk4 + $wk5 + $wk6 + $wk7 + $wk8;
error_log('contest_total: ' . $contest_total);
// ...
$weekly_quota = get_user_meta($post_id, 'wpcf-weekly-quota',true);
error_log('weekly_quota: ' . $weekly_quota);

After you submit the form, you should be able to see the error log at yoursite.com/error_log.txt, and click "refresh" to see it update as you submit more tests. This will help you debug your code.

#1691745
Screen Shot 2020-07-08 at 10.41.05 AM.png

Thanks Christian! I really appreciate the help.

A little more detail on what I'm trying to accomplish. I have a CRED form that will update a user's custom field for their weekly sales. When the user enters their sales via the CRED form, the code I'm providing subtracts the user's weekly quota (custom field) from the users weekly sales to total the difference (custom field updated with CRED form). All that works no problem but once the user enters sales above their weekly quota, it then goes into a negative number (See attached screenshot). To avoid that, I was trying to write an if statement and I think your input helps. I was not defining the field that I want to be updated when the difference is equal to or greater than the weekly quota. The custom field I want to change will be wpcf-difference. I revised my code to update that field but it didn't work so I am still messing something up. The revised code is as follows:

/** 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';
$sum_field3 = 'difference';
// if a specific form
if ( $form_data['id'] == 34183 || $form_data['id'] == 34200 )
{
// Sales
$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 );

//Quota
$weekly_quota = get_user_meta($post_id, 'wpcf-weekly-quota',true);
$quotacomplete = "Great Job! Add more sales for more changes to win!";

$weekly_difference = $weekly_quota - $wk1;
if ($weekly_difference >= $weekly_quota) {
update_user_meta( $post_id, 'wpcf-' . $sum_field3, $quotacomplete );
} else {
update_user_meta( $post_id, 'wpcf-' . $sum_field3, $weekly_difference );
}
}
}

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

Thank you!!

#1692155

Okay that makes sense. I'm not able to guess what the problem is, unfortunately. You'll need to add debugging to your code to find out more. Please refer to the previous comment for instructions to enable logs. Insert these 4 error_log statements to provide some insight (I didn't make any other changes, I just added the 4 error_log lines):

...
$weekly_quota = get_user_meta($post_id, 'wpcf-weekly-quota',true);
error_log('user id: ' . $post_id);
error_log('weekly quota: ' . $weekly_quota);
$quotacomplete = "Great Job! Add more sales for more changes to win!";

$weekly_difference = $weekly_quota - $wk1;
error_log('wk1: ' . $wk1);
error_log('weekly difference: ' . $weekly_difference);
...

Save the code, submit the Form, then the log should be available here: hidden link

Please let me know when you've made those changes, and we can discuss the results.

#1695187
Screen Shot 2020-07-09 at 5.01.35 PM.png
Screen Shot 2020-07-09 at 4.57.47 PM.png

Thank you for your reply! I added that code to wp-config.php but the file wasn't added to the server. My host is WP Engine and they have error logs so maybe they are preventing generating dual error logs? Regardless, I checked the WP Engine error logs and came up with the attached screenshot. I'm also including a screenshot to my functions.php to make sure I didn't mess that up. Thank you!!!

#1696243

Okay the weekly difference is negative, which means the user met and exceeded their weekly quota. In this case, you would like to set the value of the custom field to be $quotacomplete, but that is not happening. Like you mentioned, the problem is this conditional is false:

if ($weekly_difference >= $weekly_quota) {

The conditional should be changed to test whether or not the $weekly_difference is less than or equal to zero. If so, it's a negative number, and $quotacomplete should be displayed:

if ($weekly_difference <= 0) {

The else statement is still okay. If the number is positive, you want to display that weekly difference number. Right?

#1696957

My issue is resolved now. Thank you!

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