PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Price field dynamically populated by WP custom field

  1. Aejandro
    Member

    Hello, please how can a GF price field be dynamically populated with a WP custom field?

    The WP custom field IS in the post where the form is embedded.

    Here you have a link to a post with a GF. The WP custom field is "Precio":

    http://goo.gl/HNH7D

    Thank you very much.

    Thank you very much

    Posted 10 years ago on Friday June 21, 2013 | Permalink
  2. Alejandro, you can use the gform_pre_render filter to populate fields in your form. Here is the documentation for the filter: http://www.gravityhelp.com/documentation/page/Gform_pre_render

    [php]
    <?php
    // change the 7 here to your actual form ID
    add_filter('gform_pre_render_7', 'populate_price');
    function populate_price($form) {
    	// documentation: http://bit.ly/15C2BQu
    	// get the price from the custom field for this post
    	$price = get_post_meta(get_the_ID(), 'Precio', true);
    
    	// set the value of your product price to the value which was retrieved
    	if ($field['id'] == 1) {
    		$field['price'] = $price;
    	}
    
    	// always return the modified form
    	return $form;
    }

    That code will go in your theme's functions.php file and will need to be customized for the form ID (I used 7) and the field ID (I used 1). I used "Precio" for the meta key. That will need to match exactly as well.

    Posted 10 years ago on Tuesday June 25, 2013 | Permalink

This topic has been resolved and has been closed to new replies.