SaaS CMS has officially launched! Learn more now.

Shared cart - B2C


Has anyone implemented a shared cart for B2C websites ?

Scenario is user can shop across multiple brand sites using a single cart. I'd be interested in understanding how it can be achieved without requiring the user to log in.


Oct 22, 2019 5:44

What challenges are you facing?

I guess this is obvious for, as a certified developer, but...

Looking at the documentation, you can see how a cart is created or loaded:

var cart = orderRepository.LoadOrCreateCart<ICart>(_customerContext.CurrentContactId, name)

You should simply exchange _customerContext.CurrentContactId and name for something else, but as you understand, you would need a way to pass those parameters between the different websites, for example via cookies or query parameters. And of course, this requires the websites to share the same database.

Oct 22, 2019 9:43

Hi Andreas

The main obvious challenge for anonymous users is how do you load the same cart when the _customerContext.CurrentContactId is unique between the sites. I don't quite follow your statement "You should simply exchange _customerContext.CurrentContactId and name for something else". As you are probably aware, cart is associated to a ContactId. Also for the purposes of shared cart you would want to set the cart type (i.e name) to be the same.

The basis of passing parameters between websites is that you have central trigger where users navigate to the different websites, however users can also directly navigate to the websites. Syncing cookies across multiple sites is in itself another challenge

The possible solution I see is override the CustomerContext implementation to ensure the _customerContext.CurrentContactId returns the same contactId for the different sites However yet to test the viability of this. Keen to hear if anyone has implemented B2C shared cart

Oct 25, 2019 8:23

`As you are probably aware, cart is associated to a ContactId.`- Not really. You fetch a cart by passing a GUID and a name. The guid variable is called customerId but it could be anything. Nothing forces you to use _customerContext.CurrentContactId. Rather than overriding _customerContext.CurrentContactId, maybe you should create some method called GetCartId(...someValues) which determines whether to use a shared cart or something else.

Really, this question has nothing to do with Episerver. From what I can understand, you simply want a way to share a state over multiple websites. Easy if all the websites are hosted as the same web application, more difficult if not.

Oct 28, 2019 10:19

The GUID parameter is supposed to be a CustomerId. If a visitor is not logged in, the CustomerId will be the HttpRequest.AnonymousID which is persisted in a cookie.
True, the GUID parameter could be anything (no constraints), but that's just like asking for trouble in the future. There is another method, IOrderRepository.Load<TOrderGroup>(int orderGroupId), that expects the specific CartId.

The hard part of this shared cart would probably be sharing anonymous state (the AnonymousID) between sites, unless they are on a subdomain to the same main domain. But if you could share the cookie between sites, the AnonymousID could theoretically let you share a single cart.
However, if the user logs in on one of the sites, then the anonymous cart will automatically be changed/merged into the logged-in users cart. But on the other sites the anonymous cookie would still be there, now pointing to a missing cart that will be re-added as an empty cart on first request.

Oct 28, 2019 12:07
Andreas J - Oct 28, 2019 13:21
Why is it asking for trouble in the future? I'd say it's way more narrative to distinguish shared carts from individual carts. And also, what's really the difference between some arbitrary guid and `HttpRequest.AnonymousID` - other than not having to handle the issue that you described? Lastly, if Episerver didn't "want" us to create carts for arbitrary guids, why did they give us the means to do so?
Stefan Holm Olsen - Oct 28, 2019 18:42
Of course you are allowed to use whatever value you want. But if you are going beyond official or usual practice, it usually makes maintenance harder. Especially if no-one else knows why it was made so. That's the kind of trouble I meant.

You probably know the following. The fine thing about using AnonymousID is that when a customer logs in, the built-in cart migration handler, will automatically take care of the cart. If you generate your own CartId values, you may also need to amend the migration handler. This means more custom things that you have to document and maintain.

On the topic of sharing non-logged-in carts, I believe the problem starts on the client. We can implement all sorts of shared cart logic on the server, but the browser still needs to share a cookie across different domains to make the server load the same cart on different sites. If we could share the .ASPXANONYMOUS cookie, sharing the cart might even work out-of-the-box.

If you have a working solution to this problem, I think we would all be interested to learn from it. 😉

Thanks for the insights Stefan .

A solution that doesn't require customising the built-in cart functionality would be the ideal result. Being able to share the AnonymousId cookie across multiple sites (on different domains) seems like the way to go. This would likely involve customising the cookie authentication provider

Oct 28, 2019 20:10
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.