Skip Navigation

[Resolved] forms: set value of a field based on value of a previous field on the form

This thread is resolved. Here is a description of the problem and solution.

Problem:
forms: set value of a product to purchase based on selected plan by user while registering the user with forms commerce

Solution:
You can use the form's hook "cred_before_save_data" to assign or set different product IDs based on the plan selected by user.

You can find proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/forms-set-value-of-a-field-based-on-value-of-a-previous-field-on-the-form/#post-2312943

Relevant Documentation:

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

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 12 replies, has 2 voices.

Last updated by brendaT-2 3 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#2311743

Tell us what you are trying to do?
Hello support - what I am trying to do is: I have a user form to register/create a user. The role I assign is "lister". They have a pulldown/select field on the form where they select the membership type they want (5 types - unlimited listings, thru to pay-as-you-go model, varying prices). The membership field is used to add 'membership' product to cart, so that depending on choice, 1 of 5 products is added to cart & they pay for the membership. All good and working.
Now that they are registered as a lister, each time they fill out a listing form:
==> if they chose and paid for a membership with unlimited listings for the year, I do not want to charge them for each listing (it is included in their membership price).
==> if they chose the "pay-as-you-go' membership - every time they fill out a listing form, I want to charge them.
Problem - how to know which product to add to cart on the listing form, based on user membership type.

My solution was to have another custom field in the user record 'user-product-to-apply-to-form' which contains the product code of a free listing, or the product code of a $100 listing.
When a user submits a listing, I can get the user-product-to-charge from their user record using
[types usermeta='user-product-to-apply-to-form' format='FIELD_VALUE' current_user='true'][/types], and put it in a field on the listing form.
This logic works - When I MANUALLY set the 'user-product-to-apply-to-form' to product 20787 (free), the listing form retrieves the value and stores it in the form, and uses it to add to cart - all working!
How can I AUTOMATICALLY populate the 'user-product-to-apply-to-form' field at registration time based on their selected membership type?

OR is there a better way to do it? I am only doing what I know!
Is there any documentation that you are following?
I did read about validation of form after the user form is submitted, and query the membership type and write specific values to the user field using update_user_meta function. But I have no idea how to write the code, and where to put it. If that was an option, I'd need code & detailed instructions! I am willing to learn.
As always, I hope you can help. You have been fantastic with your support so far, and I have learnt so much. Thanks for a great product.

#2311879

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Toolset Form's commerce offers a hook "cred_commerce_add_product_to_cart" using which you can dynamically change the product to be added to cart.
- https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_add_product_to_cart

Based on the information you shared, I think we can use this hook to change the product to be added to cart based on your subscription plans but first I have to review your setup.

Can you please share problem URL with access details and share the flow to buy the plan and for what plan you want to set what product.

*** 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.

#2312271

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've few further questions.
- When you say product code, you mean the product ID - right? if yes:
- On user profile page, I can see two custom fields: hidden link
==> Product to apply to form for Directory Listing and Product to apply to form for Classified Listing

Lets say, when I register a new user with "pay-as-you-go" membership, currently user is not get charged - right? if yes:
- you want to charge user when they login and try to create listing and for that you have two custom fields added to user profile "Product to apply to form for Directory Listing" and "Product to apply to form for Classified Listing"

So, the flow would be:
- A user registered with membership plan "pay-as-you-go" and at this time with this plan you are not charging anything BUT what you want is you want to save the product ID to the user custom field "Product to apply to form for Directory Listing" and "Product to apply to form for Classified Listing" - right? If yes:
- I see you created a product with "Classified Ad (Chargeable)" which have $100
=> hidden link

Now, I login as the lister role: user: judydixon80@gmail.com and when I try to create a directory listing, you want that user should be charged $100 and the applicable product for this is "Classified Ad (Chargeable)" - right?
=> hidden link'

Now, I see with the directory listing form, you did not set any product to purchase:
- hidden link

So, what if you set the form to "Charge payment with this form" and set the option "Always this product, regardless of form input" to "Pay as you go membership" - will that work?

But - your main question here is when user try to register using "pay as you go" or any other membership plan you want to user the user custom field "Membership type" and "Product to apply to form for Directory Listing" and "
Product to apply to form for Classified Listing" fields - right?

#2312673

Hi Minesh - yes is the answer to just about all of that above.
I have added ALL products now, for completeness as it is confusing when I had only added the products for classified ads chargeable & not chargeable.......Here are all the rules for what to charge, when:

A User registers with Featured Multi membership, the fields should be set to
- "Membership type" = Featured Multi, stored as 20711 which is product for membership $2000
- "Product to apply to form for Directory Listing" = 20803 which is product for directory listing (not chargeable). **
- "Product to apply to form for Classified Listing" = 20727 which is product for classified listing (not chargeable) **
User registration form applies "Membership type" field as product to charge ==> $2000 to register/membership

That user completes Directory listing form, product to charge for this form is user field "Product to apply to form for Directory Listing" = 20803 ==> $0 charged for directory listing
User completes Job Vacancy listing form, product to charge for this form is user field "Product to apply to form for Classified Listing" = 20727 ==> $0 charged for classified listing.
That user can create unlimited directory & classified ads, and each time there is no charge because it is included in their membership.
Classified listings are jobs, events & courses, so I need to add this logic to all those forms. At the moment the only form I have updated (as you found out) is the job vacancy form. I also had not updated the create a directory listing form yet.

A User registers with Pay as you go membership, the fields should be set to
- "Membership type" = Pay as you go, stored as 20715 which is product for membership $0
- "Product to apply to form for Directory Listing" = 20802 which is product for "directory listing (chargeable)". **
- "Product to apply to form for Classified Listing" = 20787 which is product for classified listing (chargeable). **
User registration form applies "Membership type" field as product to charge ==> $0 to register/membership

That user completes Directory listing form, product to charge for this form is user field "Product to apply to form for Directory Listing" = 20802 ==> $500 charged for directory listing
User completes Job Vacancy listing form, product to charge for this form is user field "Product to apply to form for Classified Listing" = 20787 ==> $100 charged for classified listing.
That user can create unlimited directory & classified ads, and each time there is a charge because nothing is included in their membership.

This logic & model allows users a pay up front for everything, or a pay nothing up front and just pay for usage. Here are all 5 Membership types:
- "Featured Multi" - unlimited Directory & classified ads; Directory ad is "Featured" cost=$2000
- "Standard Multi" - unlimited Directory & classified ads; Directory ad is "Standard" cost=$1500
- "Featured Single" - Directory listing, but no classified ads; Directory ad is "Featured" cost=$1000
- "Standard Single" - Directory listing, but no classified ads; Directory ad is "Standard" cost=$500
- "Pay as you go" - they get unlimited listings, and they pay each time cost=$0
Featured Directory listing = lots words, images, and sorted to top of results page
Standard Directory listing = fewer words, no images, sorted after featured listings
There is no concept of featured/standard for classified ads. they are all the same.
On the Directory listing form I need to populate the "Dir Listing type" based on the user's membership type. Currently it is a simple pull down and they selected featured or standard.**

The model works. I set it up for job vacancies and it works. I did this by manually populating the fields with product ids. The problem/s I need to solve have ** next to them.
1) How to auto populate the following fields based on the membership type the user selected:
- "Product to apply to form for Directory Listing" **
- "Product to apply to form for Classified Listing" **
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.

I hope that is clear. It is a bit long, but it sets out the entire model, values, products and logic. Thanks so much for your patience.

#2312685

and... "ha ha ha I haven't finished yet!" here are the product id values for all 5 membership types:
- "Featured Multi" ($2000) - Directory product to apply 20803 (free) classified product to apply 20727 (free)
- "Standard Multi" ($1500) - Directory product to apply 20803 (free) classified product to apply 20727 (free)
- "Featured Single"($1000) - Directory product to apply 20803 (free) classified product to apply 20787 ($100)
- "Standard Single" ($500) - Directory product to apply 20803 (free) classified product to apply 20787 ($100)
- "Pay as you go" - Directory product to apply 20802 ($500) classified product to apply 20787 ($100)
It allows for a single member to change their mind later and put up n classified ads, and pay for them.
Some companies are not interested in jobs, event and courses, so they choose the single option always.
For Pay-as-you-go the directory listing should be a standard listing (not featured). I forgot to add this statement to the above description: 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) or PAYG user only creates Standard directory listings.

#2312943

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Thank you for all required information. Lets take things step by step.

So, I've adjusted your registration form and removed the unnecessary fields from it and added the following fields as hidden field using the generic field.
=> hidden link

[cred_generic_field type='hidden' field='wpcf-product-to-apply-to-form-for-directory-listing']{
        	"default":"0",
        	"persist": 1
        }[/cred_generic_field]
		
          [cred_generic_field type='hidden' field='wpcf-product-to-apply-to-form']{
            "default":"0",
            "persist": 1
            }
		[/cred_generic_field]

Then, I've added the following filter code to "Custom Code" section offered by Toolset and added the following code within "set-membership-codes-per-plan" code snippet:
=> hidden link

add_action('cred_before_save_data', 'func_set_user_listing_code_per_membership',10,1);
function func_set_user_listing_code_per_membership($form_data){
    // if a specific form
    if ($form_data['id']==20583){
      
      	$multi_plans = array(20711,20713);
        $single_plans = array(20712,20714);
        $pay_as_go = array(20715);
        
        if (isset($_POST['wpcf-membership-type']) and 
            in_array($_POST['wpcf-membership-type'],$multi_plans)) {
          
           	 $_POST['wpcf-product-to-apply-to-form-for-directory-listing'] = 20803;
             $_POST['wpcf-product-to-apply-to-form'] = 20727;
          
        } else if (isset($_POST['wpcf-membership-type']) and 
            in_array($_POST['wpcf-membership-type'],$single_plans)) {
          
           	 $_POST['wpcf-product-to-apply-to-form-for-directory-listing'] = 20803;
             $_POST['wpcf-product-to-apply-to-form'] = 20787;
          
        } else if (isset($_POST['wpcf-membership-type']) and 
            in_array($_POST['wpcf-membership-type'],$pay_as_go)) {
          
           	 $_POST['wpcf-product-to-apply-to-form-for-directory-listing'] = 20802 ;
             $_POST['wpcf-product-to-apply-to-form'] = 20787;
        }
    }
    
}

Can you please now try to create a new user using the registration form for all kind of membership plans as you mentioned as follows:
- hidden link

"Featured Multi" ($2000) - Directory product to apply 20803 (free) classified product to apply 20727 (free)
- "Standard Multi" ($1500) - Directory product to apply 20803 (free) classified product to apply 20727 (free)
- "Featured Single"($1000) - Directory product to apply 20803 (free) classified product to apply 20787 ($100)
- "Standard Single" ($500) - Directory product to apply 20803 (free) classified product to apply 20787 ($100)
- "Pay as you go" - Directory product to apply 20802 ($500) classified product to apply 20787 ($100)

And confirm that the user fields "Product to apply to form for Directory Listing" and "Product to apply to form for Classified Listing" stores the correct product ID as per above information.

#2313021

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,

#2313023

oh btw, I did have a site crash during adding all those users. I was adding judydixon130, and it crashed. Thats why the users now go 100, 110, 120, 140 150. 130 was not created when the site crashed. The email from the site says:
Error Details
=============
An error of type E_ERROR was caused in line 52 of the file /home/xpxzldl6gcu2/public_html/woo.constructionadvisor.com.au/wp-content/plugins/cred-commerce/classes/CRED_Commerce_Forms_Meta_Handler.php. Error message: Uncaught Error: Cannot use object of type WP_Error as array in /home/xpxzldl6gcu2/public_html/woo.constructionadvisor.com.au/wp-content/plugins/cred-commerce/classes/CRED_Commerce_Forms_Meta_Handler.php:52
Stack trace:
#0 /home/xpxzldl6gcu2/public_html/woo.constructionadvisor.com.au/wp-content/plugins/cred-commerce/classes/event/base.php(36): CRED_Commerce_Forms_Meta_Handler->get_forms_meta_data('cred_meta')
#1 /home/xpxzldl6gcu2/public_html/woo.constructionadvisor.com.au/wp-content/plugins/cred-commerce/classes/event/onorderchange.php(11): CRED_Commerce_Event_Base->execute()
#2 /home/xpxzldl6gcu2/public_html/woo.constructionadvisor.com.au/wp-content/plugins/cred-commerce/classes/Form_Handler.php(208): CRED_Commerce_Event_OnOrderChange->execute()
#3 /home/xpxzldl6gcu2/public_html/woo.constructionadvisor.com.au/wp-content/plugins/cred-commerce/classes/Plugin_Base.php(47): CRED_Commerce_Form_Handler->onOrderChange(Array)
#4 /home/xpxzldl6gcu2/public_html/woo.constructionadvisor.com.au/wp-content/plugins/cred-co

#2313035

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I'm not sure but I can see the user is there in admin:
- hidden link

I search for both user judydixon100@gmail.com Featured multi, 20803, 20727. (free, free) and user judydixon110@gmail.com I can see both users where there.

What you mean by site is crashed?

#2313047

HI minesh, yes users 100, 110, 120, 140 and 150 exist. The site crashed when I was registering 130, and it said "the site has had a critical problem and needs to close". (or something like that) so I had to close down my browser, and relaunch, and go in using my admin signon, and then I could see that 130 has not been created. The site sent me the email (as above) saying there was a critical error with the plugin cred commerce. I then added 140 & 150

#2313055

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

As per our support policy, we entertain only one question per ticket. This will help other users searching on the forum as well as help us to write correct problem resolution summery.

Regarding the site crash, if you see it happens again, please report back a new ticket with all required information.

I'll split the ticket with your next part and we will continue with the following new ticket:
- https://toolset.com/forums/topic/split-forms-set-value-of-a-field-based-on-value-of-a-previous-field-on-the-form-2nd-part/

We will cover following part with above new ticket and I'll follow-up there.
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,

You are welcome to mark resolve this ticket. Happy to help 🙂

#2313057

read the code snippet and I can see what it is doing. I also found the location you put it, so that is all good! great work, many many thanks.

#2313065

My issue is resolved now. Thank you!