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.

Dynamically populate price field dropodown

  1. Gaslight
    Member

    I would like to populate dynamically a dropdown price field with values from the post custom meta fields (assume 3 meta fields called price1, price2 and price3).

    I've read the documentation about populating fields http://www.gravityhelp.com/documentation/page/Gform_field_value_$parameter_name but I couldn't find documentation for dropdown fields and price fields.

    Has anyone from the staff a sample snippet for my case?

    thank you

    Posted 12 years ago on Thursday February 16, 2012 | Permalink
  2. Gaslight
    Member

    oh and another thing... since it's a dropdown AND a price field, I guess each option in the select item should have a label and a value (price); I would like to get both of these values from custom fields.

    Posted 12 years ago on Thursday February 16, 2012 | Permalink
  3. Gaslight
    Member

    sorry I didn't see this one:

    http://www.gravityhelp.com/documentation/page/Dynamically_Populating_Drop_Down_Fields

    Posted 12 years ago on Thursday February 16, 2012 | Permalink
  4. Gaslight
    Member

    I tried following the instructions above but my code below doesn't seem to work

    add_filter('gform_pre_render_7', 'populate_prices');
    function populate_prices($form){
        foreach($form['fields'] as &$field){
            if($field['type'] != 'select' || strpos($field['cssClass'], 'populate-prices') === false)
                continue;
            $post = $wp_query->post;
            if(get_field('package_paxbase', $post->ID)) {
                $pax = array();
                while(the_repeater_field('package_paxbase', $post->ID)) {
                    $pax[0] = get_sub_field('package_1_base');
                    $pax2to3 = get_sub_field('package_2_3_base');
                    $pax[1] = 2 * $pax2to3;
                    $pax[2] = 3 * $pax2to3;
                    $pax4to6 = get_sub_field('package_4_6_base');
                    $pax[3] = 4 * $pax4to6;
                    $pax[4] = 5 * $pax4to6;
                    $pax[5] = 6 * $pax4to6;
                    $pax7to9 = get_sub_field('package_7_9_base');
                    $pax[6] = 7 * $pax7to9;
                    $pax[7] = 8 * $pax7to9;
                    $pax[8] = 9 * $pax7to9;
                    $pax10to14 = get_sub_field('package_10_14_base');
                    $pax[9] = 10 * $pax10to14;
                    $pax[10] = 11 * $pax10to14;
                    $pax[11] = 12 * $pax10to14;
                    $pax[12] = 13 * $pax10to14;
                    $pax[13] = 14 * $pax10to14;
                    $pax15to19 = get_sub_field('package_15_19_base');
                    $pax[14] = 15 * $pax15to19;
                    $pax[15] = 16 * $pax15to19;
                    $pax[16] = 17 * $pax15to19;
                    $pax[17] = 18 * $pax15to19;
                    $pax[18] = 19 * $pax15to19;
                    $pax20to24 = get_sub_field('package_20_24_base');
                    $pax[19] = 20 * $pax20to24;
                    $pax[20] = 21 * $pax20to24;
                    $pax[21] = 22 * $pax20to24;
                    $pax[22] = 23 * $pax20to24;
                    $pax[23] = 24 * $pax20to24;
                    $pax25to29 = get_sub_field('package_25_29_base');
                    $pax[24] = 25 * $pax25to29;
                    $pax[25] = 26 * $pax25to29;
                    $pax[26] = 27 * $pax25to29;
                    $pax[27] = 28 * $pax25to29;
                    $pax[28] = 29 * $pax25to29;
                    $pax30to34 = get_sub_field('package_30_34_base');
                    $pax[29] = 30 * $pax30to34;
                    $pax[30] = 31 * $pax30to34;
                    $pax[31] = 32 * $pax30to34;
                    $pax[32] = 33 * $pax30to34;
                    $pax[33] = 34 * $pax30to34;
                }
            }
            $choices = array(array('text' => __('Numero persone', 'salgari'), 'value' => ' '));
            $i = 1;
            foreach($pax as $price) :
                $count = $i++;
                $choices[] = array('text' => $count, 'value' => $price, 'price' => $price);
            endforeach;
            $field['choices'] = $choices;
        }
        return $form;
    }
    Posted 12 years ago on Thursday February 16, 2012 | Permalink
  5. Gaslight
    Member

    I can get simple values to work in my forms with Gform_field_value_$parameter_name

    but no luck with the dropdown method suggested

    perhaps need to update the tutorial with the case of a price field?

    Posted 12 years ago on Thursday February 16, 2012 | Permalink
  6. Hello @Gaslight,

    The following code snippet should point you in the right direction.

    add_filter('gform_pre_render_35', 'populate_dropdown');
    function populate_dropdown($form){
    
        foreach($form['fields'] as &$field){
    
            if($field['id'] != 4)
                continue;
    
            $choices = array(
                array('text' => 'Item 1', 'value' => 'Item 1', 'price' => '1'),
                array('text' => 'Item 2', 'value' => 'Item 2', 'price' => '2'),
                array('text' => 'Item 3', 'value' => 'Item 3', 'price' => '3'),
                );
    
            $field['choices'] = $choices;
    
        }
    
        return $form;
    }

    Cheers,
    Alex.

    Posted 12 years ago on Friday February 17, 2012 | Permalink
  7. Gaslight
    Member

    oh thanks Alex that workerd really well

    one further question if you don't mind, suppose I have a standard pricing field, should I use

    Gform_field_value_$parameter_name as described here:
    http://www.gravityhelp.com/documentation/page/Gform_field_value_$parameter_name

    right now I'm trying to add the price in this way, however the value I'm fetching to set the price changes from product to product - maybe I need to use the pre_render method? If so how should I fill the $field variable? I tried $field = $my_value (fetched from custom meta) but it didn't work...

    thanks again!

    Posted 12 years ago on Saturday February 18, 2012 | Permalink
  8. Gaslight
    Member

    never mind about the simple price field, I managed to do that

    now I'm trying to dynamically populate price options checkboxes

    which method is required for those? same as dropdown with $choices ?

    Posted 12 years ago on Sunday February 19, 2012 | Permalink
  9. Gaslight
    Member

    for checkboxes, I'm trying the following code, but it doesn't work - I can create a correct amount of options, but values, labels and prices are empty - also for some reason one of them appears checked, while they should be all unchecked by default

    if($field['id'] == 9) {
                $accservices = get_field('package_accservices', $post->ID);
                if(!empty($accservices)) {
                    $choices = array();
                    foreach($accservices as $accservice) :
                        $choices[] = array(array('text' => $accservice['accservice_label'], 'value' => $accservice['accservice_price'], 'price' => $accservice['accservice_price']));
                    endforeach;
                    $field['choices'] = $choices;
                }
            }
    Posted 12 years ago on Monday February 20, 2012 | Permalink
  10. @Gaslight,
    Use the following code snippet as a guideline on how to populate the option checkbox field as well as your product drop down field.
    http://pastie.org/3420970

    Good Luck!

    Posted 12 years ago on Monday February 20, 2012 | Permalink
  11. Gaslight
    Member

    hmm sorry it didn't work

    check: http://pastie.org/3421246
    this is just the part for checkboxes

    this is the pastie for the whole function: http://pastie.org/3421271
    the others are dropdown and are working really well

    but I still have the following issue with checkboxes field

    I think the $inputs are generated correctly - suppose I have two keys in the array I'm using to generate the checkboxes, then I will have correctly 2 checkboxes, with correct ID. If my array has, suppose, 3 keys, then the checkboxes will be 3 and so on. However those have empty values, no text and by default are checked.

    Something is wrong with the $choices array perhaps? However if I do a var_dump of $choices and $inputs outiside of that context they look correct, I mean the arrays I'm using to generate the checkboxes should be ok.

    thanks

    Posted 12 years ago on Monday February 20, 2012 | Permalink
  12. Gaslight
    Member

    line 8 of the first pastie containing only the snippet for the checkboxes, there were missing quotes for "label", I fixed it but no change

    I found out if remove the brackes [] from $checkboxes[] (line 7) and $inputs[] (line 8) the code will work, but of course the foreach won't and the array will be filled only with the last array key I'm feeding the loop

    I'm looking for a solution, probably something wrong with my code

    Posted 12 years ago on Monday February 20, 2012 | Permalink
  13. this link: http://www.gravityhelp.com/documentation/page/Dynamically_Populating_Drop_Down_Fields should be posted on this page: http://www.gravityhelp.com/documentation/page/Using_Dynamic_Population#Hooks

    and once again I request commenting be enabled on the documentation pages. So people can help each other - like they do on php.net.

    Posted 12 years ago on Monday February 20, 2012 | Permalink
  14. Gaslight
    Member

    Dweanus in my case it's about price fields; the method suggested in that documentation page is not suitable for price fields, I had to specific the ID of the price field I wanted to modify, both for dropdowns and checkboxes

    Anyway, the dropdown issue is fixed... if there had been a way to contribute to the documentation pages I wouldn't mind having helped in this case

    Now I have still one issue open about the checkboxes (check a couple of my posts above http://www.gravityhelp.com/forums/topic/dynamically-populate-price-field-dropodown#post-49969 ) - if the mods wish me to open a new thread for that it's fine

    I agree documentation should be commented - I follow WooCodex for WooCommerce and it has that feature, I like it

    cheers

    Posted 12 years ago on Tuesday February 21, 2012 | Permalink
  15. Gaslight
    Member

    Any updates yet?

    Posted 12 years ago on Monday February 27, 2012 | Permalink
  16. I looked at your checkbox code and it seems like you have an extra array() when creating the $checkboxes and $inputs variable.
    Try replacing:

    $checkboxes[] = array(array('text' => $accservice['accservice_label'], 'value' => $accservice['accservice_label'], 'price' => $accservice['accservice_price'].'.00', 'isSelected' => false));
    $inputs[] = array(array('id' => $count, label => $accservice['accservice_label']));

    with the following:

    $checkboxes[] = array('text' => $accservice['accservice_label'], 'value' => $accservice['accservice_label'], 'price' => $accservice['accservice_price'].'.00', 'isSelected' => false);
    $inputs[] = array('id' => $count, label => $accservice['accservice_label']);
    Posted 12 years ago on Thursday March 1, 2012 | Permalink