Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Surjit Bharath
Aug 4, 2022
  1496
(0 votes)

Merging carts when customer logs in on Commerce 14

In 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.

For the example middleware code please go here: https://www.hiddenfoundry.com/thoughts/merging-carts-when-logging-in-from-an-anonymous-state-in-commerce-14/

Aug 04, 2022

Comments

Johannes Mols
Johannes Mols Jun 6, 2023 09:32 AM

Hi, thank you for the blog post, it helped a lot. The code snippet isn't fully working though regarding the service locator and getting the anonymous ID as a feature. Not sure if that's due to the updated in Commerce (running v14.11.0) or because it's incomplete.

For me, the anonymous ID is also removed as soon as the user is authenticated. A workaround I found is to get the ID from the cookie that the anonymous ID middleware sets. This isn't deleted upon logging in.

Here is the middleware that is working for me:

public class AnonymousCartMigrationMiddleware
{
	private readonly RequestDelegate _next;
	private readonly IOrderRepository _orderRepository;
	private readonly ICurrentMarket _currentMarket;
	private readonly IProfileMigrator _profileMigrator;

	public AnonymousCartMigrationMiddleware(
		RequestDelegate next,
		IOrderRepository orderRepository,
		ICurrentMarket currentMarket,
		IProfileMigrator profileMigrator)
	{
		_next = next;
		_orderRepository = orderRepository;
		_currentMarket = currentMarket;
		_profileMigrator = profileMigrator;
	}

	public async Task Invoke(HttpContext context)
	{
		if (context.User.Identity is { IsAuthenticated: true } && !string.IsNullOrEmpty(context.User.Identity?.Name))
		{
			var justLoggedIn = CustomContext.Current.IsFirstLoadAfterLogin;

			// Not using "context.Request.HttpContext.Features.Get<IAnonymousIdFeature>()?.AnonymousId" because it is removed at this point. The cookie, however, is not deleted (see AnonymousIdMiddleware).
			context.Request.Cookies.TryGetValue("EPiServer_Commerce_AnonymousId", out var anonymousId);

			if (justLoggedIn && !string.IsNullOrWhiteSpace(anonymousId) && Guid.TryParse(anonymousId, out var anonymousGuid))
			{
				var cart = _orderRepository.LoadCart<ICart>(anonymousGuid, "Default", _currentMarket);
				if (cart != null && cart.GetAllLineItems().Any())
				{
					_profileMigrator.MigrateCarts(anonymousGuid);
					_profileMigrator.MigrateOrders(anonymousGuid);
					_profileMigrator.MigrateWishlists(anonymousGuid);
				}

				// Add it back in so that the application can process it as normal.
				context.Session.SetString(SessionKeys.JustLoggedIn, "true");
			}
		}

		await _next(context);
	}
}

public static class AnonymousCartMigrationMiddlewareExtensions
{
	public static IApplicationBuilder UseAnonymousCartMigration(this IApplicationBuilder app)
	{
		return app.UseMiddleware<AnonymousCartMigrationMiddleware>();
	}
}

The CustomContext is an implementation that handles session info. When we process a login, we store a value in the user session that flags it as the first load after log-in. This is used elsewhere in the application. Accessing this property removes the session key, which is why I add it back in after running the migration. This prevents multiple requests from triggering the migration simulatenously.

Hope this helps someone.

Surjit Bharath
Surjit Bharath Jun 6, 2023 04:14 PM

Thanks for the comment. Sorry the initial attempt didn't work. I've just tried it on the latest version of Commerce 14.12 on a fresh Foundation instance and it was working just fine.

Just make sure you call your middleware after:

app.AnonymousId();

app.UseAuthentication();

app.UseAuthorization();

Johannes Mols
Johannes Mols Jun 9, 2023 06:18 AM

Hi, yes it seems I had the UseAnonymousId middleware after the two. Moving it to the front makes it work :) Maybe you'll want to add that line to your blog post in the last code snippet.

The syntax to get the Id seems to also have changed slightly from your snippet. This works: 

context.Request.HttpContext.Features.Get<IAnonymousIdFeature>()?.AnonymousId

Please login to comment.
Latest blogs
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog