Guest User

Untitled

a guest
Dec 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Sitecore.Commerce.Core;
  6. using Sitecore.Commerce.Plugin.Catalog;
  7. using Sitecore.Commerce.Plugin.Pricing;
  8. using Sitecore.Framework.Conditions;
  9. using Sitecore.Framework.Pipelines;
  10. using Sitecore.Services.Examples.Catalog.Pipelines.Arguments;
  11.  
  12. namespace Sitecore.Services.Examples.Catalog.Pipelines.Blocks
  13. {
  14. [PipelineDisplayName("CreateUpdateSellableItem.CreateUpdateSellableItemBlock")]
  15. public class CreateUpdateSellableItemBlock : PipelineBlock<List<CreateUpdateSellableItemArgument>, bool, CommercePipelineExecutionContext>
  16. {
  17. private readonly CommerceCommander _commander;
  18. public CreateUpdateSellableItemBlock(CommerceCommander commander)
  19. {
  20. _commander = commander;
  21. }
  22.  
  23. public override async Task<bool> Run(List<CreateUpdateSellableItemArgument> arg, CommercePipelineExecutionContext context)
  24. {
  25. Condition.Requires(arg).IsNotNull($"{Name}: The argument can not be null");
  26.  
  27. var result = new List<string>();
  28.  
  29. foreach (var sellableItemArg in arg)
  30. {
  31. var id = $"{CommerceEntity.IdPrefix<SellableItem>()}{sellableItemArg.ProductId}";
  32. //Will be created if not found
  33. var foundEntity = await _commander.Pipeline<FindEntityPipeline>().Run(new FindEntityArgument(typeof(SellableItem), id, false), context) ??
  34. await _commander.Command<CreateSellableItemCommand>().Process(context.CommerceContext,
  35. sellableItemArg.ProductId,sellableItemArg.Name,sellableItemArg.DisplayName,sellableItemArg.Description);
  36.  
  37. if (!(foundEntity is SellableItem sellableItem)) continue;
  38.  
  39. UpdateCreateListPrice(sellableItem.GetPolicy<ListPricingPolicy>(), sellableItemArg.ListPrice);
  40.  
  41. sellableItem.Description = sellableItemArg.Description;
  42. sellableItem.DisplayName = sellableItemArg.DisplayName;
  43. sellableItem.Name = sellableItemArg.Name;
  44.  
  45. foreach (var variantArgs in sellableItemArg.Variants)
  46. {
  47. var variant = sellableItem.GetVariation(variantArgs.VariantId);
  48. if (variant == null)
  49. {
  50. variant = new ItemVariationComponent
  51. {
  52. Id = variantArgs.VariantId
  53. };
  54. sellableItem.GetComponent<ItemVariationsComponent>().ChildComponents.Add(variant);
  55. }
  56.  
  57. UpdateCreateListPrice(variant.GetPolicy<ListPricingPolicy>(), variantArgs.ListPrice);
  58. }
  59.  
  60. var persistResult = await _commander.Pipeline<PersistEntityPipeline>()
  61. .Run(new PersistEntityArgument(sellableItem), context);
  62.  
  63. result.Add(persistResult.Entity.Id);
  64. }
  65.  
  66. var ok = result.Count != 0;
  67. return await Task.FromResult(ok);
  68. }
  69. private static void UpdateCreateListPrice(ListPricingPolicy pricePolicy, decimal price)
  70. {
  71. var money = pricePolicy.Prices.FirstOrDefault(x => x.CurrencyCode.Equals("USD", StringComparison.OrdinalIgnoreCase));
  72.  
  73. if (money == null)
  74. {
  75. pricePolicy.AddPrice(new Money(price));
  76. }
  77. else
  78. {
  79. money.Amount = price;
  80. }
  81. }
  82. }
  83. }
Add Comment
Please, Sign In to add comment