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:
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…
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:
- We import the namespace Sitecore.Framework.Rules which contains interfaces and classes that we need to use during the implementation of the class.
- 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.
- The class must implement the ICondition interface, this is how we let the Sitecore rules framework know that this is a condition.
- 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.
- 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.
- 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:
If we choose this type of condition, the second step is to provide the values of the properties that we defined as IRuleValue:
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:
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.