P is for Policy


“You use policies to define Commerce Engine configuration. A policy is a group of settings that control the behavior of a specific piece of functionality within the Commerce Engine. ” – From https://doc.sitecore.com/developers/92/sitecore-experience-commerce/en/commerce-engine-policies.html. Cool, it’s a way to define configuration, but how do I define it, access it, specify it for different environments and ship it as part of my plugin? Examples below are taken from my Taxjar plugin.

Defining a Policy

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sitecore.Commerce.Core;

namespace Kjaneb.Commerce.Plugin.Tax.TaxJar.Policies
    public class TaxJarPolicy : Policy
        public string APIKey { get; set; }
  • A policy will always inherit from Sitecore.Commerce.Core.Policy
  • You should be adding properties that need to be configured per-implementation or environment for your plugin. In this example I have a string property to allow developers to specify an API Key for TaxJar.

Working with Policy Files

Instead of complex instructions and getting users to merge configs you will want to ship your plugin with a default configuration. This can then be overwritten as required for each environment. In this example I am creating a file Plugin.Tax.TaxJar.PolicySet-1.0.0.json. Policy files should be included in the wwwroot/data/environments directory of the Commerce Engine.

  "$type": "Sitecore.Commerce.Core.PolicySet, Sitecore.Commerce.Core",
  "Id": "Entity-PolicySet-TaxjarPolicySet",
  "Version": 1,
  "IsPersisted": false,
  "Name": "TaxjarPolicySet",
  "Policies": {
    "$type": "System.Collections.Generic.List`1[[Sitecore.Commerce.Core.Policy, Sitecore.Commerce.Core]], mscorlib",
    "$values": [
        "$type": "Kjaneb.Commerce.Plugin.Tax.TaxJar.Policies.TaxJarPolicy, Kjaneb.Commerce.Plugin.Tax.TaxJar",
        "APIKey": "118a3baef24277cd844a2c8273bfc25c"

A policy set is used to configure a group of policies and can be easily referenced in another policy set for easy inclusion in a project. In my PlugIn.Habitat.CommerceAuthoring-1.0.0.json file I have the following:

    "$type": "Sitecore.Commerce.Core.PolicySetPolicy, Sitecore.Commerce.Core",
    "PolicySetId": "Entity-PolicySet-TaxjarPolicySet"

This will use the values defined in the PlugIn.Tax.TaxJar.PolicySet,json above.

Accessing Values Defined in PoliCies

To use and access the properties defined in your policy you can use the CommercePipelineExecutionContext.GetPolicy<T> method. This will read the values from the Policy data store.

One Last Thing – Bootstrapping

When you add, remove or edit policy files you will need to bootstrap your application. To do this you can use the Bootstrap Sitecore Commerce request in Postman which hits the Commerce Ops url at: ttps://localhost:5000/commerceops/Bootstrap()

About the author

Kelly Brennan

Add comment

By Kelly Brennan

Recent Posts

Recent Comments