Created
December 1, 2018 12:01
-
-
Save Sitecoreclimber/5bb05d234b506e49ad7d86aef2ab51a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Plugin.Demo.ExtendCommerceView | |
{ | |
using Plugin.Demo.ExtendCommerceView.Policies; | |
using Sitecore.Commerce.Core; | |
using Sitecore.Commerce.EntityViews; | |
using Sitecore.Commerce.Plugin.Catalog; | |
using Sitecore.Framework.Pipelines; | |
using System.Linq; | |
using System.Threading.Tasks; | |
[PipelineDisplayName("PopulateSellableItem")] | |
public class PopulateSellableItemImages : PipelineBlock<EntityView, EntityView, CommercePipelineExecutionContext> | |
{ | |
private readonly CommerceCommander _commerceCommander; | |
public PopulateSellableItemImages(CommerceCommander commerceCommander) | |
{ | |
this._commerceCommander = commerceCommander; | |
} | |
public override Task<EntityView> Run(EntityView entityView, CommercePipelineExecutionContext context) | |
{ | |
if (entityView.EntityId.Contains("Entity-Category-")) | |
{ | |
var currentEntityViewArgument = this._commerceCommander.Command<ViewCommander>().CurrentEntityViewArgument(context.CommerceContext); | |
//get the sellableitems section | |
var sellableItemsView = entityView.ChildViews.FirstOrDefault(p => p.Name == "SellableItems"); | |
if (sellableItemsView != null) | |
{ | |
var serverHostName = context.GetPolicy<CMServerPolicy>().ServerHostName; | |
// parse the sellableitems section | |
foreach (var currentsellableItem in (sellableItemsView as EntityView).ChildViews) | |
{ | |
var currentsellableItemFoundEntity = context.CommerceContext.GetObjects<FoundEntity>().FirstOrDefault(p => p.EntityId == (currentsellableItem as EntityView).ItemId); | |
if (currentsellableItemFoundEntity != null) | |
{ | |
var sellableItem = currentsellableItemFoundEntity.Entity as SellableItem; | |
if (sellableItem != null) | |
{ | |
//get the images component | |
var imagesComponent = sellableItem.GetComponent<ImagesComponent>(); | |
var defaultImage = imagesComponent.Images.FirstOrDefault().Replace("-",""); | |
if (defaultImage != null) | |
{ | |
(currentsellableItem as EntityView).Properties.Insert(1, new ViewProperty | |
{ | |
Name = " ", | |
IsHidden = false, | |
IsReadOnly = true, | |
OriginalType = "Html", | |
UiType = "Html", | |
IsRequired = false, | |
Value = $"<img alt='' height=60 width=60 src='{serverHostName}-/media/{defaultImage}.ashx'/>", | |
}); | |
} | |
} | |
} | |
} | |
} | |
} | |
return Task.FromResult(entityView); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment