Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
namespace Sitecore.Services.Examples.Composer
{
using System;
using System.Threading.Tasks;
using Sitecore.Commerce.Core;
using Sitecore.Framework.Conditions;
using Sitecore.Framework.Pipelines;
using Sitecore.Commerce.Plugin.Composer;
using Sitecore.Commerce.Plugin.ManagedLists;
using Sitecore.Commerce.Plugin.Views;
using Sitecore.Commerce.EntityViews;
using System.Collections.Generic;
[PipelineDisplayName("CreateComposerTemplatesBlock")]
public class CreateComposerTemplatesBlock : PipelineBlock<SampleArgument, bool, CommercePipelineExecutionContext>
{
private readonly CommerceCommander _commerceCommander;
public CreateComposerTemplatesBlock(CommerceCommander commerceCommander)
{
_commerceCommander = commerceCommander;
}
public override async Task<bool> Run(SampleArgument arg, CommercePipelineExecutionContext context)
{
Condition.Requires(arg).IsNotNull($"{this.Name}: The argument can not be null");
//Create the template, add the view and the properties
string templateId = $"{CommerceEntity.IdPrefix<ComposerTemplate>()}{"DiscontinuedTemplate"}";
var composerTemplate = new ComposerTemplate(templateId);
composerTemplate.GetComponent<ListMembershipsComponent>().Memberships.Add(CommerceEntity.ListName<ComposerTemplate>());
composerTemplate.LinkedEntities = new List<string>() { "Sitecore.Commerce.Plugin.Catalog.SellableItem" };
var itemDefinitions = new Sitecore.Commerce.Plugin.Catalog.ItemDefinitionsComponent
{
Definitions = new List<string>() { "Refrigerator" }
};
composerTemplate.Components.Add(itemDefinitions);
composerTemplate.Name = "DiscontinuedTemplate";
composerTemplate.DisplayName = "Discontinued Template";
var composerTemplateViewComponent = composerTemplate.GetComponent<EntityViewComponent>();
var composerTemplateView = new EntityView
{
Name = "Discontinued",
DisplayName = "Discontinued",
DisplayRank = 0,
ItemId = $"Composer-95DACDDD-151E-4527-9068-A27C9275967F",
EntityId = composerTemplate.Id
};
composerTemplateView.Properties.Add(new ViewProperty() { DisplayName = "Discontinued", Name = "Discontinued", OriginalType = "System.Boolean", RawValue = false, Value = "false" });
composerTemplateViewComponent.View.ChildViews.Add(composerTemplateView);
var persistResult = await this._commerceCommander.PersistEntity(context.CommerceContext, composerTemplate);
return await Task.FromResult(true);
}
}
}
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.