Getting all products a promotion applies to

If you want to create a landing page for your promotion, you might want to display all items your promotion applies to.

This is one way you might do it:

In a Promotion (PromotionData) you have a DiscountItems property. This contains ContentReferences to all items your Promotion applies to. This can be ProductContent, VariationContent or NodeContent. To get all products you can loop through the ContentReferences and get the items it points to. For a Node you will need to get all items below that. When you aggregate those, you can display them. Below is a sample based on QuickSilver. How you aggregate them of course depends on your implementation.

List<ProductViewModel> models = new List<ProductViewModel>();
 PropertyData pd = selectedPromotion.Property.FirstOrDefault(p => p.PropertyValueType == typeof(DiscountItems));

 if (pd != null)
 {
     DiscountItems a = pd.Value as DiscountItems;

     if (a != null)
     {
         foreach (ContentReference conditionBlock in a.Items)
         {
             ProductViewModel model;
             ProductContent product;

             if (contentLoader.TryGet(conditionBlock, out product))
             {
                 model = productService.GetProductViewModel(product);
                 models.Add(model);
                 continue;
             }

             VariationContent variation;

             if (contentLoader.TryGet(conditionBlock, out variation))
             {
                 model = productService.GetProductViewModel(product);
                 models.Add(model);
                 continue;
             }

             NodeContent node;

             if (contentLoader.TryGet(conditionBlock, out node))
             {
                 SearchViewModel<NodeContent> nodeContent = searchViewModelFactory.Create(node, new FilterOptionFormModel { FacetGroups = new List<FacetGroupOption>(), Page = 1, PageSize = int.MaxValue });

                 models.AddRange(nodeContent.ProductViewModels);
             }
         }
     }
 }

The complete code, embedded in the view for the campaign page in QuickSilver you can find here.

I have added the implementations discussed in the comments on my previous post in the same Gist

4 thoughts on “Getting all products a promotion applies to

  1. The IPromotionProcessor.GetPromotionItemsForCampaign method is designed to give you this type of information. Since the targets of the promotion are not necessarily described by a DiscountItmes property of the PromotionData the GetPromotionItemsForCampaign method will delegate to each promotion processor to express what items it targets. It operates on a campaign level but the returned result can easily be filtered by promotion if indeed you want to get only the items targeted by a specific promotion.

    Like

    1. Guess I missed that one. But in which version is that method available? I am using 9.19.1 now. There you only haveIPromotionProcessor.GetPromotionItems().
      So in this example I guess you would end up with:

      PromotionProcessorResolver promotionProcessorResolver = ServiceLocator.Current.GetInstance();
      IPromotionProcessor promotionProcessor = promotionProcessorResolver.ResolveForPromotion(selectedPromotion);
      PromotionItems promotionItems = promotionProcessor.GetPromotionItems(selectedPromotion);

      and then loop through the ContentReferences like this
      promotionItems.Condition.Items

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s