Home › Toolset Professional Support › [Resolved] Calculated results from repeating fields
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 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10: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/Kolkata (GMT+05:30)
This topic contains 10 replies, has 2 voices.
Last updated by angelaS-2 4 years ago.
Assisted by: Minesh.
Tell us what you are trying to do?
I would like to leverage repeating field data to calculate two values. The repeater group holds a date field and a note field for completed tasks. I would like to extract the date from the most recent entry to add to a field for "Last Completed." Then, I would like to take that date and add a constant from another field (frequency) to calculate the next date that the task should be completed, "Next Scheduled"
Ideally, these calculated values would be stored with the custom post type for other display/reporting needs.
Is there any documentation that you are following?
I've browed the help documents and tickets and can't find anything to help me.
Is there a similar example that we can see?
no
What is the link to your site?
hidden link
Hello. Thank you for contacting the Toolset support.
Can you please tell me the exact flow you will follow, and at what action you want to extract the date field value?
I checked the link you shared: hidden link
- I see a form at bottom of the above page, it seems you are creating a new log entry using the form.
If you can tell me what exactly the flow and what exactly the fields you want to calculate and at what action that would be great and then I will be able to guide you in the right direction. If possible, please share your repeating field group structure as well.
Hi, thank you!
First I will outline the content in question, and then the desired flow.
Real World Behavior:
A task is defined that should be completed on a regular schedule. There's a field to capture the frequency (in days) that the task should be completed. For example, every 90 days. Whenever the crew actually gets around to completing the task, the most recent date of the task is captured, and a future date (in 90 days) is also calculated. This would feed into an "upcoming tasks" view somewhere else in the site.
Toolset post types and fields in question:
Toolset custom post type: asset (all tasks belong to an asset via toolset relationship)
Toolset custom post type: scheduled-task
Custom fields within scheduled-task
-- Next Scheduled Date next-scheduled-date [date] (I want this to be calculated from repeater)
-- Date Last Completed date-last-completed [date] (I want this to be calculated from repeater)
-- Frequency (days) frequency-days [number] (this is defined during task creation)
-- Repeatable Group scheduled-work-comp (these are added over time as work is completed by crew)
----- Repeated Item Task Completed Date task-completed-date [date]
----- Repeated Item Additional Details task-more-details [multiple lines]
Ideal Flow:
1. user creates new scheduled-task via toolset form (new item for post type)
2. user adds a completed task via toolset form (new item for repeated field)
3. System reads most-recent repeater value task-completed-date and assigns this same value to post field data-last-completed
4. System reads most-recent repeater value task-completed-date and adds frequency-days to calculate next-scheduled-date
I need help with flow steps 3 and 4.
The site is in demo mode - feel free to use any of the forms to submit data. Thank you so much!!
Ok - thanks for sharing all required information.
1. user creates new scheduled-task via toolset form (new item for post type)
==>
For what post type? is it for assets?
2. user adds a completed task via toolset form (new item for repeated field)
==>
Ok , where I can see this form? can you please share link of that.
3. System reads most-recent repeater value task-completed-date and assigns this same value to post field data-last-completed
==>
Ok.
4. System reads most-recent repeater value task-completed-date and adds frequency-days to calculate next-scheduled-date
==>
Ok.
So, lets address the issue one by one. Lets first address the issue 3.
Can you please share access details so that I can jump into your site and add the required hooks/code.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
Unfortunately the supplied admin access details is not working at this end.
Can you please send me working admin access details.
I have set the next reply to private which means only you and I have access to it.
3. System reads most-recent repeater value task-completed-date and assigns this same value to post field data-last-completed
==>
For this, I've added the following code to "Custom Code" section offered by Toolset. It will catch the selected date of the last submitted entry and assign the same date to the parent post field "date-last-completed".
- hidden link
add_action('cred_save_data', 'func_update_parent_post_infos',10,2); function func_update_parent_post_infos($post_id, $form_data) { if ($form_data['id']==1407) { update_post_meta($_POST['@scheduled-work-comp_parent'],'wpcf-date-last-completed',$_POST['wpcf-task-completed-date']['timestamp']); } }
Can you please try to add a new entry from the form available at bottom of the page and see if the parent date field is updated automatically.
Minesh - thank you! This is working exactly as I had hoped for this field!
Great - can you please tell me what you what next? what things you want to update.
Thank you! The next part would be flow step 4.
Update this in the same action (on form submission): wpcf-next-scheduled-date = wpcf-date-last-completed + wpcf-frequency-days, where wpcf-frequency-days is an integer value representing number of days to add
I tried to do it by adding a line into your code, but then I got a critical error message on my site so I took the extra line out.
Can you please check now:
I've updated the code as given under to "Custom Code" section offered by Toolset:
add_action('cred_save_data', 'func_update_parent_post_infos',10,2); function func_update_parent_post_infos($post_id, $form_data) { if ($form_data['id']==1407) { update_post_meta($_POST['@scheduled-work-comp_parent'],'wpcf-date-last-completed',$_POST['wpcf-task-completed-date']['timestamp']); // getting the number of days frequency $frequency = get_post_meta($_POST['@scheduled-work-comp_parent'],'wpcf-frequency-days',true); // calculating the next schedule date timestamp $next_schedule_date = $_POST['wpcf-task-completed-date']['timestamp'] + ($frequency * 86400); // updating the next schedule date update_post_meta($_POST['@scheduled-work-comp_parent'],'wpcf-next-scheduled-date',$next_schedule_date); } }
Can you please confirm it works as expected now.
Everything is working perfectly as desired. My issue is resolved. Thank you!!