Backend - Alpha Solutions
Learn how simple it is to add a new custom condition and be creative when creating new conditions for promotions.



During the Sitecore Commerce 8.2.1 installation process, one of the steps is to update/install a series of packages which update/install some modules (SPEAK applications) that allow us to integrate with some Commerce Server functionalities, “Pricing & Promotions” being one of them, once installed we can see in our Sitecore Launchpad an access to this application:

Pricing and promotions - Speak App - Alpha Solutions

If you have created a promotion using this module before, you have probably noticed the “Qualifications” section, as it is required to create a promotion.

The qualifications are conditions that must be fulfilled so that the promotion can be applied, if only one is not fulfilled the promotion will not be applied.

When we add a qualification, the first thing Sitecore asks us is to select the condition type, such as: “Is Cart Item Available?”, “Cart Has Items?”, “Is Cart Contact Registered?”, “Is Current Month?”, among others…

Promotions Qualifications - Alpha Solutions

What if none of the existing conditions meet the needs of the business rules for the promotion we need to create? In this case we need to create a “custom condition”.

To create a custom condition we must create a class in one of our plugins (or create a new plugin) in our Sitecore Commerce Engine solution.

As part of this post we are going to create a very simple condition, which will validate that a specified number is odd. The following code is the condition implementation, which we will explain later:

Custom Condition - Alpha Solutions

  1. We import the namespace Sitecore.Framework.Rules which contains interfaces and classes that we need to use during the implementation of the class.

  2. The EntityIdentifier attribute allows us to set the condition metadata, in this case the identifier that will be displayed later in the SPEAK application UI.

  3. The class must implement the ICondition interface, this is how we let the Sitecore rules framework know that this is a condition.

  4. We define the IRuleValue property named “Number”, which is a generic type where we can define the data type of the property (in this case int), through which we can then provide, in the UI, the number we want to determine if it is odd or not, in other words, it is the mechanism by which we can pass information to the condition from the UI when the condition is created.

  5. We implement the Evaluate() method that is defined in the ICondition interface, which is responsible for determining the result of the condition. You must return true if the condition is evaluated successfully.

  6. We evaluate whether the number is odd or not.

After implementing this class we must deploy the Sitecore Commerce Engine so that Sitecore can “discover” this new condition, as we can see in the following image when we try to add a new condition:

is-number-odd-condition-listed  - Alpha Solutions

If we choose this type of condition, the second step is to provide the values of the properties that we defined as IRuleValue:

Condition Value - Alpha Solutions

As we can see, Sitecore asks us to provide the values for all the properties defined as IRuleValue, in this case only “Number” and finally we can see the new condition added in the list of qualifications:

promotions-qualifications-with-is-number-odd - Alpha Solutions

We saw how simple it is to add a new custom condition, in this case its implementation was very simple, but it can be as complex as we want, being able to get the shopping cart and make decisions based on its state or access the product catalog, price repository or inventory system, etc., in short, we can be very creative when creating new conditions for our promotions, even creating a conditions library that we could reuse in all our projects.