• Resolved kbb

    (@keyspan)


    Hello!

    I’ve got a question about passing a variable from a custom field (created with Advanced Custom Fields) to a CFF form via shortcode variables. Is there any way to do this? I’d like to reuse the same form for multiple pages, but each page will have a different set of data stored in the custom fields.

    Here’s a very simple example of what I mean:

    I’ve got a post with the following custom fields and data…

    event_title: Learn to Dance!
    event_price: 25
    event_date: September 21, 2019

    I then want to pass that information to CFF fields and set them as the value…

    fieldname10: Learn to Dance!
    fieldname11: 25
    fieldname12: September 21, 2019

    On the post, I’ve got a form where a user can select the number of tickets. A user comes along and says they want 2 tickets…

    fieldname13: 2

    I can then use a calculated field to get the total…

    fieldname14: fieldname11*fieldname13

    And the Summary field to generate the following…

    Event: Learn to Dance!
    Date: September 21, 2019
    Number of People: 2
    Price per Person: $25
    Total: $50

    Thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @keyspan

    If you define a new attribute in the form’s shortcode, it would be transformed into a javascript variable with global scope.

    For example, assuming you want to populate the fieldname1 with the value of attribute: my_var defined through the shortcode:

    
    [CP_CALCULATED_FIELDS id="1" my_var="43"]
    

    The previous shortcode insert the form with id=”1″ in the page, and will generate a javascript variable called my_var whose value is the number 43

    Now, in the form, insert a “HTML Content” field with the following piece of code as its content to assign the value of the my_var variable to the fieldname1 after the form be generated:

    
    <script>
    fbuilderjQuery(document).one('showHideDepEvent', function(){
    fbuilderjQuery('[id*="fieldname1_"]').val(my_var).change();
    });
    </script>
    

    and that’s all. Of course, the way you extract the value from the custom field associated to the post to be used in the shortcode, won’t be part of our plugin.

    Second alternative:

    There is a second alternative that not requires to define a new attribute into the shortcode. In this case you should to include a block of javascript code into the content of same page that includes the form, to define the default values of form’s fields. Following the same example above:

    
    <SCRIPT>
    cpcff_default = { 1 : {} };
    cpcff_default[1][ 'fieldname1' ] = 43;
    </SCRIPT>
    

    Note: Remember in this case the block of code is inserted directly into the page’s content.

    Similar to the previous case, the way you replace the number 43 in the piece of code above by the corresponding “Advance Custom Field” value should be managed by yourself in this case.

    More information in the following link:
    https://cff.dwbooster.com/documentation#populate-form

    My preferred solution:

    My preferred solution allows the form to read the information of the custom fields directly from the database, the form, simply should know the post where it was inseted.

    This solution requires the use of DS fields distributed with the Developer and Platinum versions of the plugin. DS is a set of fields whose values are read from an external data source, like a database or CSV file (Number DS, Currency DS, Line Text DS, and so on).

    The “Advanced Custom Fields” plugin stores the values of variables into the WordPress database’s table: wp_postmeta, entering into the column post_id the id of the post where the field is defined, into the meta_key column the field’s name, for example: event_title and into the meta_value column, the value assigned to the field in the corresponding post.

    So, the process would be as follows:

    Pass the post id into the form’s shortcode:

    
    [CP_CALCULATED_FIELDS id="1" post_id="123"]
    

    and then, insert a “Line Text DS” field (or the DS field that you prefer) into the form, with the following database query:

    
    SELECT meta_value as value FROM {wpdb.postmeta} WHERE meta_key='event_title' AND post_id=<%post_id%>
    

    The queries for the other Advance Custom Fields would be:

    
    SELECT meta_value as value FROM {wpdb.postmeta} WHERE meta_key='event_price' AND post_id=<%post_id%>
    

    and

    
    SELECT meta_value as value FROM {wpdb.postmeta} WHERE meta_key='event_date' AND post_id=<%post_id%>
    

    More information in the following link:
    https://cff.dwbooster.com/blog/2019/02/14/ds/?index=1

    Best regards.

    Thread Starter kbb

    (@keyspan)

    Thank you! I went with your preferred solution and it’s working well!

    Plugin Author codepeople

    (@codepeople)

    Hello @keyspan

    Thank you very much for letting me know.

    Best regards.

    Thread Starter kbb

    (@keyspan)

    Eek! A followup question for you.

    I am populating a Radio Button DS with a list of postIDs (for the values) and post titles (for the texts). No problems with this following your previous feedback. I’m now wondering if two things are possible:

    FIRST
    I would like to populate a Radio Button DS options with posts that have a specific category (tag_id). The text for each options would come from post titles with a specific tag_id, and the value from the post_id for each title.

    For example, I have 20 events. Three of the events have a category of Dance (tag_id is 15). The radio button options should be populated with only posts that have a tag_id of 15:

    o Learn to Dance! (post_id:5)
    o Intermediate Dance! (post_id:14)
    o Advanced Dance! (post_id:19)

    In each of those posts I have some custom fields (event_price and event_date):

    post_id:1
    post_title: Learn to Dance!
    event_price: 25
    event_date: September 21, 2019

    post_id:14
    post_title: Intermediate Dance!
    event_price: 40
    event_date: September 24, 2019

    post_id:19
    post_title: Advanced Dance!
    event_price: 99
    event_date: September 29, 2019

    If the user selects Intermediate Dance….

    o Learn to Dance!
    X Intermediate Dance!
    o Advanced Dance!

    … then the rest of my form should use the Intermediate Dance post_id (14) to populate the other fields:

    Event: Intermediate Dance!
    Date: September 24, 2019
    Price per Person: $40

    How many people are going? [ ]

    The user can enter a number (say 2) and then I can display a calculated total, or summary, or whatever.

    Any suggestions?

    Thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Passing variables through the shortcode’ is closed to new replies.