London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

Scott Reed
Apr 17, 2018
  3147
(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
Understanding the Infrastructure Powering AI

The marketing world is increasingly captivated by the potential of AI agents. However, it's crucial to recognize that these agents are not simply...

Patrick Lam | May 15, 2025

Meet the Newest OMVPs – Winter 2025 Cohort

We're excited to officially welcome the latest winter cohort of Optimizely Most Valuable Professionals (OMVPs) - a group of passionate tech...

Satata Satez | May 14, 2025

Helper method to encode query string properly

When using Url.ContentUrl() in Optimizely 12, encodes spaces as + in the query string. If you want to encode the spaces as %20, use the below helpe...

sunylcumar | May 13, 2025

Get ContentReference from GUID

Optimizely CMS 12 provides a Utility function to retrieve Content Reference from a Guid by ussing the below static method. var contentReference =...

sunylcumar | May 13, 2025