sitecore 9 commerce override SitecoreHow-To

Sitecore Commerce 9: How to override Policy

Policy in Sitecore Commerce is a named, versionable and variable set of data that can be used as facts within behaviors to influence behavioral outcomes. For better understanding I will provide Plugin example below:

   
using Sitecore.Commerce.Core;
using System.Collections.Generic;

namespace Sitecore.Commerce.Plugin.DigitalItems
{
  public class KnownEntitlementsTags : Policy
  {
    public KnownEntitlementsTags()
    {
      this.WarrantyTags = (IList<string>) new List<string>()
      {
        "Warranty"
      };
      this.InstallationTags = (IList<string>) new List<string>()
      {
        "Installation",
        "Service"
      };
      this.DigitalProductTags = (IList<string>) new List<string>()
      {
        "OnlineTraining",
        "OnlineLearning",
        "Subscription",
        "DigitalSubscription"
      };
    }

    public IList<string> WarrantyTags { get; set; }

    public IList<string> DigitalProductTags { get; set; }

    public IList<string> InstallationTags { get; set; }
  }
}

This Policy is available in Sitecore Commerce OOTB. The policy consists of some properties and default constructor. As we can see in the code, there is a default list of DigitalProductTags which will indicate products as digital products. Digital products have a bit different behavior if we will compare them with regular products. One of the differences is that the digital product has infinity availability (no inventory sets).

If you want to mark your product as digital, you need to add any tag to the product or product variant from list below:

  • OnlineTraining
  • OnlineLearning
  • Subscription
  • DigitalSubscription

For some reason, you can decide that you need to have your own custom tag for digital products indication and you don’t want to use OOTB tags. In this case, you need to override DigitalProductTags. How can you do it?

It’s pretty simple. You need to find configuration JSON file for your Environment. By default it should be c:\inetpub\wwwroot\CommerceAuthoring_Sc9\wwwroot\data\Environments\ PlugIn.Habitat.CommerceAuthoring-1.0.0.json

First of all, you need to be sure, that PlugIn.Habitat.CommerceAuthoring-1.0.0.json doesn’t have already defined Policy configuration with type Sitecore.Commerce.Plugin.DigitalItems. KnownEntitlementsTags, Sitecore.Commerce.Plugin.DigitalItems.

If it exists - you can just add a new tag in DigitalProductTags list. If not – you need to copy and paste configuration bellow to Policies list:

   
{
        "$type": "Sitecore.Commerce.Plugin.Availability.DigitalItemTagsPolicy, Sitecore.Commerce.Plugin.Availability",
        "TagList": {
						"entitlement",
						"service",
						"installation",
						"subscription",
						"digitalsubscription",
						"warranty",
						"onlinetraining",
						"onlinelearning",
						"giftcards",
						"{your_custom_tag}"
					}
},
{
        "$type": "Sitecore.Commerce.Plugin.DigitalItems.KnownEntitlementsTags, Sitecore.Commerce.Plugin.Availability",
        "WarrantyTags": {
						"Warranty"
					},
		"InstallationTags": {
						"Installation",
						"Service"
					},
		"DigitalProductTags": {
						"OnlineTraining",
						"OnlineLearning",
						"Subscription",
						"DigitalSubscription",
						"{your_custom_tag}"
					},
} 

After that, you need to run Bootstrap() for your environment and add the custom tag to your products.

The same overrides you can add to all existing policies in Sitecore Commerce Engine which will be based on Policy type.

Need to note that this relates to Sitecore Commerce 8.2.1 and 9 versions. Let me know if this helped you.