using System; using System.Collections.Generic; using System.Linq; using System.Web; using EPiServer.Core; using EPiServer.Framework; using EPiServer.Campaign.Commerce; using EPiServer.ServiceLocation; using EPiServer.Web.Routing; using EPiServer.Commerce.Catalog.ContentTypes; using EPiServer.Commerce.Catalog.Linking; using EPiServer.Commerce.Marketing; using Mediachase.Commerce.Pricing; namespace EPiServer.Reference.Commerce.Site.Infrastructure { [ServiceConfiguration(ServiceType = typeof(IProductFieldsHandler), Lifecycle = ServiceInstanceScope.Singleton)] internal class CustomProductFieldHandler : IProductFieldsHandler { private readonly IContentLoader _contentLoader; private readonly IUrlResolver _urlResolver; private readonly IRelationRepository _relationRepository; /// /// Initializes a new instance of the class. /// /// The content loader. /// The relation repository. /// The url resolver. public CustomProductFieldHandler(IContentLoader contentLoader, IRelationRepository relationRepository, IUrlResolver urlResolver) { _contentLoader = contentLoader; _relationRepository = relationRepository; _urlResolver = urlResolver; } /// public void PopulateCategoryFields(ProductModel model, EntryContentBase entry) { const string hashSymbol = "#"; var entryAncestors = _contentLoader.GetAncestors(entry.ContentLink); var entryAncestorNames = entryAncestors.Reverse().Skip(1).Take(10).Select(a => a.Name.Replace(hashSymbol, string.Empty)); model.Category = string.Join(hashSymbol, entryAncestorNames); } /// public void PopulateImageFields(ProductModel model, EntryContentBase entry) { var mediaCollection = entry.CommerceMediaCollection; if (mediaCollection == null || !entry.CommerceMediaCollection.Any()) { return; } var contentImages = _contentLoader.GetItems(mediaCollection.Select(p => p.AssetLink), new LoaderOptions() { LanguageLoaderOption.MasterLanguage() }) .OfType() .Take(6) .ToArray(); model.Image1 = contentImages.Length > 0 ? new ProductImageModel { ImageUrl = _urlResolver.GetUrl(contentImages[0].ContentLink) } : null; model.Image2 = contentImages.Length > 1 ? new ProductImageModel { ImageUrl = _urlResolver.GetUrl(contentImages[1].ContentLink) } : null; model.Image3 = contentImages.Length > 2 ? new ProductImageModel { ImageUrl = _urlResolver.GetUrl(contentImages[2].ContentLink) } : null; model.Image4 = contentImages.Length > 3 ? new ProductImageModel { ImageUrl = _urlResolver.GetUrl(contentImages[3].ContentLink) } : null; model.Image5 = contentImages.Length > 4 ? new ProductImageModel { ImageUrl = _urlResolver.GetUrl(contentImages[4].ContentLink) } : null; model.Image6 = contentImages.Length > 5 ? new ProductImageModel { ImageUrl = _urlResolver.GetUrl(contentImages[5].ContentLink) } : null; } /// public void PopulateTextFields(ProductModel model, EntryContentBase entry, IPriceValue price, IEnumerable discountedPrices) { //Customized from DefaultProductFieldsHandler class model.Text1 = entry.DisplayName ?? string.Empty; model.Text2 = entry.Code; model.Text3 = GetProductUrl(entry) ?? string.Empty; model.Text4 = price.UnitPrice.ToString(); model.Text5 = discountedPrices?.FirstOrDefault()?.Price.ToString() ?? string.Empty; } /// public void PopulateAdditionalDataFields(ProductModel model, EntryContentBase entry) { } /// public void PopulateLinkFields(ProductModel model, EntryContentBase entry) { } private string GetProductUrl(EntryContentBase entry) { var productUrl = _urlResolver.GetUrl(entry); if (productUrl != null) { return productUrl; } var parentProductLink = entry.GetParentProducts(_relationRepository).FirstOrDefault(); return parentProductLink != null ? _urlResolver.GetUrl(parentProductLink) : string.Empty; } } }