• Resolved salgraphics

    (@salgraphics)


    Hi,

    I am trying to create a form for monthly reports. The user will check a list of neighbors who paid maintenance service, but there are two costs depending the date of the month they paid, so days 1 to 5 of each month is 35 and after that date (6th day) changes to 40. The form is going to be sent as an email.

    I need the value change automatically on day 6 every month, or to hide / show the fields (checkbox or radio button) depending on the date, like option 1 = 35 (shown day 1 to 5) option 2 = 40 (shown after day 6 )

    I have not been able to add those 50 via a hidden HTML field with a visibility rule. Or hide / show radio boxes depending the date via calculations.

    I have been reading some related posts here but no one fits my needs.

    Sal

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @salgraphics

    I hope you are doing well.

    I made a couple of tests on my lab site and I believe with a small code we can help you here.

    Could you give it a try at

    – Add a hidden field, select “Custom value” and use {custom_day_value} as the default value:

    https://monosnap.com/file/XjDaLNusGK6AFqie9Q42m0pHkSmaGN

    – We will update the value depending on the day of the month example 35 or 40 in your case,

    – So you can use it to make any calculation or visibility rule:

    https://monosnap.com/file/1L4DGSWN7VH9ERwIKaCyPXRZMpOkl7

    For this, add this code as a mu-plugin:

    <?php 
    
    add_action('forminator_custom_form_submit_before_set_fields', 'wpmudev_change_hidden_field_custom', 10, 3);
    function wpmudev_change_hidden_field_custom($entry, $module_id, $field_data_array)
    {
        $form_ids = array(6); // Please change the form ID.
        if (!in_array($module_id, $form_ids)) {
            return;
        }
    
        foreach ($field_data_array as $key => $value) {
            if (strpos($value['name'], 'hidden-1') !== false) {
                Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field(Forminator_CForm_Front_Action::$prepared_data['hidden-1']);
            }
        }
    }
    
    add_filter('forminator_field_hidden_field_value', 'wpmudev_populate_hidden_field_date', 10, 4);
    
    function wpmudev_populate_hidden_field_date($value, $saved_value, $field, $hidden_field)
    {
    
        if ('custom_value' == $saved_value && '{custom_day_value}' == $value) {
            //get a current day of the month
            $date = strtotime(date("m/dY"));
    
            // Check if the current day is less than 6th day of the month
            if (date("d", $date) <= 6) {
                $prepared_data['hidden-1'] = 35; // change the value of hidden field
            } else {
                $prepared_data['hidden-1'] = 40; // change the value of hidden field
            }
        }
    
        return $value;
    }

    Replace the form ID where it says “Please change the form ID”,

    Update the “hidden-1” in the code in case it is a different hidden ID.

    https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    Now it will add the 40 or 35 as a value for the hidden field dynamically depending on the day.

    Let us know if it helps you
    Best Regards
    Patrick Freitas

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @salgraphics

    We haven’t heard from you in a while, I’ll go and mark this thread as resolved. If you have any additional questions or require further help, please let us know!

    Kind Regards,
    Kris

    Thread Starter salgraphics

    (@salgraphics)

    Hi Kris, I have not seen your answers !!! I really appreciate your help here.

    This is a great solution, I’ll apply it right now. I have been doing it via Jscript and CSS hiding / showing the CSS class.

    Let me try this one and I’ll share the results

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Show or Change a field value depending the date’ is closed to new replies.