How to create a custom condition in Sitecore Commerce 8.2.1

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 Launchpad icon
Pricing and Promotions SPEAK app Launchpad icon

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…

Condition type selection
Condition type selection

Custom condition

What if none of the existing conditions meet the needs of the business rules for the promotion we need to create? It is here when 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:

// 1
using Sitecore.Framework.Rules;

namespace MyShop.Commerce.Plugin.Custom.Conditions
{
    // 2
    [EntityIdentifier("Is it an odd number?")]
    // 3
    public class IsOddNumberCondition : ICondition
    {
        // 4
        public IRuleValue Number { get; set; }

        // 5
        public bool Evaluate(IRuleExecutionContext context)
        {
            // 6
            return Number.Yield(context) % 2 != 0;
        }
    }
}
  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:

New custom condition added
New custom condition added

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

IRuleValue property value in the UI
IRuleValue property value in the UI

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:

Qualifications
Qualifications

Conclusions

We saw how simple it is to add a new custom condition, in this case its implementation was very simple but 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.

How to create a custom condition in Sitecore Commerce 8.2.1
Tagged on:         

One thought on “How to create a custom condition in Sitecore Commerce 8.2.1

Leave a Reply

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