Scott Reed
Apr 17, 2018
  3055
(5 votes)

Using commerce carts for more than just basket and configuring the cart cleanup task

Different Serializable Carts

In a standard commerce implementation we would usually unless building something custom use the new Serializable Carts feature in Episerver Commerce.

For loading a cart we call either LoadCart or LoadOrCreateCart on the IOrderRepository service which allows us to pass a string such as the following

var contactId = PrincipalInfo.CurrentPrincipal.GetContactId();
return OrderRepository.Service.LoadCart<ICart>(contactId, "Default");

This is fine for when we just have a single commerce cart but we can also use cart for persisting other lists of items such as wishlists and then still using the standard validation workflows to make sure products removed from our catalogues are removed from these too. We simply pass a different string in to this method which will create a different cart.

Cart Cleanup

One of the problems that you could potentially hit is that out of the box (if you turn it on) there is a clean up Job that cleans up the carts. However as listed here https://world.episerver.com/documentation/developer-guides/commerce/configuration/feature-specific-commerce-configurations/ there is a set of commands that allow you to handle this.

You can either call your wishlist "WishList" (passing this string in to the LoadCart or LoadOrCreateCart method) which will be left by the clean up procedure or you can change the appSetting episerver:ExpiredCartsRemovalJobExcludedCartName and set the name of the cart which you don't want to be cleaned. Sadly this only supports one excluded cart and can only be set in the configuration, I would suggest maybe extending this class and disabling the base Job if you need to change this.

Hopefully this will give an example of using carts for not only the commerce process but also for a wishlist or for other persistent store of commerce items against a user.

As a bonus to note the cart cleanup defaults to 30 days buy can be controlled by adding an appSetting as defined in the above docs of ExpiredCartJobDays which you can set to change the number of days carts that exist should be cleaned up.

Thanks, Scott

Apr 17, 2018

Comments

Bob Bolt
Bob Bolt Apr 17, 2018 04:19 PM

We fixed the capitalization on "WishList" -- Thanks, Scott.

Apr 18, 2018 09:04 AM

Thanks, I've removed that part from my blog :-) 

Please login to comment.
Latest blogs
Custom form element view in Optimizely CMS 12

Do you want full control over the form element markup? Create your own views!

Tomas Hensrud Gulla | Dec 11, 2024 | Syndicated blog

How to Elevate Your Experimentation - Opticon workshop experience

As a non-expert in the field of experimentation, I’d like to share my feedback on the recent Opticon San Antonio workshop session titled "How to...

David Ortiz | Dec 11, 2024

Persisting a Strawberry Shake GraphQL Client for Optimizely's Content Graph

A recent CMS project used Strawberry Shake to generate an up-to-date C# GraphQL client at each build. But what happens to the build if the GraphQL...

Nicholas Sideras | Dec 11, 2024 | Syndicated blog

Opti ID with Secure Cookies And Third Party AddOns

Opti ID has revolutionised access to the Optimizely One suite and is now the preferred authentication method on all PAAS CMS websites that I build....

Mark Stott | Dec 9, 2024