How to assign different user roles based on checkbox selection on registration

Hi,

I’m using Gravity Forms for user registration in my WooCommerce site.

I have just one registration form.

In that form, there’s a checkbox that says something like:

“I am a professional customer (salon, etc.)”

I want to do something really simple:

  • If the checkbox is checked, I want the user to get the role wholesale_customer
  • If the checkbox is not checked, I want the user to get the standard role like customer or subscriber

I’ve already created a User Registration Feed, but I don’t understand how to use conditional logic to assign different roles depending on the checkbox.

Could someone please explain step-by-step how to do this?

I’m not a developer, so please keep the explanation as simple and clear as possible :blush:

I past te code in Code Snippets, but dosnt work:

add_action('gform_user_registered_2', 'ustaw_role_hurtownika', 10, 4);
function ustaw_role_hurtownika($user_id, $feed, $entry, $user_pass) {
    // ID pola checkboxa
    $checkbox_field_id = 12;

    // Sprawdź, czy checkbox został zaznaczony
    if (rgar($entry, (string)$checkbox_field_id) === 'Firma / Klient Profesjonalny') {
        // Przypisz rolę "wholesale_customer"
        $user = new WP_User($user_id);
        $user->set_role('wholesale_customer');
    }
}

Thank you so much in advance!

Assuming you are creating a user (and not updating), you could use multiple feeds with conditional logic. This article details the same process but for a slightly different use case.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.