Beispiel #1
0
        private static void AddSellableItemVariations(EntityView entityView, CommerceEntity entity, CommercePipelineExecutionContext context)
        {
            var policy = context.GetPolicy <KnownCatalogViewsPolicy>();

            if (entity == null || !entityView.Name.Equals(policy.Master, StringComparison.OrdinalIgnoreCase) && !entityView.Name.Equals(policy.Details, StringComparison.OrdinalIgnoreCase) && !entityView.Name.Equals(policy.ConnectSellableItem, StringComparison.OrdinalIgnoreCase))
            {
                return;
            }
            var entityView1 = new EntityView
            {
                Name          = policy.SellableItemVariants,
                EntityId      = entity.Id,
                EntityVersion = entity.EntityVersion,
                UiHint        = "Table"
            };
            var entityView2 = entityView1;
            var list        = entity.GetComponent <ItemVariationsComponent>().ChildComponents.OfType <ItemVariationComponent>().ToList();

            if (list.Count > 0)
            {
                foreach (var variationComponent in list)
                {
                    var entityView3 = new EntityView
                    {
                        Name          = policy.Variant,
                        EntityId      = entity.Id,
                        EntityVersion = entity.EntityVersion,
                        ItemId        = variationComponent.Id
                    };
                    var entityView4   = entityView3;
                    var properties1   = entityView4.Properties;
                    var viewProperty1 = new ViewProperty
                    {
                        Name       = "Id",
                        RawValue   = variationComponent.Id,
                        IsReadOnly = true,
                        UiType     = "ItemLink"
                    };
                    properties1.Add(viewProperty1);
                    var properties2   = entityView4.Properties;
                    var viewProperty2 = new ViewProperty
                    {
                        Name       = "DisplayName",
                        RawValue   = variationComponent.DisplayName,
                        IsReadOnly = true
                    };
                    properties2.Add(viewProperty2);
                    var properties3   = entityView4.Properties;
                    var viewProperty3 = new ViewProperty
                    {
                        Name       = "Disabled",
                        RawValue   = variationComponent.Disabled,
                        IsReadOnly = true
                    };
                    properties3.Add(viewProperty3);
                    entityView2.ChildViews.Add(entityView4);
                }
            }
            entityView.ChildViews.Add(entityView2);
        }
Beispiel #2
0
 public static ItemVariationComponent GetVariation(
     this CommerceEntity instance,
     string variationId)
 {
     if (instance == null || !instance.HasComponent <ItemVariationsComponent>() || string.IsNullOrEmpty(variationId))
     {
         return(null);
     }
     return(instance.GetComponent <ItemVariationsComponent>().ChildComponents.OfType <ItemVariationComponent>().FirstOrDefault(x => x.Id.Equals(variationId, StringComparison.OrdinalIgnoreCase)));
 }
        /// <summary>
        ///     Retrieves all component types applicable for the sellable item
        /// </summary>
        /// <param name="commerceEntity">Sellable item for which to get the applicable components</param>
        /// <param name="itemId"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public List <Type> GetApplicableComponentTypes(CommerceEntity commerceEntity, string itemId, CommerceContext context)
        {
            // Get the item definition
            var catalogs = commerceEntity.GetComponent <CatalogsComponent>();

            // TODO: What happens if a sellableitem is part of multiple catalogs?
            var catalog        = catalogs.GetComponent <CatalogComponent>();
            var itemDefinition = catalog.ItemDefinition;

            var allComponentTypes = GetAllComponentTypes();

            var applicableComponentTypes = new List <Type>();

            foreach (var componentType in allComponentTypes)
            {
                System.Attribute[] attrs = System.Attribute.GetCustomAttributes(componentType);

                if (attrs.Any(attr => attr is AddToSellableItemAttribute) && commerceEntity is SellableItem)
                {
                    var sellableItemsAttribute = attrs.Single(attr => attr is SellableItemAttributeBase) as SellableItemAttributeBase;
                    var addToSellableItem      = sellableItemsAttribute.AddToSellableItem;

                    if (IsApplicableComponent(itemId, addToSellableItem))
                    {
                        applicableComponentTypes.Add(componentType);
                    }
                }
                else if (attrs.Any(attr => attr is AddToItemDefinitionAttribute && ((AddToItemDefinitionAttribute)attr).ItemDefinition == itemDefinition))
                {
                    var sellableItemsAttribute = attrs.Single(attr => attr is AddToItemDefinitionAttribute) as AddToItemDefinitionAttribute;
                    var addToSellableItem      = sellableItemsAttribute.AddToSellableItem;

                    if (IsApplicableComponent(itemId, addToSellableItem))
                    {
                        applicableComponentTypes.Add(componentType);
                    }
                }
                else if (attrs.Any(attr => attr is AddToEntityTypeAttribute && ((AddToEntityTypeAttribute)attr).EntityType == commerceEntity.GetType()))
                {
                    applicableComponentTypes.Add(componentType);
                }
                else if (attrs.Any(attr => attr is AddToAllEntityTypesAttribute))
                {
                    applicableComponentTypes.Add(componentType);
                }
            }

            return(applicableComponentTypes);
        }
Beispiel #4
0
        private static EntityViewComponent GetPopulatedEntityViewComponent(ImportSingleCsvLineArgument arg, CommerceEntity composerTemplate, string viewName)
        {
            var entityViewComponent = composerTemplate.GetComponent <EntityViewComponent>();

            if (!(entityViewComponent.View.ChildViews.FirstOrDefault(x => x.Name == viewName) is EntityView entityView))
            {
                return(null);
            }

            entityView.EntityId = arg.Line.ProductId.ToEntityId <SellableItem>();
            PopulatePropertyValue(entityView, "Waist", arg.Line.Waist);
            PopulatePropertyValue(entityView, "OutsideLeg", arg.Line.Leg);
            PopulatePropertyValue(entityView, "InsideLeg", arg.Line.InsideLeg);
            return(entityViewComponent);
        }
        public override bool IsApplicableToEntity(CommerceEntity entity, string itemId)
        {
            if (!(entity is Sitecore.Commerce.Plugin.Catalog.SellableItem))
            {
                return(false);
            }

            // Get the item definition
            var catalogs = entity.GetComponent <CatalogsComponent>();

            // TODO: What happens if a sellableitem is part of multiple catalogs?
            var catalog        = catalogs.GetComponent <CatalogComponent>();
            var itemDefinition = catalog.ItemDefinition;

            return(ItemDefinition == itemDefinition && IsApplicableComponent(itemId));
        }
Beispiel #6
0
        /// <summary>
        /// Updates variants entity view
        /// </summary>
        /// <param name="variantsView">The variants view.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="context">The context.</param>
        protected virtual void UpdateVariantsView(EntityView variantsView, CommerceEntity entity, CommercePipelineExecutionContext context)
        {
            var variations = entity.GetComponent <ItemVariationsComponent>().ChildComponents.OfType <ItemVariationComponent>().ToList();

            if (variations != null && variations.Count <= 0)
            {
                return;
            }

            var variationPropertyPolicy = context.CommerceContext.Environment.GetPolicy <VariationPropertyPolicy>();

            foreach (var variation in variations)
            {
                var variationView = variantsView.ChildViews.FirstOrDefault(c => ((EntityView)c).ItemId == variation.Id) as EntityView;
                PopulateVariationProperties(variationView, variation, variationPropertyPolicy);
            }
        }
        private async Task ImportVariants(CommerceEntity commerceEntity, ImportEntityArgument importEntityArgument, CommercePipelineExecutionContext context)
        {
            var orphanVariants = new List <ItemVariationComponent>();
            ItemVariationsComponent itemVariationsComponent = null;
            var sourceEntityHasVariants = importEntityArgument.ImportHandler.HasVariants();

            if (!sourceEntityHasVariants &&
                commerceEntity.HasComponent <ItemVariationsComponent>())
            {
                itemVariationsComponent = commerceEntity.GetComponent <ItemVariationsComponent>();
                if (itemVariationsComponent.Variations != null &&
                    itemVariationsComponent.Variations.Any())
                {
                    orphanVariants = itemVariationsComponent.Variations;
                }
            }

            if (sourceEntityHasVariants)
            {
                itemVariationsComponent =
                    commerceEntity.GetComponent <ItemVariationsComponent>();

                var variants = importEntityArgument.ImportHandler.GetVariants();

                foreach (var variant in variants)
                {
                    var itemVariantMapper = await _commerceCommander.Pipeline <IResolveComponentMapperPipeline>()
                                            .Run(
                        new ResolveComponentMapperArgument(importEntityArgument, commerceEntity,
                                                           itemVariationsComponent, variant, string.Empty), context).ConfigureAwait(false);

                    if (itemVariantMapper == null)
                    {
                        await context.CommerceContext.AddMessage(context.GetPolicy <KnownResultCodes>().Warning, "ItemVariationMapperMissing", null, $"Item variation mapper instance for variantId={variant.Id} not resolved.");

                        continue;
                    }

                    var       action = itemVariantMapper.GetComponentAction();
                    Component itemVariationComponent = itemVariantMapper.Execute(action);

                    if (action != ComponentAction.Remove &&
                        importEntityArgument.SourceEntityDetail.VariantComponents != null &&
                        importEntityArgument.SourceEntityDetail.VariantComponents.Any())
                    {
                        foreach (var variantComponentName in importEntityArgument.SourceEntityDetail.VariantComponents)
                        {
                            var itemVariantChildComponentMapper = await _commerceCommander
                                                                  .Pipeline <IResolveComponentMapperPipeline>().Run(new
                                                                                                                    ResolveComponentMapperArgument(importEntityArgument, commerceEntity,
                                                                                                                                                   itemVariationComponent, variant, variantComponentName), context)
                                                                  .ConfigureAwait(false);

                            if (itemVariantChildComponentMapper != null)
                            {
                                var childComponent = itemVariantChildComponentMapper.Execute();
                                childComponent.SetComponentMetadataPolicy(variantComponentName);
                            }
                            else
                            {
                                await context.CommerceContext.AddMessage(context.GetPolicy <KnownResultCodes>().Warning, "ComponentChildComponentMapperMissing", null, $"Component's child component mapper instance for entityType={importEntityArgument.SourceEntityDetail.EntityType} and key={variantComponentName} not resolved.");
                            }
                        }
                    }
                }

                orphanVariants = (from n in itemVariationsComponent.Variations
                                  join o in variants on n.Id equals o.Id into p
                                  where !p.Any()
                                  select n).ToList();
            }

            if (itemVariationsComponent != null &&
                orphanVariants != null &&
                orphanVariants.Any())
            {
                foreach (var orphanVariant in orphanVariants)
                {
                    if (importEntityArgument.CatalogImportPolicy.DeleteOrphanVariant)
                    {
                        itemVariationsComponent.ChildComponents.Remove(orphanVariant);
                    }
                    else
                    {
                        orphanVariant.Disabled = true;
                    }
                }
            }
        }