Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Exclude products from promotions

Describes how to configure filters to exclude a product from the promotion engine in Optimizely Commerce Connect. Two APIs are available.

Exclude catalog items from all promotions or from a single promotion through the APIs described in the following sections.

Classes in this topic are available in the following namespace:

  • EPiServer.Commerce.Marketing

Exclude catalog items on all promotions

Use this code to ignore catalog items when evaluating promotions and applying discounts. For example, while promotions on sneakers may run occasionally, you might never want the Faded Glory Mens Canvas Slip-On Shoe to receive a discount or to be included in discount-related events such as Buy at least X items, get the following discount.

To exclude a product from the promotion engine, implement the IEntryFilter. In most cases, you can use the EntryFilterSettings class to configure the default implementation. If that does not meet your needs, create your own IEntryFilter implementation.

The default implementation of IEntryFilter uses the EntryFilterSettings class to implement the Filter method. If you use that implementation, add filters to the EntryFilterSettings instance. The following example illustrates various ways to configure the settings.

The following example adds filters when a site is initialized:

📘

Note

If you want the filters to work on the front end and the back end site, you must configure them on both sites.

namespace EPiServer.Reference.Commerce.Site.Infrastructure {
  [ModuleDependency(typeof (EPiServer.Commerce.Initialization.InitializationModule))]
  public class SiteInitialization: IConfigurableModule {
    public void Initialize(InitializationEngine context) {
      SetupExcludedPromotionEntries(context);
    }
    public void ConfigureContainer(ServiceConfigurationContext context) {}
    public void Uninitialize(InitializationEngine context) {}
    private void SetupExcludedPromotionEntries(InitializationEngine context) {
      var filterSettings = context.Locate.Advanced.GetInstance<EntryFilterSettings>();
      filterSettings.ClearFilters();
      //Add filter predicates for a whole content type.
      filterSettings.AddFilter<TypeThatShouldNeverBeIncluded>(x => false);
      //Add filter predicates based on any property of the content type, including implemented interfaces.
      filterSettings.AddFilter<IInterfaceThatCanBeImplementedToDetermineExclusion>(x => !x.ShouldBeExcluded);
      //Add filter predicates based on meta fields that are not part of the content type models, for example, if the field is dynamically added to entries during import or integration.
      filterSettings.AddFilter<EntryContentBase>(x => !(bool)(x["ShouldBeExcludedPromotionMetaField"] ?? false));
      //Add filter predicates based on the following codes.
      var ExcludingCodes = new string[] {
        "SKU-36127195",
        "SKU-39850363",
        "SKU-39101253"
      };
      filterSettings.AddFilter<EntryContentBase>(x => !ExcludingCodes.Contains(x.Code));
    }
  }
}

Exclude catalog items from a promotion

You can exclude catalog items from a promotion (discount) through the Marketing (UI).

Set this through the API by setting the promotion's ExcludedCatalogItems property to a list of ContentReference objects. The following example sets ExcludedCatalogItems for a promotion:

promotion.ExcludedCatalogItems = new List<ContentReference>() { catalogLink, categoryLink, productLink };

Use ContentReferences to specify catalogs, categories, and entries.

📘

Note

The exclusion is always recursive. If you add a category to ExcludedCatalogItems, all subcategories and their subcategories are also excluded.

To change the default method for excluding catalog items, implement the IPromotionExcludedCatalogItemService interface and register it in one of your initialization modules. For more information, see Initialization.

The following example shows the interface and a custom implementation:

public interface IPromotionExcludedCatalogItemService {
  /// <summary>
  /// Gets the line item codes that are excluded by the promotion setting.
  /// </summary>
  /// <param name="promotionData">The promotion.</param>
  /// <param name="codes">Codes of line items in the order.</param>
  IEnumerable<string> GetExcludedLineItemCodes(PromotionData promotionData, IEnumerable<string> codes);
}

namespace EPiServer.Reference.Commerce.Site {
  [ServiceConfiguration(typeof (IPromotionExcludedCatalogItemService), Lifecycle = ServiceInstanceScope.Singleton)] public class CustomPromotionExcludedCatalogItemService: IPromotionExcludedCatalogItemService {
    IEnumerable<string> IPromotionExcludedCatalogItemService.GetExcludedLineItemCodes(PromotionData promotionData, IEnumerable<string> codes) {
      return Enumerable.Empty<string>();
    }
  }
}

public void ConfigurationContainer(ServiceConfigurationContext context) {
  var services = context.Services;
  services.RemoveAll<IPromotion.ExcludedCatalogItemsService>();
  services.AddSingleton<IPromotion.ExcludedCatalogItemsService, CustomPromotionExcludedCatalogItemService>();
}

Related blog post: Exclude catalog items per promotion