Home › Toolset Professional Support › [Resolved] Split: forms: set value of a field based on value of a previous field on the form – 2nd part
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.
This topic is split from https://toolset.com/forums/topic/forms-set-value-of-a-field-based-on-value-of-a-previous-field-on-the-form/
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 20 replies, has 2 voices.
Last updated by brendaT-2 3 years, 3 months ago.
Assisted by: Minesh.
Hi Minesh - all working correctly for registration. WOW!
For my test, I created the following users and checked the 1st field & 2nd field were populated correctly.
user judydixon100@gmail.com Featured multi, 20803, 20727. (free, free)
user judydixon110@gmail.com Standard multi, 20803, 20727 (free, free)
user judydixon120@gmail.com Featured single 20803, 20787 (free, $100)
user judydixon150@gmail.com Standard single, 20803, 20787 (free, $100)
user judydixon140@gmail.com payg, 20802, 20787 ($500, $100).
I then logged on as each user, and selected Job vacancy form, and I can see this form is correctly looking up 2nd field each time, and it is configured to use that field to charge for the form.
I have only done this for the job vacancy form. I will need to add this logic to the other forms (events & Courses). I will also need to hide the field, but I have it showing at the moment so I can debug easily.
Then I have to change the Directory listing form to lookup the first field, and use that to charge.
The next part is: 2) How to auto-populate the "Dir Listing type" field in the directory listing form based on user's record field - "Membership type" so that a Featured (Multi or Single) user only creates Featured directory listings, and a Standard (Multi or Single) user only creates Standard directory listings.
Thanks so much for your help - I am going to have a good read about what you did. I will also need to know how and where you put the code in because I will need to put it onto my PROD site when the time comes,
Hello. Thank you for contacting the Toolset support.
We can now conditionally check the user membership type as well as the user custom field values which we stored.
For instance, if I logged in as any of the following user, there is no payment.
- user judydixon100@gmail.com Featured multi, 20803, 20727. (free, free)
- user judydixon110@gmail.com Standard multi, 20803, 20727 (free, free)
What post form you created for directory and classified - you point out classified as Job form - right?
As there is no payment involved we will just display the form and user should be able to submit it, thats it. Correct?
Hi Minesh, Yes the charging-for-the-forms logic is working for all forms. In some cases it correctly applies a product which has a zero price, and it still does a checkout for that item after submitting the form and that is good.
This next part is different. If the user completing the directory listing form is:
- a Featured Multi or Featured single membership, when they complete the Directory listing form, the field "Dir Listing type" in the form should be set to "Featured". (stored value 1)
- a Standard Multi or Standard single or PAYG membership, when they complete the Directory listing form, the field "Dir Listing type" in the form should be set to "Standard" (stored value 2)
The value in the "Dir Listing type" field determines which fields show on the form and are available to complete for their listing.
Featured listing have all fields, standard listings are missing some fields. This logic is working well using conditional display settings in the custom field group.
I just need to code to set the "Dir Listing type" field based on logged in user's membership type.
This logic is not applicable to jobs, events & courses forms.
I hope that is clear.
Hi Minesh, Yes the charging-for-the-forms logic is working for all forms. In some cases it correctly applies a product which has a zero price, and it still does a checkout for that item after submitting the form and that is good.
==>
Do you want to keep this way, even the user is allowed to submit the directory or job as free still you want to redirect user to checkout page?
yes, we will choose that to happen - they will be reminded every time, that because they paid the high membership price, now everything is free - it is a good feeling for them, and will encourage them to continue to submit as many listings as possible.
The checkout page is simple, and it does NOT go to payment if the amount is zero (ie no paypal). so I think it is not too tedious for the user.
Ok great.
This next part is different. If the user completing the directory listing form is:
- a Featured Multi or Featured single membership, when they complete the Directory listing form, the field "Dir Listing type" in the form should be set to "Featured". (stored value 1)
- a Standard Multi or Standard single or PAYG membership, when they complete the Directory listing form, the field "Dir Listing type" in the form should be set to "Standard" (stored value 2)
==>
I try to search for the custom field "Featured" but I do not see it with Directory custom field group. Can you please tell me where you created the custom field group? if not created create it and tell me where you created it.
The field is called "Dir Listing type". It is the first field in the group.
This is the field that needs to be set, based on the membership of the user (which we saved into the user record in the first part of this question).
users with a featured multi or featured single we need to set this field to "featured".
users with standard multi, standard single or payg we need to set this field to "standard"
As it is a field type "select", featured & standard are the displayed values, stored values are 1 and 2.
I've added the following code to "Custom Code" section within the code snippet "set-membership-codes-per-plan":
=> hidden link
add_action('cred_save_data','func_set_featured_directory_listing',10,2); function func_set_featured_directory_listing($post_id,$form_data) { global $current_user; if ($form_data['id']==18139) { $user_id = $current_user->ID; $multi_plans = array(20711,20713); $single_plans = array(20712,20714); $pay_as_go = array(20715); $listing_type = get_user_meta($user_id, 'wpcf-membership-type', true); if (in_array($listing_type,$multi_plans)) { update_post_meta($post_id, 'wpcf-dir-listing-type', 1); } else if (in_array($listing_type,$single_plans) or in_array($listing_type,$pay_as_go) ) { update_post_meta($post_id, 'wpcf-dir-listing-type', 2); } } }
I can see on the directory listing form the field " "Dir Listing type" i.e. "Listing Type" is added to the form.
=> hidden link
You should just edit the directory listing form and remove that field and save the form.
=> hidden link
Once you submit the directory listing form, based on membership type, it will set the directory listing type automatically using the above code. Can you please confirm it works and set correct listing-type.
thanks Minesh, I have followed the logic, and it reads well. I will edit the form and remove the field as you instruct, and then test.
It says you are editing the form at the moment, so I will wait for you to exit.
I exit the form from here.
Hi Minesh, I have changed the logic. Pls take a look at the new code in the snippet.
However, there is still a problem, because the code is being executed after the form is filled out at the time all data is saved to the post (I think).
This field needs to be set before the user fills in the form fields, because the value of this field determines which subsequent fields are displayed on the form.
In that case I would suggest you create a two forms.
- Directory Form with Featured fields (which we have currently)
- Directory Form without Featured fields (where you create another form for standard membership and remove the featured fields)
Then we can display form based on the membership. We can check membership conditionally and display the form.
Does that sounds good? if yes:
- please create another form without Featured fields (where you create another form for standard membership and remove the featured fields) and send me the link of that and I'll setup the things accordingly.
That sounds good. I have created a 2nd form, Directory Standard Form, and the existing one has been renamed to Directory Featured Form.
I have the "Dir Listing type" field in both forms, and the Standard form has it default to standard, and the featured form defaults to Featured. I will hide this field when we are done.
Currently the existing form (now Directory Featured Form), was made available to users on the Directory pages - Search page and results page as a button link in the top right hand corner. It is also available on the My Account page for logged in users. It would be a clean solution if it was only available on the My account page, an already I am displaying the membership type of the user here. You can see it if you select My account on the Members menu. If there is a way to keep it on the Directory pages that would be good, but I am unsure of limitations on this option as ALL USERS (FEATURED & STANDARD) can view the Directory page. Over to you. Thanks so much for your help!
So, I've adjusted the code added to the "Custom Code" section as given under:
add_action('cred_save_data','func_set_featured_directory_listing',10,2); function func_set_featured_directory_listing($post_id,$form_data) { global $current_user; $user_id = $current_user->ID; if ($form_data['id']==18139) { // for featured form update_post_meta($post_id, 'wpcf-dir-listing-type', 1); } else if ( $form_data['id']== 20827 ) { // for standard form update_post_meta($post_id, 'wpcf-dir-listing-type', 2); } }
- We update post meta for "dir-listing-type" field for featured form with 1 and for standard form with 2.
I've added following filter to "Custom Code" section, so that it will display fields that is having dependent display based on "dir-listing-type" so we need to remove the conditional display you set:
function func_filter_featured_fields_directory_form($field) { global $current_user; $user_id = $current_user->ID; $form_html_id = $field['form_html_id']; $form_id = 0; if ( isset($form_html_id) ) { $parts = explode( '_', $form_html_id); $form_id = (int) $parts[2]; } if ($form_id == 18139) { $featured_fields = array('dir-listing-description-1','dir-listing-description-2','dir-listing-description-3','dir-banner-image','dir-gallery-images'); if (in_array($field['id'], $featured_fields) ) { $field['data']['conditional_display'] = array(); } } return $field; } add_filter('cred_filter_field_before_add_to_form', 'func_filter_featured_fields_directory_form', 10, 1);
Then, to the directory listing form, I've added the conditional display of form for featured and standard form as given under:
=> hidden link
[wpv-conditional if="( '[types usermeta='membership-type' output='raw' current_user='true'][/types]' eq '20711' ) OR ( '[types usermeta='membership-type' output='raw' current_user='true'][/types]' eq '20713' )"] [cred_form form="directory-form"] [/wpv-conditional] [wpv-conditional if="( '[types usermeta='membership-type' output='raw' current_user='true'][/types]' eq '20712' ) OR ( '[types usermeta='membership-type' output='raw' current_user='true'][/types]' eq '20714' )"] [cred_form form="directory-standard-form"] [/wpv-conditional]
Can you please remove unwanted field like "dir-listing-type" as its not required now. You just keep field which you want to display with standard form with standard form and featured form.
I hope this helps.
I dont fully undertstand what you have done - but...the last filter is wrong I believe here is my correction:
If featured multi or featured single then...form=directory-form
If standard multi or standard single or payg then....form= directory-standard-form
I added newlines for readability.
[wpv-conditional if="
( '[types usermeta='membership-type' output='raw' current_user='true'][/types]' eq '20711' ) OR
( '[types usermeta='membership-type' output='raw' current_user='true'][/types]' eq '20712' )"]
[cred_form form="directory-form"]
[/wpv-conditional]
[wpv-conditional if="
( '[types usermeta='membership-type' output='raw' current_user='true'][/types]' eq '20713' ) OR
( '[types usermeta='membership-type' output='raw' current_user='true'][/types]' eq '20714' ) OR
( '[types usermeta='membership-type' output='raw' current_user='true'][/types]' eq '20715' )"]
[cred_form form="directory-standard-form"]
[/wpv-conditional]
Can you tell me what the user is clicking on in the front end to initiate this logic?