The New Order Repository In Commerce

With the release of Episerver commerce 8.12.0 comes a new beta feature for managing promotions and orders. From my initial look, the new system seems a lot more flexible then the old version.  Aside from providing extra functionality to promotions, one of the immediate advantages of using this new approach is that that the api components have been developed to abstractionist allowing us as developers to be able to unit test commerce a lot more easily.

There are quite a few advantages of using the new features including:

The ability to completely bypass having to store your ordering data within commerce.  If, for example, you have your own custom ordering service and your own custom admin screens to view and edit orders, historically, you still had to store this data within commerce.  If you don’t need to access your order data from within commerce, then there’s not a lot of point writing the code to integrate with it.  Using the new API you can create your own implementation of the order repository.  Removing the overhead of storing the order information twice, in your custom service and in commerce

The ability to completely remove the current commerce workflow from your project entirely.  This means you can even create your own baskets and, at checkout, apply the logic in your own code without having to customise the Episerver workflow classes.

This also means you could use something like Reddis to manage your cart data and completely get rid of some of the nastier commerce API calls like the CartHelper.  Historically, unit testing Episerver commerce has been pretty tough and frustrating as none of the major API’s have been written to abstractions.  CartHelper for example was a massive pain to get under a test harness.  In order to get the cart helper, I had to create a wrapper around it and the  basket helper and the cart itself just to be able to write a few simple tests to check that products can be added to the basket successfully.  With the new order repository you can now create your own implementations as you’re not restricted into using the old workflow code.

Getting Started with the Order Repository

In the new version of commerce, we how have an order repository for managing orders, which implements an IOrderRepository.  In your order repository you can do standard things like create a new cart, run a promotion etc…

public void CreateAndStoreBasket(IOrderRepostory orderRepostory)
{
var cart = orderRepository.Create<Cart>(Guid.NewGuid(), Cart.DefaultName);
var orderGroup = ((IOrderGroup)cart);
promotionEngine.Run(orderGroup);
cart.AcceptChanges();
}

You use the order repository to create a new cart.  You then need to type the cart as an IOrderGroup.  Using this approach means you can create your own implementation of cart by implementing IOrderGroup and pass that into the repository 🙂

In IOrderGroup you can then set and get features like address, currency, forms, promotions etc…

Getting the total for the basket

public Money GetBasketTotal(IOrderRepository orderRepository)
{
var cart = orderRepository.Create<Cart>(Guid.NewGuid(), Cart.DefaultName);
var orderGroup = ((IOrderGroup)cart);
return orderGroup.GetTotals().Total;
}
<h3>Checking Inventory Exists</h3>
If you need to check if an item exists in stock you can use the warehouse inventory service:
public bool DoesInventoryExists(VariationContent item, IWarehouseInventoryService wareHouseInventoryService, IWarehouse warehouse)
{
var applicationId = Guid.NewGuid();
var inventory = wareHouseInventoryService.Get(new CatalogKey(applicationId, item.Code), warehouse);
return inventory.InStockQuantity > 0;
}

Enabling Beta Functionality

At the time of writing this the new order repository and promotions engine are in beta mode. In EPiServer beta functions are not available out of the box. TO enable beta functionality please read:

How To Enable Beta Features In Episerver

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

2 replies

Trackbacks & Pingbacks

  1. […] my previous post ‘The New Order Repository In Commerce‘ I went through some of the benefits of using the new order repository that comes with […]

  2. […] previous articles I’ve talked about Using The New Promotions Engine in EpiServer Commerce and The New Order Repository In Commerce. Also don’t forget you need to read this as well How To Enable Beta Features In […]

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 *