UserGroups do not migrate?

For general questions and discussions specific to the AbleCommerce GOLD ASP.Net shopping cart software.
Post Reply
nethrelm
Lieutenant (LT)
Lieutenant (LT)
Posts: 61
Joined: Thu May 09, 2013 4:47 pm

UserGroups do not migrate?

Post by nethrelm » Fri Aug 15, 2014 9:43 am

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?

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: UserGroups do not migrate?

Post by mazhar » Thu Aug 28, 2014 9:15 am

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();

nethrelm
Lieutenant (LT)
Lieutenant (LT)
Posts: 61
Joined: Thu May 09, 2013 4:47 pm

Re: UserGroups do not migrate?

Post by nethrelm » Thu Sep 04, 2014 9:43 am

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();
}

Post Reply