Beispiel #1
0
        public override async Task <SynchronizeCategoryArgument> Run(SynchronizeCategoryArgument arg, CommercePipelineExecutionContext context)
        {
            Sitecore.Commerce.Plugin.Catalog.Category category = null;
            var categoryId = $"{CommerceEntity.IdPrefix<Category>()}{arg.Catalog.Name}-{arg.ImportCategory.CategoryId.ProposeValidId()}";

            if (await _doesEntityExistPipeline.Run(
                    new FindEntityArgument(typeof(Sitecore.Commerce.Plugin.Catalog.Category), categoryId),
                    context.CommerceContext.GetPipelineContextOptions()))
            {
                category = await _getCategoryPipeline.Run(new GetCategoryArgument(categoryId),
                                                          context.CommerceContext.GetPipelineContextOptions());
            }
            else
            {
                var createResult = await _createCategoryPipeline.Run(
                    new CreateCategoryArgument(arg.Catalog.Id, arg.ImportCategory.CategoryId.ProposeValidId(),
                                               arg.ImportCategory.CategoryName, ""), context.CommerceContext.GetPipelineContextOptions());

                category = createResult?.Categories?.FirstOrDefault(c => c.Id.Equals(categoryId));
            }

            Condition.Requires <Sitecore.Commerce.Plugin.Catalog.Category>(category)
            .IsNotNull($"{this.Name}: The Category could not be created.");

            arg.Category = category;

            return(arg);
        }
        public override Task <EntityView> Run(EntityView arg, CommercePipelineExecutionContext context)
        {
            Condition.Requires(arg).IsNotNull($"{Name}: The argument cannot be null.");

            var request = this._viewCommander.CurrentEntityViewArgument(context.CommerceContext);

            var catalogViewsPolicy = context.GetPolicy <KnownCatalogViewsPolicy>();

            var sellableitemstiboattributesviewspolicy = context.GetPolicy <SellableItemStiboAttributesViewsPolicy>();

            var isVariationView = request.ViewName.Equals(catalogViewsPolicy.Variant, StringComparison.OrdinalIgnoreCase);
            var isConnectView   = arg.Name.Equals(catalogViewsPolicy.ConnectSellableItem, StringComparison.OrdinalIgnoreCase);

            // Only proceed if the current entity is a sellable item or Category
            if ((request.Entity is Sitecore.Commerce.Plugin.Catalog.Category) || (request.Entity is Sitecore.Commerce.Plugin.Catalog.SellableItem))
            {
                // Make sure that we target the correct views
                if (string.IsNullOrEmpty(request.ViewName) ||
                    !request.ViewName.Equals(catalogViewsPolicy.Master, StringComparison.OrdinalIgnoreCase) &&
                    !request.ViewName.Equals(catalogViewsPolicy.Details, StringComparison.OrdinalIgnoreCase) &&
                    !request.ViewName.Equals(sellableitemstiboattributesviewspolicy.CategoryAttributes, StringComparison.OrdinalIgnoreCase) &&
                    !isVariationView &&
                    !isConnectView)
                {
                    return(Task.FromResult(arg));
                }
                Sitecore.Commerce.Plugin.Catalog.SellableItem sellableItem = null; Sitecore.Commerce.Plugin.Catalog.Category category = null; var variationId = string.Empty;
                if (request.Entity is Sitecore.Commerce.Plugin.Catalog.SellableItem)
                {
                    sellableItem = (Sitecore.Commerce.Plugin.Catalog.SellableItem)request.Entity;
                }
                if (request.Entity is Sitecore.Commerce.Plugin.Catalog.Category)
                {
                    category = (Sitecore.Commerce.Plugin.Catalog.Category)request.Entity;
                }


                CategoryAttributes component = null;
                if (category != null)
                {
                    component = category.GetComponent <CategoryAttributes>();
                }

                // See if we are dealing with the base sellable item or one of its variations.
                if (sellableItem != null)
                {
                    if (isVariationView && !string.IsNullOrEmpty(arg.ItemId))
                    {
                        variationId = arg.ItemId;
                        component   = sellableItem.GetVariation(variationId).GetComponent <CategoryAttributes>();
                    }
                    else if (!string.IsNullOrEmpty(arg.ItemId) && arg.VersionedItemId != ("-1"))
                    {
                        variationId = arg.ItemId;
                        component   = sellableItem.GetVariation(variationId).GetComponent <CategoryAttributes>();
                    }
                    else
                    {
                        component = sellableItem.GetComponent <CategoryAttributes>(variationId);
                    }
                }

                var  targetView = arg;
                bool isEditView = false;

                #region 16. Category Mapping
                isEditView = !string.IsNullOrEmpty(arg.Action) && arg.Action.Equals(sellableitemstiboattributesviewspolicy.CategoryAttributes, StringComparison.OrdinalIgnoreCase);
                if (!isEditView)
                {
                    // Create a new view and add it to the current entity view.
                    var view = new EntityView
                    {
                        Name          = context.GetPolicy <SellableItemStiboAttributesViewsPolicy>().CategoryAttributes,
                        DisplayName   = "Category Mapping",
                        EntityId      = arg.EntityId,
                        DisplayRank   = 2,
                        EntityVersion = arg.EntityVersion,
                        ItemId        = variationId,
                        Icon          = "piece",
                    };

                    arg.ChildViews.Add(view);
                    targetView = view;
                }

                AddSellableItemProperties.AddPropertiesToViewWithSection(targetView, component, Models.ProductContsants.NonEditableField, nameof(component.SitecoreMappingPath), component.SitecoreMappingPath, component.GetDisplayName(nameof(component.SitecoreMappingPath)));

                AddSellableItemProperties.AddPropertiesToViewWithSection(targetView, component, Models.ProductContsants.NonEditableField, nameof(component.ClassificationType), component.ClassificationType, component.GetDisplayName(nameof(component.ClassificationType)));
                #endregion
            }
            return(Task.FromResult(arg));
        }