Sitecore OrderCloud External Promotions - image showing Sitecore and OrderCloud logos as will as 3 icons related to promotions

This article shows how to use external promotions with Sitecore OrderCloud. 

3.18.25

Roland Villemoes

OrderCloud’s Rule Engine

OrderCloud’s rule engine is really strong, easy to use and extremely flexible. Below an example of the "rules" you can create:

 

$50 off line items from a given supplier when you spend more than $100 on that supplier's products:
EligibleExpression"item.SupplierID = '123' and items.total(SupplierID = '123') >= 100"
ValueExpression"50 / items.count(SupplierID = '123')"

 

*) There a full list of examples and good explanations in the documentation

 

As you can see the flexibility is good, and also the XP's can be used in these rules. XPs is simply a piece of json on each object in OrderCloud where you can add "what ever you want". 

As an example you could add data around promotions that's only eligible if at a certain level in your loyalty program. 

 

**) Forgot to warn you: If you haven't guessed yet, this is a technical article.  

 

Sometimes there's special cases where the current rule engine can't be used. In these cases OrderCloud now has an option to call out to an external middleware or external promotion engine. As you can imagine this should only happen if you "really have to do it!". Performance and there-by user experience will take a hit. 

In this article You well see an example on how to set this up and create a minimal middleware that calculates a specific promotion. 

 

Expressions and how to configure OrderCloud

The example below shows and example of a promotion purely handled by OrderCloud.

The Eligible Expression must evaluate to true for this promotion to be evaluated by OrderCloud. 

The Value Expression calculated the promotions value - the discount. 

Showing exanmple of a promotion in Sitecore OrderCloud with rules/formulas for the Eligible Expression and the Value Expression 
 
 
When defining an external promotion in OrderCloud you still have to specific an Eligible Expression and that still has to evaluate to true. 

You then leave the Value Expression empty/null - and you mark a new property on the promotion. Below and example:

 


{
	"ID": "ext-promo",
	"Code": "50OFFMENS",
	"Name": "Buy2WomenGet50OffMens",
	"Description": "Buy 2 items from Womens Category, Get 2 items from Mens Category for 50% off",
	"EligibleExpression": "items.quantity(product.incategory('womens')) >= 2 and items.quantity(product.incategory('mens')) >= 1",
	"ValueExpression": null,
	"Active": true,
	"UseIntegration": true,
	.........
}

 

Besides setting "UseIntegration": true and "ValueExpression": null, you have to tell OrderCloud where call. 

OrderCloud Integration Promotion Endpoint Example

 

The final step is to setup the endpoint that OrderCloud will call. 

But before we do that, let's look at an example of the promotion we need to build outside OrderCloud.

 

Promotion Example

 

Buy 2 items from Category ‘womens’, Get 2 from Category ‘mens’ for 50% off

 

The above promotion is not possible to configure using OrderClouds Promotions and the Rules Engine. The Rules engine is really strong, but it can't handle this one. 

Also, the above promotion must scale as well. So if a customer purchase say 16 products in the 'womens' category, the customer should be getting 50% of from up to 16 items from the 'mens' category - but always giving the discounts to the least-expensive items first. Below the example we will use for our implementation and test. 

 

OrderCloud External Promotion Example

 

Implementing the custom promotion in the Middleware

 

You can find the actual example code in my GitHub repository here (together with other examples with OrderCloud). This is the specific file of interest that implements this promotion example: api/promotion/route.ts

 

This is how it works: 

- OrderCloud post to the specified endpoint

- The request from OrderCloud includes the full order/cart, promotions to evaluate etc. 

- The middleware evaluate the request, and calculates the promotions. 

- The middleware returns whether or not the promotion(s) was accepted and what the discount should be. 

 

Testing the promotion

 

For testing I have created an order/cart in OrderCloud for a specific user, having the cart exactly with the items listed in our example described earlier:

 


{
	"ID": "testuser01cart",
	"FromUser": {
		"Username": "testuser01",
		......
	},
	"LineItemCount": 3,
	"Status": "Unsubmitted",
	"Subtotal": 870,
	"PromotionDiscount": 0,
	"Currency": null,
	"Total": 870,
	.....
}

 

Where the carts line items looks like this:

 


{
	"Items": [
		{
			"ProductID": "womens-hellion-onyx-jacket",
			"Quantity": 4,
			"UnitPrice": 120,
			......
		},
		{
			"ProductID": "mens-hellion-onyx-jacket",
			"Quantity": 2,
			"UnitPrice": 120,
			......
		},
		{
			"ProductID": "mens-intrigue-active-shorts",
			"Quantity": 3,
			"UnitPrice": 50,
			......
		}
	]
}

 

Finally I can add the promotion to the cart using the OrderCloud dashboard. That will trigger the call to my endpoint, and showing this on the console in my vscode:

 

OrderCloud Promotion - Running the example in vscode

 

When I again look at my cart, I can see that the discount has been applied - as expected. 


{
	"ID": "testuser01cart",
	"FromUser": {
		"Username": "testuser01",
		.....
	},
	"LineItemCount": 3,
	"Status": "Unsubmitted",
	"Subtotal": 870,
	"PromotionDiscount": 135,
	"Total": 735,
	.....
}

 

A couple of closing remarks

 

I hope you agree that this feature is pretty strong, and that can solve special cases - if it's really needed. Calling external will be slower, and it will put another dependency on a potentially high performing system. So consequences can be really bad, if it's not architected well, and having full control over WHEN external promotions gets triggered and how often. 

 

On the other hand, this opens up for the use of dedicated promotion engines like Talon.one or Voucherify.io

This could for some business cases make sense. Something to explore in upcoming insight articles. 

 

As always - please reach out to comment, discuss - Always up for a challenge. 

 

OrderCloud Promotion Integration

As part of API v1.0.373 a feature for using external calculations on promotions.

 

This is a very strong feature for flexibility as no ecommerce system and include all possible promotion calculations. 

 

That said, OrderCloud's rule engine is really strong, and more importantly.

 

Using external calculations for specific promotions should be the very last resort you use. 

 

Calling externally for cart and order calculations is obviously not great for performance (and dependencies). 

Sitecore OrderCloud – External Promotions