How to create a custom promotion action in Sitecore Commerce 8.2.1

Like the qualifications, when we create a promotion in the “Pricing & Promotions” module, there is a section where we can add the actions we want to execute when the conditions are met, that section is called “Benefits”:

Benefits
Benefits

By default Sitecore Commerce 8.2.1 comes with the following actions:

Built-in actions list
Built-in actions list

Custom action

In the previous post we saw how to create a custom condition to be used in a promotion, but what if, as in the case of conditions, we do not have the action or benefit that we want to execute when the conditions are met?

In this post we will see how to create a custom action.

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.

The action that we are going to “create” will allow a selected product to be added to the shopping cart for free (we will not fully implement the action, we will only create the structure of the class and we will see ideas on how to complete the implementation as it is beyond the scope of this post):

// 1
using Sitecore.Framework.Rules;

namespace MyShop.Commerce.Plugin.Custom.Actions
{
    // 2
    [EntityIdentifier("Add free [specific] product")]
    // 3
    public class AddFreeProductAction : IAction
    {
        // 4
        public IRuleValue TargetItemId { get; set; }

        // 5
        public void Execute(IRuleExecutionContext context)
        {
            // Action implementation
        }
    }
}
  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 action metadata, in this case the identifier that will be displayed later in the SPEAK application UI.
  3. The class must implement the IAction interface, this is how we let the Sitecore rules framework know that this is an action.
  4. We define the IRuleValue property named “TargetItemId”, which is a generic type where we can define the data type of the property (in this case string), through which we can then provide in the UI the product that we want to add for free.
  5. We implement the Execute() method that is defined in the IAction interface, which is responsible for executing the desired action, in this case adding the product to the shopping cart free of charge. As we said we will not provide the implementation of the action but as a recommendation, you can use the IAddCartLinePipeline pipeline to add the product to the shopping cart and don’t forget to add a CartLineLevelAwardedAdjustment adjustment to the shopping cart to specify the amount to be discounted.

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

New custom action
New custom action

After selecting the action and clicking “OK”, in the next step, Sitecore asks us to provide the values for all the properties defined as IRuleValue, in this case only “Target Item Id” providing a control to look for products in the catalog:

Select product
Select product

Finally we can see the new action added in the list of benefits:

Actions list with new action
Actions list with new action

Conclusions

As with conditions, creating the necessary “infrastructure” for a new action is easy, the difficulty depends only on the implementation of the Execute(), that is, on the complexity of the concrete actions that we want to do when the action is executed.

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

Leave a Reply

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