CommerceSitecoreUncategorized

Adding new conditional logical operators(and/or..) to Promotions Qualifications

Promotions: Sitecore Commerce promotions are based on Qualifications and Actions. In this article, I will mainly talk about Qualifications.

A qualification is a set of conditions that has to be true for a promotion to be applied to a cart. While each condition represents a Rule in Sitecore world. If you have worked with Rules in Sitecore, it’s the same concept. An example of such a condition would be “Customer is purchasing more than 5 items”.

When we define multiple conditions in qualification, we have to add logical condition operators between these conditions. e.g. “Customer is purchasing more than 5 itemsAnd “One of the items in cart is Nike Product“.  OOTB Sitecore Commerce supports two logical operators; And, Or. So the previous example can also be “Customer is purchasing more than 5 items” OR “One of the items in cart is Nike Shirt

ConditionsOperators

Recently working on a project we had to implement the following examples of qualifications.

  1. “Customer is purchasing more than 5 items” AndOne of the items is Nike Product
  2. “Customer is purchasing more than 5 items” And “None of the items is Nike Product
  3. “Customer is purchasing more than 5 items” Or “One of the items is Nike Product

Please consider Nike Product as a Category here. The above examples are just for reference. We had to implement a lot of those. Since most of the conditions were not available out of the box, we had to implement all the conditions. If you look at the examples above, there are following conditions that we need to have. In our case, most of the conditions we had to create because they were not available OOTB.

  1. Customer is purchasing more than 5 items
  2. One of the items is Nike Product
  3. None of the items is Nike Product

Condition 2 is inverse of 3. If we go down this path, we will be creating an inverse condition of each condition we wanted to have. So rather than doing that, I decided to add an AndNot operator in qualifications. This will give the flexibility to use any condition(OOTB or Custom) to be used as the inverse of itself.

The condition #2 in the example above became

“Customer is purchasing more than 5 items” AndNot “One of the items is Nike Product

If you think of different examples of conditions we get OOTB, e.g. Customer has registered, if you want to inverse it, you will have to create a contrary condition for each. But if you add AndNot it will eliminate the need for it.

How I did: First you have to add AndNot option to the dropdown list of condition operators. For that, I had to override the block which adds And and Or  operators to SelectList.

Important: Only custom code is under //Custom Code Started. Rest is copied from Sitecore assebmly

Then I registered this block using ConfigureSitecore

After this, I started seeing AndNot in the list of condition operators

AndNot

 

Not we need to handle the AndNot when it is selected. Sitecore commerce uses Sitecore Rules engine which has the option for AndNot. So I only had to map this new option to Sitecore rules engine operator.

I had to override a block again which gets executed when we create a promotion. This block translates these promotion conditions into Sitecore engine rules. I had to look for AndNot and map it to its counterpart in Sitecoe rules engine.

And then register this block in ConfigureSitecore

 

 

Happy coding!!

Leave a Reply

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