Skip to content

Instantly share code, notes, and snippets.

@jstemerdink
Last active July 25, 2016 15:09
Show Gist options
  • Save jstemerdink/7c32d51074c2dbd6cbda3c7733ebc072 to your computer and use it in GitHub Desktop.
Save jstemerdink/7c32d51074c2dbd6cbda3c7733ebc072 to your computer and use it in GitHub Desktop.

Some extension methods and helpers for dealing with promotions and campaigns

Compiled from earlier blogposts and comments, which can be found here

Powered by ReSharper image

/// <summary>
/// Class PromotionExtensions.
/// </summary>
/// <author>Jeroen Stemerdink</author>
public static class PromotionExtensions
{
/// <summary>
/// The <see cref="ILogger" /> instance
/// </summary>
private static readonly ILogger Log = LogManager.GetLogger();
/// <summary>
/// The <see cref="IContentLoader" />
/// </summary>
private static IContentLoader contentLoader;
/// <summary>
/// The <see cref="IPromotionEngine" />
/// </summary>
private static IPromotionEngine promotionEngine;
/// <summary>
/// The <see cref="PromotionEngineContentLoader" />
/// </summary>
private static PromotionEngineContentLoader promotionEngineContentLoader;
/// <summary>
/// The<see cref="PromotionFilters" />
/// </summary>
private static PromotionFilters promotionFilters;
/// <summary>
/// The <see cref="PromotionProcessorResolver" />
/// </summary>
private static PromotionProcessorResolver promotionProcessorResolver;
/// <summary>
/// Gets or sets the <see cref="IContentLoader" />.
/// </summary>
/// <value>The <see cref="IContentLoader" />.</value>
/// <exception cref="ActivationException">if there are errors resolving the service instance.</exception>
private static IContentLoader ContentLoader
{
get
{
if (contentLoader != null)
{
return contentLoader;
}
contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
return contentLoader;
}
}
/// <summary>
/// Gets or sets the <see cref="IPromotionEngine" />.
/// </summary>
/// <value>The <see cref="IPromotionEngine" />.</value>
/// <exception cref="ActivationException">if there are errors resolving the service instance.</exception>
private static IPromotionEngine PromotionEngine
{
get
{
if (promotionEngine != null)
{
return promotionEngine;
}
promotionEngine = ServiceLocator.Current.GetInstance<IPromotionEngine>();
return promotionEngine;
}
}
/// <summary>
/// Gets or sets the <see cref="PromotionEngineContentLoader" />.
/// </summary>
/// <value>The <see cref="PromotionEngineContentLoader" />.</value>
/// <exception cref="ActivationException">if there are errors resolving the service instance.</exception>
private static PromotionEngineContentLoader PromotionEngineContentLoader
{
get
{
if (promotionEngineContentLoader != null)
{
return promotionEngineContentLoader;
}
promotionEngineContentLoader = ServiceLocator.Current.GetInstance<PromotionEngineContentLoader>();
return promotionEngineContentLoader;
}
}
/// <summary>
/// Gets or sets the <see cref="PromotionFilters" />.
/// </summary>
/// <value>The <see cref="PromotionFilters" />.</value>
/// <exception cref="ActivationException">if there are errors resolving the service instance.</exception>
private static PromotionFilters PromotionFilters
{
get
{
if (promotionFilters != null)
{
return promotionFilters;
}
promotionFilters = ServiceLocator.Current.GetInstance<PromotionFilters>();
return promotionFilters;
}
}
/// <summary>
/// Gets or sets the <see cref="PromotionProcessorResolver" />.
/// </summary>
/// <value>The <see cref="PromotionProcessorResolver" />.</value>
/// <exception cref="ActivationException">if there are errors resolving the service instance.</exception>
private static PromotionProcessorResolver PromotionProcessorResolver
{
get
{
if (promotionProcessorResolver != null)
{
return promotionProcessorResolver;
}
promotionProcessorResolver = ServiceLocator.Current.GetInstance<PromotionProcessorResolver>();
return promotionProcessorResolver;
}
}
/// <summary>
/// Gets the associated catalog content.
/// </summary>
/// <param name="promotion">The promotion.</param>
/// <returns>ReadOnlyCollection&lt;CatalogContentBase&gt;.</returns>
public static ReadOnlyCollection<CatalogContentBase> GetAssociatedContent(this PromotionData promotion)
{
List<CatalogContentBase> associatedContent = new List<CatalogContentBase>();
try
{
IPromotionProcessor promotionProcessor = PromotionProcessorResolver.ResolveForPromotion(promotion);
PromotionItems promotionItems = promotionProcessor.GetPromotionItems(promotion);
foreach (ContentReference contentReference in promotionItems.Condition.Items)
{
CatalogContentBase catalogContent;
if (!ContentLoader.TryGet(contentReference, out catalogContent))
{
continue;
}
associatedContent.Add(catalogContent);
}
return new ReadOnlyCollection<CatalogContentBase>(associatedContent);
}
catch (ActivationException activationException)
{
Log.Critical(activationException.Message, activationException);
}
catch (NotImplementedException notImplementedException)
{
Log.Error(notImplementedException.Message, notImplementedException);
}
catch (ArgumentNullException argumentNullException)
{
Log.Error(argumentNullException.Message, argumentNullException);
}
return new ReadOnlyCollection<CatalogContentBase>(associatedContent);
}
/// <summary>
/// Gets the associated content for a <see cref="SalesCampaign" />.
/// </summary>
/// <param name="campaignReference">The sales campaign reference.</param>
/// <returns>
/// System.Collections.ObjectModel.ReadOnlyCollection&lt;
/// EPiServer.Commerce.Catalog.ContentTypes.CatalogContentBase&gt;.
/// </returns>
public static ReadOnlyCollection<CatalogContentBase> GetAssociatedContent(
this ContentReference campaignReference)
{
List<CatalogContentBase> associatedContent = new List<CatalogContentBase>();
try
{
List<PromotionItems> promotionItems =
PromotionEngine.GetPromotionItemsForCampaign(campaignReference).ToList();
foreach (PromotionItems promotionItem in promotionItems)
{
foreach (ContentReference contentReference in promotionItem.Condition.Items)
{
CatalogContentBase catalogContent;
if (!ContentLoader.TryGet(contentReference, out catalogContent))
{
continue;
}
associatedContent.Add(catalogContent);
}
}
return new ReadOnlyCollection<CatalogContentBase>(associatedContent);
}
catch (ActivationException activationException)
{
Log.Critical(activationException.Message, activationException);
}
catch (ArgumentNullException argumentNullException)
{
Log.Error(argumentNullException.Message, argumentNullException);
}
return new ReadOnlyCollection<CatalogContentBase>(associatedContent);
}
/// <summary>
/// Gets the associated content for a <see cref="SalesCampaign" />.
/// </summary>
/// <param name="salesCampaign">The sales campaign.</param>
/// <returns>
/// System.Collections.ObjectModel.ReadOnlyCollection&lt;
/// EPiServer.Commerce.Catalog.ContentTypes.CatalogContentBase&gt;.
/// </returns>
public static ReadOnlyCollection<CatalogContentBase> GetAssociatedContent(this SalesCampaign salesCampaign)
{
return salesCampaign.ContentLink.GetAssociatedContent();
}
/// <summary>
/// Gets the associated catalog content by name.
/// </summary>
/// <param name="promotionName">Name of the promotion.</param>
/// <returns>ReadOnlyCollection&lt;CatalogContentBase&gt;.</returns>
public static ReadOnlyCollection<CatalogContentBase> GetAssociatedContentByName(string promotionName)
{
try
{
PromotionData selectedPromotion = GetPromotionByName(promotionName);
return selectedPromotion.GetAssociatedContent();
}
catch (ActivationException activationException)
{
Log.Critical(activationException.Message, activationException);
}
catch (ArgumentException argumentException)
{
Log.Error(argumentException.Message, argumentException);
}
return new ReadOnlyCollection<CatalogContentBase>(new List<CatalogContentBase>());
}
/// <summary>
/// Gets the associated catalog content by Url segment.
/// </summary>
/// <param name="segment">Url segment.</param>
/// <returns>ReadOnlyCollection&lt;CatalogContentBase&gt;.</returns>
public static ReadOnlyCollection<CatalogContentBase> GetAssociatedContentBySegment(string segment)
{
try
{
PromotionData selectedPromotion = GetPromotionByUrlSegment(segment);
return selectedPromotion.GetAssociatedContent();
}
catch (ActivationException activationException)
{
Log.Critical(activationException.Message, activationException);
}
catch (ArgumentException argumentException)
{
Log.Error(argumentException.Message, argumentException);
}
return new ReadOnlyCollection<CatalogContentBase>(new List<CatalogContentBase>());
}
/// <summary>
/// Gets the associated catalog references.
/// </summary>
/// <param name="promotion">The promotion.</param>
/// <returns>ReadOnlyCollection&lt;ContentReference&gt;.</returns>
public static ReadOnlyCollection<ContentReference> GetAssociatedContentReferences(this PromotionData promotion)
{
try
{
IPromotionProcessor promotionProcessor = PromotionProcessorResolver.ResolveForPromotion(promotion);
PromotionItems promotionItems = promotionProcessor.GetPromotionItems(promotion);
return new ReadOnlyCollection<ContentReference>((IList<ContentReference>)promotionItems.Condition.Items);
}
catch (ActivationException activationException)
{
Log.Critical(activationException.Message, activationException);
}
catch (NotImplementedException notImplementedException)
{
Log.Error(notImplementedException.Message, notImplementedException);
}
catch (ArgumentNullException argumentNullException)
{
Log.Error(argumentNullException.Message, argumentNullException);
}
return new ReadOnlyCollection<ContentReference>(new List<ContentReference>());
}
/// <summary>
/// Gets the associated content referencesfor a <see cref="SalesCampaign" />.
/// </summary>
/// <param name="campaignReference">The sales campaign reference.</param>
/// <returns>ReadOnlyCollection&lt;ContentReference&gt;.</returns>
public static ReadOnlyCollection<ContentReference> GetAssociatedContentReferences(
this ContentReference campaignReference)
{
List<ContentReference> associatedContentReferences = new List<ContentReference>();
try
{
List<PromotionItems> promotionItems =
PromotionEngine.GetPromotionItemsForCampaign(campaignReference).ToList();
foreach (PromotionItems promotionItem in promotionItems)
{
associatedContentReferences.AddRange(promotionItem.Condition.Items);
}
return new ReadOnlyCollection<ContentReference>(associatedContentReferences);
}
catch (ActivationException activationException)
{
Log.Critical(activationException.Message, activationException);
}
catch (ArgumentNullException argumentNullException)
{
Log.Error(argumentNullException.Message, argumentNullException);
}
return new ReadOnlyCollection<ContentReference>(associatedContentReferences);
}
/// <summary>
/// Gets the associated content referencesfor a <see cref="SalesCampaign" />.
/// </summary>
/// <param name="salesCampaign">The sales campaign.</param>
/// <returns>ReadOnlyCollection&lt;ContentReference&gt;.</returns>
public static ReadOnlyCollection<ContentReference> GetAssociatedContentReferences(
this SalesCampaign salesCampaign)
{
return salesCampaign.ContentLink.GetAssociatedContentReferences();
}
/// <summary>
/// Gets the catalog references by name.
/// </summary>
/// <param name="promotionName">Name of the promotion.</param>
/// <returns>ReadOnlyCollection&lt;ContentReference&gt;.</returns>
public static ReadOnlyCollection<ContentReference> GetAssociatedContentReferencesByName(string promotionName)
{
try
{
PromotionData selectedPromotion = GetPromotionByName(promotionName);
return selectedPromotion.GetAssociatedContentReferences();
}
catch (ActivationException activationException)
{
Log.Critical(activationException.Message, activationException);
}
catch (ArgumentException argumentException)
{
Log.Error(argumentException.Message, argumentException);
}
return new ReadOnlyCollection<ContentReference>(new List<ContentReference>());
}
/// <summary>
/// Gets the catalog references by Url segment.
/// </summary>
/// <param name="segment">The segment.</param>
/// <returns>ReadOnlyCollection&lt;ContentReference&gt;.</returns>
public static ReadOnlyCollection<ContentReference> GetAssociatedContentReferencesBySegment(string segment)
{
try
{
PromotionData selectedPromotion = GetPromotionByUrlSegment(segment);
return selectedPromotion.GetAssociatedContentReferences();
}
catch (ActivationException activationException)
{
Log.Critical(activationException.Message, activationException);
}
catch (ArgumentNullException argumentNullException)
{
Log.Error(argumentNullException.Message, argumentNullException);
}
catch (ArgumentException argumentException)
{
Log.Error(argumentException.Message, argumentException);
}
return new ReadOnlyCollection<ContentReference>(new List<ContentReference>());
}
/// <summary>
/// Gets the promotion by name.
/// </summary>
/// <param name="promotionName">Name of the promotion.</param>
/// <returns>PromotionData.</returns>
public static PromotionData GetPromotionByName(string promotionName)
{
try
{
IEnumerable<PromotionData> promotions = PromotionEngineContentLoader.GetPromotions();
PromotionFilterContext promotionFilterContext = PromotionFilters.Filter(
promotions,
new List<string>(),
RequestFulfillmentStatus.None);
PromotionData selectedPromotion =
promotionFilterContext.IncludedPromotions.FirstOrDefault(
p => p.Name.Equals(promotionName, StringComparison.OrdinalIgnoreCase));
return selectedPromotion;
}
catch (ActivationException activationException)
{
Log.Critical(activationException.Message, activationException);
}
catch (ArgumentException argumentException)
{
Log.Error(argumentException.Message, argumentException);
}
return null;
}
/// <summary>
/// Gets the promotion by Url segment.
/// </summary>
/// <param name="segment">Url segment.</param>
/// <returns>PromotionData.</returns>
public static PromotionData GetPromotionByUrlSegment(string segment)
{
try
{
IEnumerable<PromotionData> promotions = PromotionEngineContentLoader.GetPromotions();
PromotionFilterContext promotionFilterContext = PromotionFilters.Filter(
promotions,
new List<string>(),
RequestFulfillmentStatus.None);
PromotionData selectedPromotion =
promotionFilterContext.IncludedPromotions.FirstOrDefault(
p =>
UrlSegment.GetUrlFriendlySegment(p.Name).Equals(segment, StringComparison.OrdinalIgnoreCase));
return selectedPromotion;
}
catch (ActivationException activationException)
{
Log.Critical(activationException.Message, activationException);
}
catch (ArgumentException argumentException)
{
Log.Error(argumentException.Message, argumentException);
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment