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.

Hide product fields

  1. Hi, i am trying to build a form where i place five product fields and depending of how many items are in the shopping cart turn the product field visibility on or off. I have tried several methods and none of then i can't get to work,
    1) If i do it with conditional logic the checkbox field must be visible, it won't work if the field is admin only (not visible), that is not acceptable for my solution.
    2) I tried the gform_product_info filter to try to add new product fields dynamically, somehow i never got this filter to even execute, i tried both gform_product_info and gform_product_info_ID.
    3) Tried the gform_pre_render approach changing the price field's adminOnly property, the property does change but it does nothing, the field always shows, even though the documentation says every field has this property when i edit the form it is not there.

    Could anybody suggest how to to accomplish this? Any of the three approaches are ok by me, but the second one, where i could create the fields dynamically would be the best one.

    I would like to know also how to change the product field labels (name, Qty, price), i can't find a clear explanation on how to do this this on the documentation.

    Posted 12 years ago on Saturday July 9, 2011 | Permalink
  2. Searching in the forums i did find a fourth approach using CSS, it works perfectly and solves that problem, no i have a different but related problem that i can't seem to solve by searching the forums or the documentation.
    -------------
    I want to change the name, price and quantity for each product in the gform_pre_render action, so far i discovered that the name can be set using the ['label'] attribute, the price can be set with the ['basePrice'] attribute, it does show the price but when i check the value in validation it is still 0.00, the quantity, after 6 long hours i have not been able to find it.

    I suspect that maybe the answer is hidden in the grab.by shortened links (http://grab.by/9exC) but this service seems to be down. I really need to solve this asap, can anyone share a solution?

    I would also like to change the product field labels (name, Qty, price), i can't find a clear explanation on how to do this this on the documentation, from the forums i understand that it was suppose to be incorporated in the translation file, but the one that i have doesn't have this labels. Any suggestions?

    Posted 12 years ago on Monday July 11, 2011 | Permalink
  3. This is actually a bit tricky. I will look for ways to improve it, but the following should do the trick for you.

    1- You will need to configure your product fields to "Allow field to be populated dynamically" on the advanced tab. Enter unique parameter names for each field. Following are the parameters I used for the code snippet below: http://tardis1.tinygrab.com/awEm

    2- Use the following code snippet, replacing the IDs to match your form and fields.

    add_filter("gform_field_value_prod1_name", "prod1_name");
    function prod1_name($name){
       return "new product name";
    }
    
    add_filter("gform_field_value_prod1_quantity", "prod1_quantity");
    function prod1_quantity($name){
       return 3;
    }
    
    add_filter("gform_pre_render", "populate_product");
    function populate_product($form){
    
        //only setting product for form id 165
        if($form["id"] != 165)
           return $form;
    
        //Setting 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["label"] = "new product name";
                $field["basePrice"] = 22;
            }
    
        return $form;
    }
    Posted 12 years ago on Monday July 11, 2011 | Permalink
  4. The form could have 5 items, that would mean that for every sub field in the a product field i would have to check if the item is valid, read the item form the database and then set the value, the end result would be a very inefficient and slow page to say the least, and me writing 10 filters+functions to do a job that would be more efficient in the form preload action. If you say that this is the only way to do it, i guess will have to do it that way, but please consider implementing some other way to access the product field variables in code.

    But your solution only solves part of the problem. In your code you set the basePrice in the preload form action, i am already doing this, but when i access the value in the validate action i still get 0.00 not the price i placed in the field before, i also need to access the quantity field to check it is bot zero or some strange value, how would i do this?

    Could you please tell me how i could translate the Price and quantity labels? This is very important!

    Posted 12 years ago on Monday July 11, 2011 | Permalink
  5. I understand pre-populating product fields are not very developer friendly right now. This will be improved in our next major release.
    For the validation hook, you need to access the price and quantity from the $_POST variable.
    Following is an example to point you in the right direction.

    //Replace 165 with your actual Form ID
    add_filter('gform_validation_165', 'custom_validation');
    function custom_validation($validation_result){
    
        //finding field with ID of 8 and making sure quantity is greater than 0
        foreach($validation_result["form"]["fields"] as &$field){
    
            //NOTE: replace 8 with the field you would like to validate
            if($field["id"] == "8"){
    
                //Base prices are retrieved using "input_FIELDID_2"
                $base_price = GFCommon::to_number($_POST["input_8_2"]);
    
                //Quantities are retrieved using "input_FIELDID_3"
                $quantity = $_POST["input_8_3"];
    
                //fails validation if quantity is not a positive integer or if price is less than $20
                if($quantity <= 0 || $base_price < 20){
                    $validation_result["is_valid"] = false;
                    $field["failed_validation"] = true;
                    $field["validation_message"] = "This field is invalid!";
                }
    
                break;
            }
        }
        return $validation_result;
    }

    Regarding the translation of the Price and Quantity sublabels, you can translate them using the translation files with PoEdit. Following is a good article explaning how to do that. http://weblogtoolscollection.com/archives/2007/08/27/localizing-a-wordpress-plugin-using-poedit/
    If you don't find those strings in your translation file, you may need to update it from the gravityforms.pot file. You can do that in poEdit by clicking on Catalog -> Update from POT file. http://tardis1.tinygrab.com/awKN

    Posted 12 years ago on Tuesday July 12, 2011 | Permalink
  6. Hi Alex, thanks for your helpful answer. I have one more question, when you say major release you mean 1.6 or 2.0. When could we expect that release where the price fields will be more developer friendly, in one, two, three, six months time frame? I need this answer to make some decisions.

    Thanks and best regards

    Posted 12 years ago on Tuesday July 12, 2011 | Permalink
  7. The next major release will be 1.5.3. We are still collecting requirements for it, so I imagine it will be around 3 months, maybe a little more.

    Posted 12 years ago on Tuesday July 12, 2011 | Permalink
  8. Just making a note on this post, the next major release won't be 1.5.3. We have decided the next major release will now be released as v1.6. It's just a version name change. There are too many new features and enhancements for it to be a 1.5 related point release. So look for 1.6 to have this feature.

    Posted 12 years ago on Monday July 18, 2011 | Permalink
  9. Is there and ETA on 1.6 and will the end user field duplication feature still be in as indicated by this post?

    http://www.gravityhelp.com/forums/topic/optional-multiple-inputs

    Posted 12 years ago on Tuesday July 26, 2011 | Permalink
  10. v1.6 is in development. It should be released as a beta release sometime in August. The List Field is still planned for this release.

    To clarify, it is not end user field duplication. It isn't going to let you duplicate existing fields. It is a List/Datagrid Field. It allows you to define a field that consists of multiple columns of inputs that you define, and then the end user can add as many rows of data as they would like. We'll also have an option to limit how many rows they can enter.

    It will be similar to how you add data to a Product Field under the Pricing Fields when adding choices. There are multiple columns where you enter information and then you can hit plus or minus to add or remove rows.

    Posted 12 years ago on Tuesday July 26, 2011 | Permalink
  11. Thanks for the clarification. Will it be usable with Custom Post Types out of the box or will is require extra code to get the information in to CPT/Meta boxes?

    Posted 12 years ago on Tuesday July 26, 2011 | Permalink
  12. It's going to be an Advanced Field, not a Post Field, initially. It's a multi-value field consisting of multiple columns and rows so i'm not sure how we could make it work with post custom fields out of the box as an easy to use feature because custom fields are single value fields. This is a multi-dimensional array with columns a rows.

    Posted 12 years ago on Tuesday July 26, 2011 | Permalink