Had an interesting question on the forum today:
- The site has multiple publications that are available for purchase, each has a separate price value. The rest of the form is the same for each publication, only the price changes.
- I want all publications to be purchased using a singular form.
- I want the variation in price controlled by a custom field in WordPrss.
Does that sound possible?
For developers familiar with Gravity Forms, the first tool you might reach for is Dynamic Population using the gform_field_value hook. That will work, but here is an easier method.
Using Merge Tags with Dynamic Population
If you’ve used Gravity Forms before, you’re probably familiar with merge tags. Merge tags allow you to include information specific to the current form, entry, user, page, post and much more! Product fields don’t support the default value option so we’ll need to enable dynamic population for this field.
You will see that I am using the {embed_post:post_title}
merge tag with the “post_title” property. This will display the current post/page title as the product name. You can add the same form on multiple posts and this merge tag will always output the title of the post on which the form is currently being viewed.
Next, we’re using the {custom_field:price}
to populate the post’s custom field titled “price” for the Product field’s “price” input. This assumes you have already setup a custom field on each post named “price”. Whatever value is stored for this custom field (in my example below, the value is “16.99”) will be populated for the field input you’ve indicated. We use the same method to populate the quantity input as well.
Wait… I didn’t think you could use merge tags as population parameters?
Gravity Forms does not support using merge tags as dynamic population parameters out of the box but I’ve written a little snippet that makes these work automagically!
Just paste this code in your theme’s functions.php file.
Summary
The finished product (no pun intended) will be a product that is completely populated from custom fields (aka post meta).
This is just one example of how enabling merge tags for dynamic population can be used. Are you planning on using this for something else? Tell us about it in the comments.
Did this resource help you do something awesome with Gravity Forms?
Then you'll absolutely love Gravity Perks; a suite of 46+ essential add-ons for Gravity Forms with support you can count on.
Does this method work for WooCommerce Product custom fields as well? My real goal is for the price of the particular product the form is attached to to be auto-populated into my form, but this as is would be sufficient.
Do I have to change something for it to load Product custom fields instead?
Thank you!
As far as I know, WooCommerce Products are a “standard” custom post type so they have all the attributes of posts and pages including custom fields. I would expect this to work if you’re populating a Gravity Form on a WooCommerce Product page.
Will this snippet allow a field to be populated from another Gravity Form field within the same form and this upon submission of said form?
For example, if I have an email field named “email”, with ID of 7 i.e. {email:7} and I want to populate an “extra email” field, which is a hidden field (or admin?), will I simply use this snippet and added {email:7} to extra email’s default or dynamic populate?
I’ve tried that without success and am wondering if I’m not fully understanding the use case here.
Thank you for your time!
Looks like this is the actual snippet I needed: https://gist.github.com/spivurno/7029518
Hiii great snippet!
Got 1 question maybe you can help with it.
We integrate this on the following site: http://horecabieding.customcompanyapp.com/horecapanden/vught-unit-1-vughtse-hart/
When I paste the shortcode in the message, everything goes okay. We get the email and the title (custom fields).
But when we put the shortcode in a flyout, nothing happens. You can click on “bieden” under the price to show up the flyout with the form.
What can we do?
Hi Mark, unfortunately, we’d have to dig into your configuration to determine what’s going on here. We’ll leave this open for other users who may have run into the same problem.
This is nice, is there any way that I can add taxonomy metadata to this. What I’m trying to accomplish:
I have added a custom user taxonomy called registration type, here the admin can have different types of user, e.g. architect, technician, etc. *This is working properly
I have added metadata to this type called cost, and displays the cost related to the user *this is done already,saving properly,etc.
I have added a dropdown to the registration form that populates all of the taxonomies dynamically * this is already working properly thanks to your custom post plugin.
I now want to add the custom taxonomy meta to the form so that when the user selects a taxonomy the price gets updated on the form, this doesn’t have to be an actual price field (can be plain text as well).
Would this be at all possible?
Hi John, absolutely possible but not without additional custom code. I’d reach out to Codeable to hire a developer to assist with this one.
Amazing, thank you very much for sharing this trick.
My pleasure, Marco!
I want to display Author Name with Merge Tags trick, but I can not find it, how can I do this?
Actually, I can pull following 3, but not Author Name seen listed. Thanks for your good skills.
https://www.gravityhelp.com/documentation/article/merge-tags/#post-fields
:ID
Displays the ID of the post/page. Example: {embed_post:ID}
:post_title
Displays the title of the post/page. Example: {embed_post:post_title}
:post_author
Displays the author ID of the post/page. Example: {embed_post:post_author}
I don’t currently have a solution for this, Howard.
Hello. This seems exactly what I was looking for. I have just a question: My product dropdown needs to be populated by a list of custom post type fields. But I also want to select values from the dropdown via an url parameter. Can I?
Example :
dropdown values: CPT Title ; CPT Price Field
I want to insert the form on a page and direct users to a precompiled form like http://www.nameofsite.dom/pageurl/?url_param=xxx
Possible?
Populating Drop Down fields (of any type) will be a little different. Here’s a tutorial: https://www.gravityhelp.com/documentation/article/dynamically-populating-drop-down-fields/
Hi Prati, this is a little off topic. Try to keep your comments specific to the article. :)
Hey David,
I seem to also be having an issue with the snippet. For a current project I’ve used a couple of your snippets so far and all have worked great, but when I add this particular one and update my form, my site goes blank. I’m not really sure what I’m missing. I’ve tried various different things playing with the opening and ending statements of the code previous (your gw round code) and the code I’m adding but it just doesn’t seem to like it. Any help would be sincerely appreciated as this functionality is pretty much a necessity for this project to work.
White screen typically means the snippet wasn’t installed correctly or there is a conflict. Follow these instructions to determine which: https://gravitywiz.com/documentation/snippet-troubleshooting/
Ok, so I fixed the issue there, but that unfortunately leads me to another question. How would I make this work if I wanted my custom value to be displayed within the same form? (ie. I have a custom number field that is doing a calculation that determines the price of a membership, and I want that calculated number to populate a product I’ve created on the same form)…
Hi Patrick, you would need to use a calculated product field or if you are trying to dynamically set the quantity, you can use GP Copy Cat to copy the value from your calculation field and then GP Read Only to prevent the user from modifying it.
Hey David,
I greatly appreciate your help. I think I’m just going to use custom fields to calculate the price totals based on the field entries on one form and push them to (part 2 as the user will see) form 2 and then I can further use that data since it’s stored in post meta fields. However, the copycat perk will help make that ALOT easier. Your perks are literally priceless.
Awesome! Glad you find Gravity Perks to be so valuable! :)
Hi, is this still working?
because I cannot make it work.
I added the code in functions.php
Thanks
Hi Enrique, I just confirmed this is still working.
This gives me some ideas based on a current project we’re working on.
That said is this the only or best way to pull through the product_id / post_id of the woo page the gravity form is currently attached to ?
If I var_dump the $woocommerce object I can see the values of the form that is attached to the product I’m currently validating that hasn’t yet hit the cart and I see the product_id in there but not sure how to grab it just yet.
This is just one way to do this. I don’t fully understand what you’re trying to do but Gravity Forms has a lot of hooks that make dynamic population very flexible.
Your tutorials and snippets have been quite helpful in the creation of my new quote system.
There’s only 1 thing I’m lacking and its a summary sidebar widget is on the right hand side, summarising total quantity, products, options, per unit price and total price.
I feel like merge tags, dynamically pre-populated items and read-only might provide the results I’m after but a bit out of my depth here, if you could point me in the right direction that would be great!
Thank you.
I don’t have an existing solution for this; however, I’d love to create one. Any interest in commissioning this functionality? Get in touch.
Hey David… I’m constantly running into your site for instructions on GF workarounds… so thank you for all the great information. Over the last few months I have found myself working on a site for a non-profit and it has very deeply involved GF and GravityView. Here’s my latest effort: I’m using the PayPal add-on to accept payments for an event. We offer the option to pay a 50% deposit or pay it in full at the time of the application. The application is a GF so once the initial payment is accepted, it shows the details, amount, date etc in the form… which is great, but here’s my problem. If the person elects to only pay the deposit, there is no way to accept the balance due without creating a separate form for that transaction.
This may be asking way too much, but here’s what I see a remote possibility: If I can create a separate form that has a dynamically populated amount due (based on subtracting the initial deposit amount collected in the initial form) then I can use a hidden field (to collect USER ID) and then hopefully match those up on the back end.
Is there a hook or example that you could give that would allow me to make this happen? I hope this remotely makes sense. :)
Hi Tim, I don’t have a ready solution for this. I’ve seen this requested a bunch though so it’s on my radar for a plugin or snippet.
Is it possible to do this same thing with a variable price as well? I have a very complex product that I need to have GForms pull the price from the variable price and not just the main product price. Any help you could provide would be amazing!!
I’m not sure I understand?
Thanks for the info !
I’ve been trying to populate gravity form’s drop down menu to list the options of a woocommerce product’s ATTRIBUTES.
What parameter should I put in for it to do so ? Is this even possible ?
Hi Jonoc, if you’re trying to populate a drop down, you’ll most likely want to use gform_pre_render. Here’s a great tutorial on that.