By default Sitecore Commerce 8.2.1 comes with the following actions:
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):
- 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 action metadata, in this case the identifier that will be displayed later in the SPEAK application UI.
- The class must implement the IAction interface, this is how we let the Sitecore rules framework know that this is an action.
- 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.
- 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:
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:
Finally we can see the new action added in the list of benefits:
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.