Skip Navigation

[Gelöst] Change select field value on form submit

This support ticket is created vor 7 Jahre, 7 Monate. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

Dieses Thema enthält 6 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von carlosG-8 vor 7 Jahre, 6 Monate.

Assistiert von: Konstantinos Galanakis.

Author
Artikel
#447911

We're trying to update a select field value when we modify a datepicker field. We are using this CRED hook, but it don't work:

Date field slug: data-cancelation
Select field slug: status

/*change status field if product deactivated*/
add_filter('cred_post_expiration_custom_actions', 'change_status_field', 10, 2);
function change_status_field($post_id, $form_data) {
// if a specific form
if ($form_data['id']==131){
if (isset($_POST['wpcf-date-cancelation']['datepicker'])){
update_post_meta($post_id, 'wpcf-status', 3);
}
}
}

Can you help us? Thank you.

#447946

Hello Carlos.

Thank you for contacting the Toolset Support.

Please help me better understand what you are trying to do.

So, you want to update the select field when the datepicker field is modified upon form submission or upon form expiration? How is this datepicker field value modified?

Also, you are using the "cred_post_expiration_custom_actions" filter wrong. See here https://toolset.com/documentation/user-guides/automatic-post-expiration/. $post_id is the second argument while $form_data is the third.

add_filter('cred_post_expiration_custom_actions', 'my_custom_actions', 10, 3);
function my_custom_actions($custom_actions, $post_id, $form_data) {
    $custom_actions[] = array( 'meta_key' => 'advert_active', 'meta_value' =>'false' );
    return $custom_actions;
}

Looking forward to hearing from you soon.

If you have any further issues, please let me know.

Regards

#448734

We are developing an intranet to control the product stock in our company.

We have created a CRED form for the product sheet and one of the fields is the "state" (select field). We need that when the user inserts a cancellation date in the "date" (datepicker) field, the state of the "state" field will be changed to "Cancelled".

It should do when the user submit the form.

We have trying to do that with this function, but it don't work fine.

Thank you for your help.

#448772

Hello Carlos.

To my understanding, you have a "New Products" CRED form, that among other fields, it also has a select field for the "Status" and a datepicker field for the Cancellation Date.

So, upon form submission, you want to check if the user submitted a cancellation date and if he did, then you want that product to expire at that date.

In order to achieve this, you need the following code

add_action('cred_save_data', 'check_if_cancellation_date_is_present',10,2);
function check_if_cancellation_date_is_present($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==354)
    {
        if (isset($_POST['wpcf-cancellation-date']))
        {
            update_post_meta($post_id, '_cred_post_expiration_time', $_POST['wpcf-cancellation-date']['datepicker']);
        }
    }
}

add_filter('cred_post_expiration_custom_actions', 'actions_for_cancellation', 10, 3);
function actions_for_cancellation($custom_actions, $post_id, $form_data) {
    $custom_actions[] = array( 'meta_key' => 'wpcf-status', 'meta_value' => '3' );
    return $custom_actions;
}

The first function checks the submission of a specific form (you need to change '354' to the id of your form) and if the submitted form is the form you want and the cancellation date is submitted, then the product is set to expire on the select date. Keep in mind that if you select the 21st of October 2016, the product will expire at 00:00:00 21st of October 2016.

The second function performs custom actions when a post expires, so in your case, the function, upon product expiration, will update the "Status" field of that post to 3, which is the value for "Cancelled".

You might need to adjust this code a little bit to suit your needs, but I hope you get the general idea of the proposed solution.

If this doesn't resolve your issue or you have any further issues or questions, please let me know.

Regards

#450881

Hello, and thanks for your reply 🙂

I have not been able to make it work. We could make this easier?

What we need to do is, if I enter a date in the "date-cancelation" field (any date), it will change the value of the "status" field when submit the form.

I have tested this code, but I am doing something wrong.

add_filter('cred_save_data', 'actions_for_cancellation', 10, 3);
function actions_for_cancellation($custom_actions, $post_id, $form_data) {
if ($form_data['id']==131){
if (isset($_POST['wpcf-date-cancelation']['datepicker'])){
update_post_meta($post_id, 'wpcf-status', '3');
}
}
}

Could you help me with that?

Best regards.

#451000

Hello Carlos.

Sorry that I didn't get your concept earlier.

The code below implements this concept and works on my end.

add_filter('cred_save_data', 'actions_for_cancellation', 10, 3);
function actions_for_cancellation($post_id, $form_data) {
    if ($form_data['id']==131){
        if (isset($_POST['wpcf-date-cancelation'])){
            update_post_meta($post_id, 'wpcf-status', '3');
        }
    }
}

Also, you are using the "cred_save_data" filter wrong. See here https://toolset.com/documentation/user-guides/cred-api/#csd. $post_id is the first argument while $form_data is the second.

If this doesn't resolve your issue or you have any further issues or questions, please let me know.

Thanks

#452299

Thank you!! It works fine 🙂
Best regards.

Dieses Ticket ist jetzt geschlossen. Wenn Sie ein Toolset Kunde sind und Hilfe benötigen, eröffnen Sie bitte ein neues Support-Ticket.