Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
namespace Sitecore.Services.Examples.Indexing
{
using System.Threading.Tasks;
using Sitecore.Commerce.Core;
using Sitecore.Framework.Pipelines;
using Sitecore.Commerce.Plugin.Catalog;
using System.Collections.Generic;
using System;
[PipelineDisplayName("FacetsBlock")]
public class FacetsBlock : PipelineBlock<GenerateFacetsArgument, GenerateFacetsArgument, CommercePipelineExecutionContext>
{
CommerceCommander _commander;
public FacetsBlock(CommerceCommander commander)
{
_commander = commander;
}
public override async Task<GenerateFacetsArgument> Run(GenerateFacetsArgument arg, CommercePipelineExecutionContext context)
{
var getItemsCommand = _commander.Command<GetCatalogItemsCommand>();
//Here we just loop through all the products and set the values, price are retrieved for a specific catalog
//Ideally we should be doing this in a more effiecient way e.g. using the paging functionality etc.
//You may want to do this in a minion or part of your catalog import
//You should also look at spliting up the variant property fields from pricing as pricing changes more often.
var items = await getItemsCommand.Process(context.CommerceContext,context.CommerceContext.Environment.Name, 0, int.MaxValue);
foreach (var catalogItem in items.Items)
{
SellableItem item;
if (catalogItem is SellableItem == false)
{
continue;
}
else
{
item = catalogItem as SellableItem;
}
var variationComponent = item.GetComponent<ItemVariationsComponent>();
var facetsComponent = item.GetComponent<FacetsComponent>();
facetsComponent.Colors = string.Empty;
facetsComponent.Sizes = string.Empty;
facetsComponent.Styles = string.Empty;
facetsComponent.SellPrices = string.Empty;
facetsComponent.ListPrices = string.Empty;
facetsComponent.PercentagesOff = string.Empty;
facetsComponent.PriceRange = string.Empty;
List<string> ids = new List<string>();
foreach (var variant in variationComponent.Variations)
{
var displayProperties = variant.GetComponent<DisplayPropertiesComponent>();
facetsComponent.Colors += " " + displayProperties.Color;
facetsComponent.Sizes += " " + displayProperties.Size;
facetsComponent.Styles += " " + displayProperties.Style;
}
//We only get prices for the specific catalog
ids.Add(arg.CatalogId + "|" + item.ProductId + "|");
var getBulkPricesCommand = _commander.Command<GetBulkPricesCommand>();
var priceResults = getBulkPricesCommand.Process(context.CommerceContext, ids).Result;
decimal highestPrice = 0.0M;
decimal lowestPrice = 0.0M;
foreach (var price in priceResults)
{
foreach (var variations in price.Variations)
{
if (Convert.ToDecimal(variations?.SellPrice?.Amount) > highestPrice)
{
highestPrice = Convert.ToDecimal(variations?.SellPrice?.Amount);
}
if (Convert.ToDecimal(variations?.SellPrice?.Amount) < lowestPrice || lowestPrice == 0)
{
lowestPrice = Convert.ToDecimal(variations?.SellPrice?.Amount);
}
facetsComponent.SellPrices += " " + variations?.SellPrice?.Amount;
facetsComponent.ListPrices += " " + variations?.ListPrice?.Amount;
//Percentages off
var difference = variations?.SellPrice?.Amount - variations?.ListPrice?.Amount;
var percentage = Decimal.Round(Convert.ToDecimal((difference / variations?.SellPrice?.Amount) * 100), 0);
facetsComponent.PercentagesOff += " " + (percentage >= 0 ? string.Empty : (percentage * -1).ToString() + "%");
}
}
//Price range
if ((highestPrice >= 0 && highestPrice <= 1000) && (lowestPrice >= 0 && lowestPrice <= 1000))
{
facetsComponent.PriceRange = "0 to 1000";
}
else if ((highestPrice >= 1001 && highestPrice <= 2000) && (lowestPrice >= 1001 && lowestPrice <= 2000))
{
facetsComponent.PriceRange = "1001 to 2000";
}
else if ((highestPrice >= 2001 && highestPrice <= 3000) && (lowestPrice >= 2001 && lowestPrice <= 3000))
{
facetsComponent.PriceRange = "2001 to 3000";
}
else if (highestPrice >= 3001 && lowestPrice >= 3001)
{
facetsComponent.PriceRange = "more than 3000";
}
facetsComponent.Colors = facetsComponent.Colors.TrimStart();
facetsComponent.Sizes = facetsComponent.Sizes.TrimStart();
facetsComponent.Styles = facetsComponent.Styles.TrimStart();
facetsComponent.SellPrices = facetsComponent.SellPrices.TrimStart();
facetsComponent.ListPrices = facetsComponent.ListPrices.TrimStart();
facetsComponent.PercentagesOff = facetsComponent.PercentagesOff.TrimStart();
await _commander.Pipeline<PersistEntityPipeline>().Run(new PersistEntityArgument(item), context);
}
return await Task.FromResult(arg);
}
}
}
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.