NOTE: THIS POST WAS WRITTEN WHEN PROMOTIONS WERE STILL IN BETA. THE CODE CHANGED SLIGHTLY WHEN IT WAS REALISED INTO PRODUCTION. THIS POST IS MORE FOR HISTORICAL REFERENCE ONLY
With the release of Episerver Commerce 8.12.0 comes a new beta feature for managing promotions and orders. From an initial look, the new system seems a lot more flexible than the older version. Aside from providing extra functionality, one of the immediate advantages of using the new APIs is that that they have been developed against interfaces. This means you will be able to unit test promotions and orders in commerce a lot more easily 😊
There are quite a few advantages of using the new order engine, 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 did not duplicate your order data, certain things would not work and you would encounter errors. The new API allows you to create your own implementation of the order repository, meaning you can process and manage orders wherever you choose. This can remove the overhead of storing the order information twice, in your custom service and in commerce which is nice. 💥
Another new ability is to completely remove the legacy 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 Redis to manage your cart data and completely get rid of some of the nastier commerce API calls like the
Historically, unit testing Episerver commerce has been pretty hard going 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 test
CartHelper, I had to create a wrapper around it and the
BasketHelper and the
Cart objects. All this, before I could 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 to using the old workflow code.
At the time of writing, the new order repository and promotions engine are in beta development. In Episerver beta, functions are not available out of the box. To enable beta functionality please read this tutorial 📖
Getting Started with the Order Repository
In the new version of commerce, we now have an order repository for managing orders. This API implements from
IOrderRepository. In the order repository, you can do standard things like creating a new cart, running a promotion etc...
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 cart by implementing
IOrderGroup and passing that into the repository. In
IOrderGroup you can then set and get order-related data like address, currency, forms, promotions etc...
Getting the total for the basket:
Checking Inventory Exists:
If you need to check if an item exists in stock you can use the warehouse inventory service:
THis gives you a summary of the new beta promotion and order APIs. Happy Coding 🤘