Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sitecoreclimber/5bb05d234b506e49ad7d86aef2ab51a1 to your computer and use it in GitHub Desktop.
Save Sitecoreclimber/5bb05d234b506e49ad7d86aef2ab51a1 to your computer and use it in GitHub Desktop.
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