Merging carts when a customer logs in

It’s quite common when a customer browses your site without logging in – either she/he intentionally does that, or just forget about logging in. The customer might add some items to carts and even checks out, but then is asked to log in or remember to log in. What would happen?

By default, Episerver Commerce will do as following:

  • Attach all orders made by that section to logged in customer. (I once placed an order in a famous retailer in Sweden without logging in, and then I asked their customer service to link that order to my account so I can track it easier. To my surprise, it cannot be done! That’s why I personally appreciate this feature.)
  • Merge all carts to existing carts, by name and market. So if I’m currently in US market and I added a item to that cart, and the US-cart linked to my account already have 2 other items, then when I log in, my US-cart will contain 3 items.
  • Merge all wishlist to existing wishlists, also per market. (The name for wishlist, by default, is fixed to “Wishlist”)

These default behaviors make senses in most of the cases. However, your business might have different requirements. For example, you don’t want to merge carts, you want to replace them. (I’ve heard a customer wants to do that). So instead of having 3 items in cart after logging in, the customer only has 1 newest item. Or you just don’t want to do anything, if the customer logs in, he or she will get items in the existing cart (that would be quite strange behavior, but that might be something your boss asked to). How?

Like most of other features in Episerver Commerce, that can be customized (that’s why it’s called a Framework). If you are using Commerce 10.4.0 or earlier, however, the customization can be a bit obscure. The ability to change the behaviors when a customer logs in is hidden in a small IHttpModule: ProfileModule. You can replace this section

with your own implementation. You can create a class like below, and replace the type of “ProfileModule” with the full name of your class (including the assembly)

Beware that by replacing the HttpModule, you’re also removing the non-cart related functionalities that Episerver might put there. Is there a better way to do that, without risking doing something wrong? Then Commerce 10.4.1 and newer might help you.

Commerce 10.4.1 comes with a new interface – IProfileMigrator, and it looks like this:

What you can do is to implement a class for this interface, and then register that class in your IConfigurableModule:

There is an supposed internal class by Episerver (ProfileMigrator). You can inherit from this class and override the behavior you want to change. Note that the class itself can be changed without a major version, so beware about that.

Leave a Reply

Your email address will not be published. Required fields are marked *