Posted on 06/03/2024 12:57:53
Hi Stephen,
The user you impersonate is always (or should be - depends on your settings) the primary user, so the discount would be applied to it (the impersonated user).
If you need to run any logic when impersonating a user, you can look for SecondaryUserId being null. If it's not null, then it's someone impersonating.
Here's something we use that you might find useful
public static User GetRealUser()
{
return GetUserByUserType(Impersonation.UserType.Real);
}
public static User GetImpersonatedUser()
{
return GetUserByUserType(Impersonation.UserType.Impersonated);
}
private static User GetUserByUserType(Impersonation.UserType userType)
{
var user = User.GetCurrentFrontendUser();
if (user == null || userType == Impersonation.UserType.Impersonated && user.CurrentSecondaryUser == null)
{
return null;
}
return IsRequestedUserPrimaryUser(userType) ? user : user.CurrentSecondaryUser ?? user;
}
private static bool IsRequestedUserPrimaryUser(Impersonation.UserType userType)
{
var impersonationSetting = SystemConfiguration.Instance.GetValue("/Globalsettings/Modules/Users/Impersonation");
var isImpersonationFull = impersonationSetting.Equals("Full", StringComparison.CurrentCultureIgnoreCase);
return userType == Impersonation.UserType.Impersonated && isImpersonationFull || userType == Impersonation.UserType.Real && !isImpersonationFull;
}
The Impersonation.UserType is a simple enum
internal static class Impersonation
{
internal enum UserType
{
Real,
Impersonated
}
}
Does this help?
Best Regards,
Nuno Aguiar