While implementing the price calculation mechanism we faced a need to reflect the price fluctuation over the previous 30 days in SAP Commerce solution. It was required by the EU Omnibus Directive legislation for e-commerce solutions to ensure transparent price reflection especially relating to promotions and discounts and to protect the clients’ interests. Our customer was operating in the retail optical industry with thousands of products. SAP Commerce out of the box price mechanism didn’t allow for a large number of calculations in the breakdown of big volumes of products. So, we faced the need to implement a mechanism to cover the following business requirements:

Business requirements were to show the special discounted prices for products in a clear way:

Regular OOTB SAP Commerce solution for calculating promotional prices (using database backed Cart) assumed the following:

Leveraging this solution was extremely time-consuming, overloading the platform, causing the following drawbacks:

So, such a solution was unacceptable especially because all calculations were done on database. In backoffice we collect samples with the lowest product price to be able to present it for the customer for every product and every active promotion. So those are very big calculations as we currently have approximately 150 thousands of products and almost 2 thousands of promotions.

Data that we collect are samples with details as below.

Samples with the lowest product price details

Naturally a new requirement appeared – increasing efficiency and acceleration of prices calculation and page loading.

We considered and came up with introducing the InMemoryCart solution taking into account the following:

So, we’ve created a solution based on inMemoryCart. All promotional calculations for presentation are performed in the InMemoryCart, while the customer still retains a regular Cart, similar to what is found in most shops. Please see selected screenshots of the solution’s anonymized code below:

  1. Creating a key generator to distinguish Cart and InMemoryCart code.

Key generator to distinguish Cart and InMemoryCart code

  1. Creating a new factory for carts

New factory for carts

  1. Making a kind of InMemoryCartService. In original it can hold multiple carts but in the example just one. SessioService can also be used – all depends on the further usage.

InMemoryCartService code example

  1. Why making getInMemoryCart by code? Because we need to tell the promo engine that results need to be applied to InMemoryCart not to regular Cart.

getInMemoryCart code example

This is only one of few places where you need to do the trick. This may depend on SAP Commerce version.

When we locate all places we can try to ask promo engine for real result of promotion (not potential). This will be exactly the price that appears when user add this product to cart. Ha can have other items in cart and other promotions can be applied. How you use it later is business related.

Lets put it together in most basic example

Basic InMemoryCart promotional calculation example

Adding a cart could look like this.

Adding a cart code example

PromotionCalculationData can have, for example, promotions code, basePrie and discountPrice if any promotion was applied.

Below you can see the timing which is based on a real example.

Timing based on a real example

As a result we achieved next positive effects after promotional prices calculation method change:

The conclusion is simple – in our case with the need to display the products’ prices for the previous 30 days period involving complex multiple calculations for promotional prices only for displaying them, the solution using InMemoryCart could be the best option to leverage instead of using the regular Cart.