I have successfully setup a rule to create a new user based on the values entered in the registration form and assign their newly created account to the registration, though I can't seem to figure out how to add existing users to the registration if they match what the user entered into the email address.

Here is the create and assign rule. There are some extra fields here because I added the same fields to the User Account that exist in the Registration form. This way, I can pre-populate the form later if someone adds their email in again using an autocomplete entity reference field (which I am having some other issues with but that's a different post): (If you want to use this, and do not user Commerce, remove the commerce sections before you import)

{ "rules_create_user_account_from_registration_form" : {
    "LABEL" : "Create User Account from Registration form",
    "PLUGIN" : "reaction rule",
    "WEIGHT" : "9",
    "OWNER" : "rules",
    "REQUIRES" : [
      "rules",
      "commerce",
      "commerce_registration",
      "registration",
      "commerce_checkout"
    ],
    "ON" : { "registration_insert" : [], "commerce_checkout_complete" : [] },
    "IF" : [
      { "NOT entity_exists" : { "type" : "user", "property" : "mail", "value" : "[registration:mail]" } }
    ],
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "user",
            "param_field_first_name" : "[registration:field-first-name]",
            "param_field_last_name" : "[registration:field-last-name]",
            "param_field_home_street_address" : "[registration:field-home-street-address]",
            "param_field_home_city" : "[registration:field-home-city]",
            "param_field_home_state" : "[registration:field-home-state]",
            "param_name" : "[registration:field-first-name] [registration:field-last-name]",
            "param_mail" : "[registration:mail]"
          },
          "PROVIDE" : { "entity_created" : { "entity_created" : "Created User" } }
        }
      },
      { "entity_save" : { "data" : [ "entity-created" ], "immediate" : "1" } },
      { "user_unblock" : { "account" : [ "entity-created" ] } },
      { "data_set" : { "data" : [ "registration:user" ], "value" : [ "entity-created" ] } },
      { "commerce_registration_set_state" : {
          "commerce_order" : [ "site:current-cart-order" ],
          "registration_state" : "complete"
        }
      },
      { "entity_save" : { "data" : [ "registration" ], "immediate" : "1" } }
    ]
  }
}

And here is what I am doing for the Add Existing Users, but can't get it to work even though I have added some messages to see if it assigns it and it "does".

{ "rules_add_existing_user_to_registration" : {
    "LABEL" : "Add Existing User to Registration",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "commerce", "rules", "registration" ],
    "ON" : { "registration_insert" : [] },
    "IF" : [
      { "entity_exists" : {
          "type" : "user",
          "property" : "mail",
          "value" : [ "registration:anon-mail" ]
        }
      }
    ],
    "DO" : [
      { "entity_query" : {
          "USING" : {
            "type" : "user",
            "property" : "mail",
            "value" : [ "registration:anon-mail" ],
            "limit" : "1"
          },
          "PROVIDE" : { "entity_fetched" : { "entity_fetched" : "Fetched User" } }
        }
      },
      { "data_set" : { "data" : [ "registration:user" ], "value" : [ "entity-fetched:0" ] } },
      { "entity_save" : { "data" : [ "registration" ], "immediate" : "1" } },
      { "drupal_message" : { "message" : "Entity Fetched 0 [entity-fetched:0]\r\nshould of been added to \r\nRegistration User [registration:user]\r\nsince it was found.. though it\u0027s not being added. " } }
    ]
  }
}

Has anyone else managed to do this yet?

Comments

TravisJohnston’s picture

Issue summary: View changes

adding some extra descriptions about my first rule.

TravisJohnston’s picture

Issue summary: View changes
Greg Boggs’s picture

Version: 7.x-1.4 » 7.x-1.x-dev
Component: Documentation » Registration Core
Category: Support request » Feature request

Having an option to attach registrations successfully to existing users is a good feature. It would be worth implementing in the module itself. Although, we would want to make certain that it was an optional feature, and it might need a permission setting, because you wouldn't want a spammer to be able to register all your users for an event.

TravisJohnston’s picture

Make sense.

TravisJohnston’s picture

This is a major need for me as we are going to be integrating Salesforce with our website which uses entity registration and commerce registration for signing up and paying for events.

The first rule works great and I have it tied to Salesforce to create a Contact based on the information in the User Account form which is populated from the information provided in the Registration form.

Though if that person is then registered again for an event, I really need their current account to be assigned to that registration. It's weird that I can get this to work fine if it's a new account, but if it's an existing account it doesn't.

joegl’s picture

I am also having this difficulty.

I noticed the anon_email field is only available when the who_is_registering option is set to "Other Person". The user field is not available for "Other Person", but for "Other Account". My presumption, is the module is not going to allow the user field to be set when the who_is_registering is set to "Other Person." Unfortunately, the who_is_registering option is not available through rules, so I cannot move forward in testing my hypothesis at the moment.

joegl’s picture

I think I figured it out based on this code on line 202 of registration.entity.inc:

public function registrant_type($account) {
    $reg_type = NULL;
    if (!empty($account)) {
      if ($account->uid && $account->uid === $this->user_uid) {
        $reg_type = REGISTRATION_REGISTRANT_TYPE_ME;
      }
      elseif ($this->user_uid) {
        $reg_type = REGISTRATION_REGISTRANT_TYPE_USER;
      }
    }
    if (!empty($this->anon_mail)) {
      $reg_type = REGISTRATION_REGISTRANT_TYPE_ANON;
    }
    return $reg_type;
  }

If the anon_mail field is set, it's not going to update the user or email fields, so the anon_mail field needs to be emptied. Also, the $account field from what I found refers to the author. Emptying the anon_mail field isn't enough then, since that simply sets the registration to "for myself". The author needs to be set to the user account as well. Sorry if this sounds convoluted, my brain is a bit fried right now. What ended up working for me was basically taking the rule Travis has and changing the Action order to:

- Fetch entity by property (get the user account by the anon_email)
- Set a data value (set the [registration:anon-mail] field to blank to empty it)
- Set a data value (set the [registration:author] to the user account)
- Set a data value (set the [registration:user] to the user account)
- Save entity immediately (save the registration entity immediately after changing these values)

In summary, anon_mail needs to be unset and the author needs to be set to the desired user account for it to work.

john.oltman’s picture

Version: 7.x-1.x-dev » 3.0.x-dev
Status: Active » Closed (duplicate)
Related issues: +#2340765: Match anonymous registration to user