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
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.
LikeLike
Guess I missed that one. But in which version is that method available? I am using 9.19.1 now. There you only have
IPromotionProcessor.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
LikeLike
Found it BTW, did some digging. It’s an extension method on IPromotionEngine
LikeLike
Sorry I was unclear about that.
LikeLike