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
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