When developing an ecommerce solution, the ability to create and manage a shopping cart is a fairly important feature. In this post I’m hoping to shed some light on how the shopping cart functions within Commerce Connect. I’ll discuss what it is, how it’s managed, what it does and doesn’t do out of the box and how to extend the functionality or integrate it with another system.
First of all, the cart defined by Commerce Connect is more than just a way to save products a customer wishes to purchase. In fact, a better way to think of the cart is more of a pending order. In addition to products for purchase, the cart can contain shipping and payment info, addresses, discounts, etc… As your users are providing information and making selections, such as what shipping method they prefer, it’s important to remember that these operations will all be cart centric.
From a high level the cart is managed by using Commerce Connect’s CartServiceProvider. Different cart features can be accessed by creating a request, passing the request to the appropriate method and checking the results returned.
var userId = Sitecore.Analytics.Tracker.Current.Contact.ContactId; var cartServiceProvider = new CartServiceProvider(); var createCartRequest = new CreateOrResumeCartRequest("website", userId.ToString()); var results = cartServiceProvider.CreateOrResumeCart(createCartRequest);
Behind the scenes the CartServiceProvider will end up invoking one or more pipelines. For cart related tasks you can find these pipelines within App_Config\Include\Sitecore.Commerce.Carts.config.
There are a couple of interesting points to make about the out of the box Carts.config. First, just about every pipeline has a processor with a parameter referencing the “eaStateCartRepository”. This is an implementation of a shopping cart that leverages Sitecore’s engagement analytics and MongoDB to store the state of a user’s shopping cart. It comes out of the box but can be disabled if not needed.
For more information I highly recommend this video: https://www.youtube.com/watch?v=ef8M5DZXFnM
This does highlight one of the key concepts and strengths of Sitecore’s Commerce Connect in that it abstracts the implementation of the carts functionality. If I’m developing a feature for a site I can manage the cart as needed without having to worry about if the cart is stored in the AutomationStates collection or there is a synchronization process to another system. Further I can begin development of a project using the eaState repository and later very easily swap out how and where cart information is stored.
The other thing you’ll notice is that the pipelines eventually end up running the save cart pipeline. This is important to keep in mind as you’re adding your custom processors. If, for example, you’re modifying how shipping information is added to the cart you may insert a processor into the “commerce.cats.addShippingInfo” pipeline that could handle a “AddShippingInfo” request and would verify the shipping info and update the cart. You could then create a processor in the “commerce.carts.saveCart” that would be responsible for clearing relevant caches whenever a cart was updated.
In our example above both pipelines were invoked when shipping info was associated with a cart. Either pipeline could be used to integrate with 3rd party ecommerce systems that need access to details of a user’s cart. Which pipeline to use will probably be dictated by the specifics of your 3rd party commerce system. If you’re able to make a single call to update all of the details of your cart it would make sense to use the save cart pipeline. If, however, you need to update each component of the cart individually (shipping info, payment info, line items etc…) you may want to do these in their specific pipeline to avoid unnecessary calls to the ecommerce system.
Well that’s it. I hope this post was helpful and gave you enough background knowledge for you to sink your teeth in deeper while developing a Sitecore Commerce Connect solution.