Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Using rendering templates to render your campaign, promotion

An example of how you can use a rendering template to display your banner(s) for you campaigns or promotions.

The Campaign.cshtml file is an extended Index.cshtml file for the Campaign page in Quicksilver

Blogpost can be found here

Updated as discussed in the comments

Powered by ReSharper image

@using EPiServer.Globalization
@using EPiServer.Web
@using EPiServer.Web.Routing
@model EPiServer.Commerce.Marketing.EntryPromotion
@{
string imageUrl = UrlResolver.Current.GetUrl(
Model.Banner,
null,
new VirtualPathArguments { ContextMode = ContextMode.Default });
string urlSegment = UrlSegment.GetUrlFriendlySegment(Model.Name);
}
<div class="jumbotron" style="background-image: url(@imageUrl)">
<div class="container">
<h1>@Model.Name</h1>
<p>@Model.Description</p>
<p>
<a class="btn btn-primary btn-lg" href="/@ContentLanguage.PreferredCulture.Name/promotions/@urlSegment" role="button">Learn more</a>
</p>
</div>
</div>
@using EPiServer
@using EPiServer.Commerce.Marketing
@using EPiServer.ServiceLocation
@using EPiServer.Web
@using EPiServer.Web.Routing
@model SalesCampaign
@{
IContentLoader contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
List<PromotionData> promotions = contentLoader.GetChildren<PromotionData>(Model.ContentLink).Where(p => p.IsActive).ToList();
PromotionFilters promotionFilters = ServiceLocator.Current.GetInstance<PromotionFilters>();
PromotionFilterContext promotionFilterContext = promotionFilters.Filter(promotions, new List<string>(), RequestFulfillmentStatus.None);
}
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<div class="carousel-inner" role="listbox">
@foreach (PromotionData promotion in promotionFilterContext.IncludedPromotions)
{
string imageUrl = UrlResolver.Current.GetUrl(
promotion.Banner,
null,
new VirtualPathArguments { ContextMode = ContextMode.Default });
string urlSegment = UrlSegment.GetUrlFriendlySegment(promotion.Name);
<div class="item">
<img src="@imageUrl" alt="@promotion.Name">
<div class="container">
<div class="carousel-caption">
<h1>@promotion.Name</h1>
<p>@promotion.Description</p>
<p>
<a class="btn btn-lg btn-primary" href="/@urlSegment" role="button">Learn more</a>
</p>
</div>
</div>
</div>
}
</div>
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
@using EPiServer
@using EPiServer.Commerce.Catalog.ContentTypes
@using EPiServer.Commerce.Marketing
@using EPiServer.Commerce.Marketing.Internal
@using EPiServer.Commerce.Marketing.Promotions
@using EPiServer.Framework.Localization
@using EPiServer.Reference.Commerce.Site.Features.Product.Models
@using EPiServer.Reference.Commerce.Site.Features.Product.Services
@using EPiServer.Reference.Commerce.Site.Features.Search.Models
@using EPiServer.Reference.Commerce.Site.Features.Search.Services
@using EPiServer.ServiceLocation
@using EPiServer.Web
@using EPiServer.Web.Routing
@model EPiServer.Reference.Commerce.Site.Features.Campaign.Pages.CampaignPage
@{
Layout = "~/Views/Shared/_Layout.cshtml";
string segment;
if (!Model.Property.TryGetPropertyValue("PageExternalURL", out segment))
{
segment = Model.URLSegment;
}
PromotionEngineContentLoader promotionEngineContentLoader = ServiceLocator.Current.GetInstance<PromotionEngineContentLoader>();
IContentLoader contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
IProductService productService = ServiceLocator.Current.GetInstance<IProductService>();
ISearchService searchService = ServiceLocator.Current.GetInstance<ISearchService>();
PromotionFilters promotionFilters = ServiceLocator.Current.GetInstance<PromotionFilters>();
LocalizationService localizationService = ServiceLocator.Current.GetInstance<LocalizationService>();
PromotionProcessorResolver promotionProcessorResolver = ServiceLocator.Current.GetInstance<PromotionProcessorResolver>();
SearchViewModelFactory searchViewModelFactory = new SearchViewModelFactory(localizationService, searchService);
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));
IPromotionProcessor promotionProcessor = promotionProcessorResolver.ResolveForPromotion(selectedPromotion);
PromotionItems promotionItems = promotionProcessor.GetPromotionItems(selectedPromotion);
}
<div class="category-page">
@if (selectedPromotion != null)
{
string imageUrl = UrlResolver.Current.GetUrl(selectedPromotion.Banner, null, new VirtualPathArguments { ContextMode = ContextMode.Default });
<div class="jumbotron" style="background-image: url(@imageUrl)">
<div class="container">
<h1>@selectedPromotion.Name</h1>
<p>@selectedPromotion.Description</p>
</div>
</div>
<div class="product-row jsSearch container-fluid">
<div class="row">
@{
List<ProductViewModel> models = new List<ProductViewModel>();
foreach (ContentReference conditionBlock in promotionItems.Condition.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(variation);
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);
}
}
Html.RenderPartial("_Category", models);
}
</div>
</div>
}
@Html.PropertyFor(x => x.MainContentArea, new { CssClass = "row" })
</div>
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.Framework.Web;
using EPiServer.ServiceLocation;
using EPiServer.Web.Mvc;
namespace EPiServer.Reference.Commerce.Site.Infrastructure
{
using EPiServer.Commerce.Marketing;
[ServiceConfiguration(typeof(IViewTemplateModelRegistrator))]
public class ViewTemplateModelRegistrator : IViewTemplateModelRegistrator
{
public void Register(TemplateModelCollection viewTemplateModelRegistrator)
{
viewTemplateModelRegistrator.Add(typeof(PageData), new TemplateModel
{
Name = "PartialPage",
Inherit = true,
AvailableWithoutTag = true,
TemplateTypeCategory = TemplateTypeCategories.MvcPartialView,
Path = "~/Views/Shared/_Page.cshtml"
});
viewTemplateModelRegistrator.Add(typeof(EntryPromotion), new TemplateModel
{
Name = "EntryPromotion",
Inherit = true,
AvailableWithoutTag = true,
TemplateTypeCategory = TemplateTypeCategories.MvcPartialView,
Path = "~/Views/Shared/_EntryPromotion.cshtml"
});
viewTemplateModelRegistrator.Add(typeof(SalesCampaign), new TemplateModel
{
Name = "SalesCampaign",
Inherit = true,
AvailableWithoutTag = true,
TemplateTypeCategory = TemplateTypeCategories.MvcPartialView,
Path = "~/Views/Shared/_SalesCampaign.cshtml"
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.