Adding A LineItem to the Cart In Episerver Commerce

This is another short and simple How To guide. In the latest version of Episerver commerce, we have a new interface available to us called the IOrderRepository, we get access to the order repository the standard way by using a Service Locator or the Injected property (read Dependency Injection In Episerver – ServiceLocator and Injected Explained for more information).

After you have access to the cart, you need to decide which Form you need to use. From my experience, most people use a single default Form to store a users line items, which is what we are going to do in the snippet below.

When we have the form we can then add the line item. Shipments can be used to allow the same order to be split and shipped to different locations. In my experience, I’ve never had a requirement to use this, so again we use a single shipment architecture.

private void AddLineItemToBasket(LineItem lineItem)
{
var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();
var cart = orderRepository.Load<Cart>(SecurityContext.Current.CurrentUserId, Cart.DefaultName).FirstOrDefault();
var orderGroup = (OrderGroup)cart;
var form = orderGroup.OrderForms.FirstOrDefault();
form.LineItems.Add(lineItem);
var index = form.LineItems.IndexOf(lineItem);
var shipment = form.Shipments.FirstOrDefault();
if (shipment != null)
shipment.AddLineItemIndex(index, lineItem.Quantity);
}

Adding A Line Item – Episerver Commerce 7 below

The classic way of getting access to the cart is via the OrderContext. This method is not very unit testing friendly so if you can upgrade to the latest version of commerce, it is worth the effort. If you cannot upgrade then this code snippet is the way to go:

private void AddLineItemToBasket(LineItem lineItem)
{
var cart = OrderContext.Current.GetCart(Cart.DefaultName, SecurityContext.Current.CurrentUserId);
var orderGroup = (OrderGroup)cart;
var form = orderGroup.OrderForms.FirstOrDefault();
form.LineItems.Add(lineItem);
var index = form.LineItems.IndexOf(lineItem);
var shipment = form.Shipments.FirstOrDefault();
if (shipment != null)
shipment.AddLineItemIndex(index, lineItem.Quantity);
}

Lastly, there is also a helper method that can make life a bit easier. The cart helper is an Episerver helper class that provides a set of standard features for the management of carts and orders. Cart helper doesn’t use IOrderRepository (at time of writing). Using it means you have less duplication in your code base but you do lose some flexibility in regards to the options you can use.

var cartHelper = new CartHelper(Cart.DefaultName);
cartHelper helper.AddEntry(SKU);

Conclusion

That covers how to add a line item into the basket. The code isn’t very difficult to grasp. With the new IOrderRepository unit testing your code is easier. If you can’t use the latest version of commerce then OrderContext is still available.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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