One of the most powerful yet underutilized tools within Sitecore is the rules engine. By exposing the rules engine to a content author, via the rules field, content authors will be able to easily create complex sets of logic. One example of how to take advantage of the rules field would be to power promotions in an ecommerce solution.
This will be a series of blog posts designed to give an introduction to the Sitecore rules field, the rules engine and how it can be customized to meet the needs of your client. This is written using the freshly released Sitecore 7.1 rev 130926 which saw some significant changes. If you’re using an older version adjustments will need to be made. These blog posts will be written from the perspective of a multisite ecommerce project.
The first post will walk you through adding a rules field to a template and getting that field to use custom conditions and actions. Next, I’ll cover some of the options you have to allow input into a condition or action. In a later post I’ll cover the implementation of those actions and conditions as well as the actual execution of the rules. Finally, I’ll discuss some of the workings of the rules engine and possibilities for customizations and enhancements.
If you haven’t worked with rules within Sitecore before, a rule is made up of a number of conditions and actions. The Rule Set Editor allows conditions to be easily combined together and manipulated. With a few clicks a new condition can be added to a rule and the logic inverted. Meanwhile, a series of actions can be added that will be performed if all of the conditions are met. The rule factory does a lot of the work on determining what logic should be run when and it is fairly simple to create custom conditions and actions.
In this post we’ll cover:
- Creating a new template with a Rules field
- Creating and using a rules context
- Creating a rules tag
- Creating custom conditions and actions
One thing to note is that we’ll be jumping back and forth between items a bit as we go. This may not be the most efficient way to set things up but I feel that it’s a leads to a more natural order of operations when creating new rules.
The first step in using the rules field is to create a new template, named “Promotions”. Let’s give our new template an icon and some standard values. Create a new field section and add a field named Promotion Rules to this section. For the field type use Rules, this is under the System Types.
If you were to view the standard values now our promotion rules field would be active but have no options for applying conditions or actions. To add our custom rules we’ll need to do a little more work.
Under /sitecore/system/Settings/Rules create a new Rules Context Folder and name it Promotions.
Our context will allow us to control which rules to use with our new rules field. This is done by the use of tags but that’s a step or two down the road. For now we need to get our field using the new context. Copy the path for our context folder into the source of the Promotions Rules field on our Promotions template.
At this point we should probably create and start using a tag to tie all of our promotion rules together. Create a tag named “Promotions” under /sitecore/system/Settings/Rules/Definitions/Tags
Later we will be associating different conditions and actions with this tag but for now we need to update the rules context we’re using to include this tag. Switching back to our rule context /sitecore/system/Settings/Rules/Promotions we’ll see that it already includes a number of sub-items. Of interest to us is the ./Tags/Default sub-item. Let’s add our promotion tag to the “Tags” field of this item.
At this point our Promotions Rules field is all set to use any condition or action that is associated with the Promotions tag. We just have to go create those now. Create a new element folder within /sitecore/system/Settings/Rules/Definitions/Elements. This will contain all of the conditions and actions we’ll be creating to support our promotions but first we need to associate this folder with our Promotions tag. Again this is done in the ./Tags/Default item’s Tags field.
It took a bit to get there but at this point any condition or action we create should be available for our Promotions Rule field. Let’s create a sample condition and action now. Create a condition and use the following for the Text field:
when the subtotal is over [Amount]
Since we’re not implementing these quite yet leave the rest of the field blank. We’ll revisit these in a later post.
Create a new Action, using the following for the Text field:
give free shipping to [selection]
If you create a new instance of your promotion template and open the rules editor for the Promotion Rules field you should now have the option to select both our condition and action. Congratulations.
If you try to set this up at this point you’ll probably be greeted with a nasty error message and think, what the heck. This is likely due to the fact that we haven’t identified what code should be run when evaluating a rule. In fact we’ve totally ignored that field. In the next post I’ll cover writing a condition and action.
One thing to point out is that the Amount and selection allow additional input. Clicking one of those will allow you to enter a value. We’ll cover everything you can do with these in the next post but if you want to read ahead take a look at /sitecore/system/Settings/Rules/Definitions/Macros or http://www.sitecore.net/Community/Technical-Blogs/Morten-Ljungberg-Sitecore-Whats-New/Posts/2012/12/Creating-custom-conditions-Part-1.aspx