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.

Can Pricing Field Populate Dynamically From Custom Field?

  1. Gil Namur
    Member

    Hi There,

    Suppose I have a simple order form, and I want the price in the product field to come from a custom field in the post, can that be done and if so, how?

    I searched for this but could not see it so forgive me if this has been covered!

    Thanks and Cheers,
    Gil

    Posted 8 years ago on Wednesday July 13, 2011 | Permalink
  2. Use the following code snippet, making sure to update the Form and Field IDs to match yours.

    add_filter("gform_pre_render", "populate_price");
    function populate_price($form){
    
        //only applies to form id 165
        if($form["id"] != 165)
           return $form;
    
        global $post;
        //replace "my_price_meta" with your custom field name
        $price = get_post_meta($post->ID, "my_price_meta", true);
    
        //Setting price for product field id 8. Replace 8 with your actual field id. You can get the field id by looking at the input name in the markup.
        foreach($form["fields"] as &$field)
            if($field["id"] == 8){
                $field["basePrice"] = $price;
            }
    
        return $form;
    }
    Posted 8 years ago on Wednesday July 13, 2011 | Permalink
  3. Gil Namur
    Member

    Hi Alex!
    Thanks so much! So if I had 3 different custom fields
    _widget_1_price
    _widget_2_price
    _widget_3_price
    I would need to add this code to my functions.php for each one yes?

    Cheers,
    Gil

    Posted 8 years ago on Wednesday July 13, 2011 | Permalink
  4. You could incorporate all 3 into the single function call. So you wouldn't add that code 3 times to your functions.php, you'd add it once but you'd modify it so it does all 3 at the same time.

    Posted 8 years ago on Wednesday July 13, 2011 | Permalink
  5. It depends. Do these apply to different forms, or different fields in the same form?

    Posted 8 years ago on Wednesday July 13, 2011 | Permalink
  6. Gil Namur
    Member

    Hi Alex,

    Different fields in the same form.

    Thanks !
    Cheers,
    Gil

    Posted 8 years ago on Thursday July 14, 2011 | Permalink
  7. You can use the following then. Just make sure to replace the Field IDs (i.e. 8, 9 and 10) with yours.

    add_filter("gform_pre_render", "populate_price");
    function populate_price($form){
    
        //only applies to form id 165
        if($form["id"] != 165)
           return $form;
    
        global $post;
    
        //Setting price for product field id 8. Replace 8 with your actual field id. You can get the field id by looking at the input name in the markup.
        foreach($form["fields"] as &$field)
            if($field["id"] == 8){
    			$price = get_post_meta($post->ID, "_widget_1_price", true);
                $field["basePrice"] = $price;
            }
    		else if($field["id"] == 9){
    			$price = get_post_meta($post->ID, "_widget_2_price", true);
                $field["basePrice"] = $price;
            }
    		else if($field["id"] == 10){
    			$price = get_post_meta($post->ID, "_widget_3_price", true);
                $field["basePrice"] = $price;
            }
    
        return $form;
    }
    Posted 8 years ago on Thursday July 14, 2011 | Permalink
  8. Gil Namur
    Member

    Thanks Alex!
    I'll let you know how it turns out!
    Cheers,
    Gil

    Posted 8 years ago on Thursday July 14, 2011 | Permalink
  9. Gil Namur
    Member

    Hey Alex,
    This worked PERFECTLY!
    THANK YOU!!!
    Cheers,
    Gil

    Posted 8 years ago on Friday July 15, 2011 | Permalink

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