allBlogsList

Sitecore Experience Commerce - Override a promotion benefit action

Sitecore Commerce 9 comes with a set of built-in Promotion Qualifications and Benefits.

In some situations you might need to create custom Qualifications or Benefit actions. Here's a good article on the subject: https://sitecoresmurf.wordpress.com/2019/05/01/create-custom-promotion-benefit-action-with-sitecore-commerce-9/

In other situations you'd rather override/replace an existing Qualification or Benefit to introduce a small change. I was in this situation lately where i wanted to exclude donation products from Percentage based Promotion calculations.

In this blog post, i will walk you through this example and how i did override an OOB Benefit action. (Same process can be applied to qualification actions)

In this example we will be overriding this Benefit action 'CartItemSubtotalPercentOffAction'.

In order to do this we need to follow these steps:

Step 1: First, we need to create a new action class and create an Execute method that would replace the implementation from CartItemSubtotalPercentOffAction**.**

 Sitecore Commerce Benefit Actions 1

Depending on your requirements, you can also derive from CartItemSubtotalPercentOffAction and override specific methods.

Example:

Sitecore Commerce Benefit Actions 4

 Important: Note here that we use the same EntityIdentifier in order to replace the original implementation.

Step 2: Override GetActionsMetadataBlock used in IGetActionsPipeline to exclude CartItemSubtotalPercentOffAction : 

  Sitecore Commerce Benefit Actions 2

Step 3: Configure IGetActionsPipeline to use the previously created block:

  Sitecore Commerce Benefit Actions 3

This has been implemented and tested on Sitecore Experience Commerce 9.0.2

Conclusion:

As you can see it is quite easy to replace any Qualification or Benefit action by your own implementation by following these 3 easy steps.

P.S: If you intend to do this more than once, it might be useful to create a Policy that contains a list of OOB Actions to replace, and then use that policy in GetActionsMetadataBlock.

View more blogs and tutorials about Sitecore Commerce

Learn about our Sitecore Commerce work.