Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
namespace Sitecore.Services.Examples.Indexing
{
using System;
using System.Threading.Tasks;
using Sitecore.Commerce.Core;
using Sitecore.Commerce.EntityViews;
using Sitecore.Commerce.Plugin.Catalog;
using Sitecore.Framework.Conditions;
using Sitecore.Framework.Pipelines;
[PipelineDisplayName("GetFacetsViewBlock")]
public class GetFacetsViewBlock : PipelineBlock<EntityView, EntityView, CommercePipelineExecutionContext>
{
private readonly ViewCommander _viewCommander;
public GetFacetsViewBlock(ViewCommander viewCommander)
{
this._viewCommander = viewCommander;
}
public override Task<EntityView> Run(EntityView arg, CommercePipelineExecutionContext context)
{
Condition.Requires(arg).IsNotNull($"{Name}: The argument cannot be null.");
var request = this._viewCommander.CurrentEntityViewArgument(context.CommerceContext);
var catalogViewsPolicy = context.GetPolicy<KnownCatalogViewsPolicy>();
var isConnectView = arg.Name.Equals(catalogViewsPolicy.ConnectSellableItem, StringComparison.OrdinalIgnoreCase);
// Make sure that we target the correct views
if (string.IsNullOrEmpty(request.ViewName) ||
!request.ViewName.Equals(catalogViewsPolicy.Master, StringComparison.OrdinalIgnoreCase) &&
!request.ViewName.Equals(catalogViewsPolicy.Details, StringComparison.OrdinalIgnoreCase) &&
!request.ViewName.Equals("Facets", StringComparison.OrdinalIgnoreCase) &&
!isConnectView)
{
return Task.FromResult(arg);
}
// Only proceed if the current entity is a sellable item
if (!(request.Entity is SellableItem))
{
return Task.FromResult(arg);
}
var sellableItem = (SellableItem)request.Entity;
var targetView = arg;
// Check if the edit action was requested
var isEditView = !string.IsNullOrEmpty(arg.Action) && arg.Action.Equals("EditFacets", StringComparison.OrdinalIgnoreCase);
if (!isEditView)
{
// Create a new view and add it to the current entity view.
var view = new EntityView
{
Name = "Facets",
DisplayName = "Facets",
EntityId = arg.EntityId,
ItemId = arg.ItemId
};
arg.ChildViews.Add(view);
targetView = view;
}
if (sellableItem != null && (sellableItem.HasComponent<FacetsComponent>() || isConnectView || isEditView))
{
FacetsComponent component = sellableItem.GetComponent<FacetsComponent>();
AddPropertiesToView(targetView, component, !isEditView);
}
return Task.FromResult(arg);
}
private void AddPropertiesToView(EntityView entityView, FacetsComponent component, bool isReadOnly)
{
entityView.Properties.Add(
new ViewProperty
{
Name = nameof(FacetsComponent.Colors),
RawValue = component.Colors,
IsReadOnly = isReadOnly,
IsRequired = false
});
entityView.Properties.Add(
new ViewProperty
{
Name = nameof(FacetsComponent.Styles),
RawValue = component.Styles,
IsReadOnly = isReadOnly,
IsRequired = false
});
entityView.Properties.Add(
new ViewProperty
{
Name = nameof(FacetsComponent.Sizes),
RawValue = component.Sizes,
IsReadOnly = isReadOnly,
IsRequired = false
});
entityView.Properties.Add(
new ViewProperty
{
Name = nameof(FacetsComponent.SellPrices),
RawValue = component.SellPrices,
IsReadOnly = isReadOnly,
IsRequired = false
});
entityView.Properties.Add(
new ViewProperty
{
Name = nameof(FacetsComponent.ListPrices),
RawValue = component.ListPrices,
IsReadOnly = isReadOnly,
IsRequired = false
});
entityView.Properties.Add(
new ViewProperty
{
Name = nameof(FacetsComponent.PercentagesOff),
RawValue = component.PercentagesOff,
IsReadOnly = isReadOnly,
IsRequired = false
});
entityView.Properties.Add(
new ViewProperty
{
Name = nameof(FacetsComponent.PriceRange),
RawValue = component.PriceRange,
IsReadOnly = isReadOnly,
IsRequired = false
});
}
}
}
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.