Skip to content

Instantly share code, notes, and snippets.

@websterian
Created December 17, 2018 18:11
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 websterian/5dc52e9f366849dae79c7fd664621d13 to your computer and use it in GitHub Desktop.
Save websterian/5dc52e9f366849dae79c7fd664621d13 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Sitecore.Commerce.Core;
using Sitecore.Commerce.Plugin.Catalog;
using Sitecore.Commerce.Plugin.Pricing;
using Sitecore.Framework.Conditions;
using Sitecore.Framework.Pipelines;
using Sitecore.Services.Examples.Catalog.Pipelines.Arguments;
namespace Sitecore.Services.Examples.Catalog.Pipelines.Blocks
{
[PipelineDisplayName("CreateUpdateSellableItem.CreateUpdateSellableItemBlock")]
public class CreateUpdateSellableItemBlock : PipelineBlock<List<CreateUpdateSellableItemArgument>, bool, CommercePipelineExecutionContext>
{
private readonly CommerceCommander _commander;
public CreateUpdateSellableItemBlock(CommerceCommander commander)
{
_commander = commander;
}
public override async Task<bool> Run(List<CreateUpdateSellableItemArgument> arg, CommercePipelineExecutionContext context)
{
Condition.Requires(arg).IsNotNull($"{Name}: The argument can not be null");
var result = new List<string>();
foreach (var sellableItemArg in arg)
{
var id = $"{CommerceEntity.IdPrefix<SellableItem>()}{sellableItemArg.ProductId}";
//Will be created if not found
var foundEntity = await _commander.Pipeline<FindEntityPipeline>().Run(new FindEntityArgument(typeof(SellableItem), id, false), context) ??
await _commander.Command<CreateSellableItemCommand>().Process(context.CommerceContext,
sellableItemArg.ProductId,sellableItemArg.Name,sellableItemArg.DisplayName,sellableItemArg.Description);
if (!(foundEntity is SellableItem sellableItem)) continue;
UpdateCreateListPrice(sellableItem.GetPolicy<ListPricingPolicy>(), sellableItemArg.ListPrice);
sellableItem.Description = sellableItemArg.Description;
sellableItem.DisplayName = sellableItemArg.DisplayName;
sellableItem.Name = sellableItemArg.Name;
foreach (var variantArgs in sellableItemArg.Variants)
{
var variant = sellableItem.GetVariation(variantArgs.VariantId);
if (variant == null)
{
variant = new ItemVariationComponent
{
Id = variantArgs.VariantId
};
sellableItem.GetComponent<ItemVariationsComponent>().ChildComponents.Add(variant);
}
UpdateCreateListPrice(variant.GetPolicy<ListPricingPolicy>(), variantArgs.ListPrice);
}
var persistResult = await _commander.Pipeline<PersistEntityPipeline>()
.Run(new PersistEntityArgument(sellableItem), context);
result.Add(persistResult.Entity.Id);
}
var ok = result.Count != 0;
return await Task.FromResult(ok);
}
private static void UpdateCreateListPrice(ListPricingPolicy pricePolicy, decimal price)
{
var money = pricePolicy.Prices.FirstOrDefault(x => x.CurrencyCode.Equals("USD", StringComparison.OrdinalIgnoreCase));
if (money == null)
{
pricePolicy.AddPrice(new Money(price));
}
else
{
money.Amount = price;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment