
Brick-and-Mortar Stores Can Thrive with E-commerce
In a world where online shopping is king, traditional stores are finding new ways to collaborate, bl
Optimizely Most Valued Professional (OMVP), Subject Matter Expert (SME), CMS and Commerce certified
Contact MeIn Commerce 13 and below, we had out of the box default behaviour that when a customer logs in, their cart, wishlist and any orders they made during their anonymous state will get merged into their customer contact associated with their principal object. This is done via a httpmodule where an authentication event would be raised and the IProfileMigrator would get called executing the said actions.
Unfortunately in the current version of Commerce 14 (v14.5.0 at the time of this post) does not include the middleware to hook up the IProfileMigrator and do this automatically as it did with its previous versions.
Luckily if you reading this, you're most likely just looking to provide a solution to your client's requirements. Here it is:
public async Task InvokeAsync(HttpContext context)
{
if (context.User.Identity.IsAuthenticated && !string.IsNullOrEmpty(context.User.Identity?.Name))
{
var anonymousId = context.Request.HttpContext.Features.Get()?.AnonymousId;
if (!string.IsNullOrWhiteSpace(anonymousId))
{
var orderRepository = ServiceLocator.Current.GetInstance();
var currentMarket = ServiceLocator.Current.GetInstance();
var anonCart = orderRepository.LoadCart(new Guid(anonymousId), "Default", currentMarket);
if(anonCart != null && anonCart.GetAllLineItems().ToList().Count > 0)
{
var profileMigrator = ServiceLocator.Current.GetInstance();
profileMigrator.MigrateCarts(new Guid(anonymousId));
}
}
}
await _next(context);
}
In Commerce 13 and below, IProfileMigrator was called from an authentication event called MigrateAnonymous attached in httpmodule. Aspnetcore 5 and above doesn't have httpmodules and there doesn't seem to be any appropriate migration event I can take advantage of at the time of writing this post. There are events associated with cookie authentication such as SigningIn, SignedIn and ValidatePrincple; but occur too early in the pipeline for the user object to be instantiated and the correct Customer Contact to be loaded. The same also goes for IAuthorizationMiddlewareResultHandler as implementing this interface allows you check the authorization result and can be useful to target a challenge response but not so useful for targeting a successful authentication.
As a result of this, we have to do additional checks against the state of the anonymous cart and the user principal object to make sure the migrate code is only run once, after the customer logs in.
These guards should be enough to ensure the MigrateCarts called is called and not called again on the next subsequent request.
The example above only shows MigrateCarts but you can also add in MigrateOrders and MigrateWishlists depending on your client requirements.
With the middleware example above. We are attempting to only run the code when the user is authenticated. So within the pipeline, the middleware should be called after the UseAuthentication is called.
app.UseAuthentication();
app.UseAuthorization();
app.EnableAnonymousCartMerging();
In a world where online shopping is king, traditional stores are finding new ways to collaborate, bl
Overcomplicated requirements are slowing you down. Here’s how trimming the excess can help you launc
I discuss our experiences not validating business features early which result in delayed projects
The virtues of finding flaws before they find your customers.
Best practice of managing your Optimizely nuget packages
Demystify Image Resizing on the edge for your Optimizely solution
Explore the adaptability and advanced features of Optimizely Customized Commerce, highlighting its s
Headless vs Traditional doesn't have to be a hard choice. An Optionality approach gives you the best
We are proud to announce Optimizely has awarded Hidden Foundry with Specializations
We discuss our experiences of working with Navico to refactor their single site into a multi-site so
Grasp ODP and how it's AI and data consolidation enables insight driven conversions
Redirect the user elsewhere instead of the default Optimizely login page when attempting to access a
Learn to use CLI and dotnet command to create new cms / commerce projects and perform admin actions.