UserGroups do not migrate?
UserGroups do not migrate?
We have a situation where we automatically add users to special groups if they come from certain referring URLs. However, it has come to our attention that when going through checkout, they are losing their group status. I have tracked this down to the EditBillAddress.aspx.cs file where the customer is prompted to login, create an account, or use guest checkout. Part of this process is a call to CommerceBuilder.Users.User.Migrate which when looking at the definition of this method, it does NOT migrate the UserGroups from the oldUser to the newUser. Is there a reason for this? We can certainly work around it by adding group migration code in the specific places during checkout, but it seems to me that group migration should be a part of the Migrate method itself. Why isn't it?
Re: UserGroups do not migrate?
I will report this issue. Normally we migrate anonymous users and its not common to have then assigned to user groups. You can fix it for your website by adding few statements. For example you can update the location where we migrate user to something like
Code: Select all
CommerceBuilder.Users.User.Migrate(oldUser, newUser, true, true);
// FIX: MIGERATE USERGROUPS
foreach(var ug in oldUser.UserGroups)
newUser.UserGroups.Add(new UserGroup(newUser.Id, ug.GroupId));
newUser.Save();
Re: UserGroups do not migrate?
Yes, that's is pretty much what I did. As I said, it was easy to work around, it just caught me by surprise that this behavior wasn't already there. I created a utility method that does the job, and added it after each call to the normal Migrate method where it was needed.
Code: Select all
public static void MigrateUserGroups(User oldUser, User newUser)
{
foreach (UserGroup userGroup in oldUser.UserGroups)
{
if (!newUser.IsInGroup(userGroup.GroupId))
{
newUser.UserGroups.Add(new UserGroup(newUser.Id, userGroup.GroupId));
}
}
newUser.UserGroups.Save();
}