Selecting a location group during user registration

I would like my new users to select their country, province and district. And then the user is allocated to country group, province group and district group.

I created the following groups:

Country A
Province D
District F
District G
District H
District I
District J
Province E
Country B
Province K
District L
Country C
Province M
District L

I am using 3 countries (Country A, Country B and Country C). Dropdown is labelled Country.

Country A has 2 provinces (Province D and Province E). Dropdown is labelled Province.
Province D has 2 districts (District F and District G). Dropdown is labelled District.
Province E has 3 districts (District H, District I, District J). Dropdown is labelled District.

Country B has 1 province (Province K). Dropdown is labelled Province.
Province K has 1 district (District L). Dropdown is labelled District.

Country C has 1 province (Province M). Dropdown is labelled Province.
Province M has 1 district (District L). Dropdown is labelled District.

Notice that District L has the same name in Province K and Province M, however, they are two different locations.

I implemented this by using the Dropdown feature in the Gravity Form. And in Field settings, I am using the Conditional Logic. So when the user selects Country A, then appears a dropdown with Province D and Province E. And when the user selects Province D, then appears a dropdown with District F and District G).

In the Form, Settings, Groups, if I select Group Memberships for Form Submissions, and then Add users who submit this form to groups based on a form field? The field shows the following:
Country (3 options)
Province (2 options)
District (2 options)
District (3 options)
Province (1 option)
District (1 option)
Province (1 option)
District (1 option)

I can select only one of them though. This means that the user will be given only the options available in the selected location. And the other cannot be selected.

May I ask how would you achieve allocating the new user to a group that is under a dropdown conditional logic, please? Or please suggest how you would approach it if different solution.

Many thanks!


Comments

7 responses to “Selecting a location group during user registration”

  1. BTW, you can use the function
    error_log( $variable );
    or
    error_log( print_r( $array_or_object_variable, true ) );

    to log this variable in your debug.log. Also, in order to allow logging on your site, you should enable WP debugging.

    For debugging to be enabled, edit your wp-config.php file and replace this line: 

    define( ‘WP_DEBUG’, false );

    with these lines:

    define( ‘WP_DEBUG’, true );
    define( ‘WP_DEBUG_LOG’, true );
    define( ‘WP_DEBUG_DISPLAY’, false );

    All logs will be stored in your site’s installation folder under wp-content/debug.log file.

  2. Hi, you’re welcome.

    Please try and log the $entry when the form is submitted. Does the $entry contain all the field selections you have made when submitting this form?

    I’m expecting it to have an array with several data, among which you should also find your selections. What do you get instead?

    Kind regards,
    George

  3. Hi again Andres and apologies for the delay.

    What you need to use is the following GravityForms action hook gform_user_registered, since you need to add the groups on registration. The arguments available with this hook are described in the GF documentation here:
    https://docs.gravityforms.com/gform_user_registered/

    The $entry argument contains the data submitted by the form fields, including the group(s) that your users will choose. Once you have these groups let’s say in an array, you need to check their validity in an iteration and then add the new user to each of these groups like the following:


    foreach ( $groups_array as $group_name ) {
    if ( $group = Groups_Group::read_by_name( $group_name ) ) {
    Groups_User_Group::create( array( 'user_id' => $user_id, 'group_id' => $group->group_id ) );
    }
    }

    Hope it makes sense.

    Kind regards,
    George

    1. Thanks for your answer.

      I created this:

      add_action( ‘gform_user_registered’, ‘add_user_group’, 10, 3 );
      function add_user_group( $user_id, $feed, $entry ) {
      // get group from field 17 of the entry.
      $selected_group = rgar( $entry, ’17’ );
      $user = new WP_User( $user_id );
      $user->add_group( $selected_group );
      }

      I am using the Dropdown with Conditional Logic from Gravity Forms so when Country A is selected, then a dropdown of Provinces corresponding to Country A is shown (Province D and E). If Province D is selected, then a dropdown of Districts corresponding to Province D is shown (District F and G). District F is field 17.

      However, we have 3 countries, one of them will be selected and user will be added to this group.
      For Country A, we have two provinces. One of them will be selected and user will be added to this group as well.
      For Province D, we have 2 districts. One of them will be selected and user will be added to this group too.
      The user will become a member of three groups (1 Country, 1 Province and 1 District)

      All Groups will be created beforehand.

      This means that for:
      3 countries are 3 fields that could be selected.
      2 provinces are 2 fields that could be selected. If selected country A.
      2 districts are 2 fields that could be selected. If selected Province D.

      How can the entry object work with conditional selection rather than being a fixed field?
      // get group from field conditional_selection_field of the entry.
      $selected_group = rgar( $entry, ‘conditional_selection_field’ );

      Many thanks

  4. Hey Andres,

    Sure thing, let me prepare the details and I’ll follow-up here.

    Cheers

  5. Hello George,

    I am somehow familiar with PHP but not much with WP API. Nevertheless, I can have a look at it.

    Grateful if you can share further details to implement this.

    Many thanks

  6. Hi Andres,

    Welcome to our support forum for Groups Gravity Forms addon.

    What you get is expected because Groups Gravity Forms addon is designed to take into account only one form field and the user selection for that predefined field, to create a new user-group relation or if you prefer make the user a member of the selected group.

    In order to achieve what you are describing, then you need a different implementation that could handle multiple fields declaring groups and on form submit, calculate which of these fields have a value selected to finally create those user-groups relations.

    If you are familiar with PHP and the WP API, I can share further details on what you need to implement this. You may follow-up here and I’ll include the necessary pointers to achieve this.

    Kind regards,
    George

Share