private async Task PerformEntityVariantLocalization(ImportLocalizeContentArgument arg, CommercePipelineExecutionContext context, ILanguageEntity language, IList <LocalizableComponentPropertiesValues> componentsPropertiesList, ItemVariationComponent itemVariationComponent, IEntity variant) { if (itemVariationComponent.ChildComponents != null && itemVariationComponent.ChildComponents.Any()) { foreach (var component in itemVariationComponent.ChildComponents) { var mapper = await _commerceCommander.Pipeline <IResolveComponentLocalizationMapperPipeline>() .Run(new ResolveComponentLocalizationMapperArgument(arg.ImportEntityArgument, arg.CommerceEntity, component, language, variant), context).ConfigureAwait(false); if (mapper != null) { mapper.Execute(componentsPropertiesList, component, language); } else { await context.CommerceContext.AddMessage(context.GetPolicy <KnownResultCodes>().Warning, "ComponentChildComponentLocalizationMapperMissing", null, $"Component's child component localization mapper missing for componentType={component.GetType().FullName} (key={component.GetComponentMetadataPolicy().MapperKey}) not resolved."); } } } }
public override async Task <SynchronizeProductArgument> Run(SynchronizeProductArgument arg, CommercePipelineExecutionContext context) { var sellableItem = arg.SellableItem; var variantsComponent = sellableItem.GetComponent <ItemVariationsComponent>(); variantsComponent.ChildComponents.Clear(); foreach (var variant in arg.ImportProduct.Variants) { var colorProperty = variant.ProductProperties.FirstOrDefault(pp => pp.PropertyId.Equals("11")); var defaultColorValue = colorProperty?.Values?.FirstOrDefault(v => v.Language.Equals("en"))?.Value; var sizeProperty = variant.ProductProperties.FirstOrDefault(pp => pp.PropertyId.Equals("12")); var defaultSizeValue = sizeProperty?.Values?.FirstOrDefault(v => v.Language.Equals("en"))?.Value; var variantId = $"{arg.SellableItem.Name}{defaultColorValue}{defaultSizeValue}"; var variationItem = new ItemVariationComponent() { Id = variantId, Name = $"{arg.SellableItem.DisplayName} {defaultColorValue} {defaultSizeValue}" }; //Add identifiers var identifiersComponent = variationItem.GetComponent <IdentifiersComponent>(); identifiersComponent.SKU = variantId; variationItem.SetComponent(identifiersComponent); variantsComponent.ChildComponents.Add(variationItem); } arg.SellableItem = (await _persistEntityPipeline.Run(new PersistEntityArgument(sellableItem), context.CommerceContext.GetPipelineContextOptions()))?.Entity as SellableItem ?? sellableItem; return(arg); }
public override async Task <SynchronizeProductArgument> Run(SynchronizeProductArgument arg, CommercePipelineExecutionContext context) { var sellableItem = arg.SellableItem; var variantsComponent = sellableItem.GetComponent <ItemVariationsComponent>(); variantsComponent.ChildComponents.Clear(); foreach (var variant in arg.MasterProduct.ProductVariants) { var displayName = variant?.Values?.FirstOrDefault(f => f.Language == "en")?.Name ?? variant?.Values?.FirstOrDefault()?.Name ?? string.Empty; var variationItem = new ItemVariationComponent { Id = variant.ArticleNr.ProposeValidId(), Name = variant.ArticleNr, DisplayName = displayName }; //Add identifiers var identifiersComponent = variationItem.GetComponent <IdentifiersComponent>(); identifiersComponent.SKU = variant.ArticleNr; identifiersComponent.gtin13 = variant.GTIN; variationItem.SetComponent(identifiersComponent); variantsComponent.ChildComponents.Add(variationItem); } arg.SellableItem = (await _persistEntityPipeline.Run(new PersistEntityArgument(sellableItem), context.CommerceContext.PipelineContextOptions))?.Entity as SellableItem ?? sellableItem; return(arg); }
protected async virtual Task <string> GetVariantInventoryStatus(ItemVariationComponent variant, string inventorySetName, CommercePipelineExecutionContext context) { if (variant.HasPolicy <AvailabilityAlwaysPolicy>()) { return(Engine.CatalogConstants.InventoryStatus.Perpetual); } if (!variant.HasComponent <InventoryComponent>()) { return(Engine.CatalogConstants.InventoryStatus.NotAvailable); } var inventoryComponent = variant.GetComponent <InventoryComponent>(); var association = inventoryComponent.InventoryAssociations.FirstOrDefault( i => i.InventorySet.EntityTarget.RemoveIdPrefix <InventorySet>().Equals(inventorySetName, StringComparison.InvariantCultureIgnoreCase)); if (association != null) { var inventoryInformation = await Commander.GetEntity <InventoryInformation>(context.CommerceContext, association.InventoryInformation.EntityTarget).ConfigureAwait(false); return(this.ToInventoryStatus(inventoryInformation)); } return(Engine.CatalogConstants.InventoryStatus.NotAvailable); }
protected override void Map(ItemVariationComponent component) { component.Id = SourceVariant.Id; component.DisplayName = SourceVariant.DisplayName; component.Description = SourceVariant.Description; component.Disabled = SourceVariant.Disabled; component.Tags = SourceVariant.Tags; }
/// <summary> /// Gets the variation property /// </summary> /// <param name="variationComponent">The item variation component.</param> /// <param name="variationProperty">The name of the variation property.</param> /// <returns></returns> protected virtual object GetVariationProperty(ItemVariationComponent variationComponent, string variationProperty) { foreach (var component in variationComponent.ChildComponents) { var property = component.GetType().GetProperty(variationProperty); if (property != null) { return(property.GetValue(component)); } } return(null); }
private string GetVariantProperty(ItemVariationComponent variation, VariationPropertyPolicy variationPropertyPolicy, string v) { foreach (var variationProperty in variationPropertyPolicy.PropertyNames) { var property = GetVariationProperty(variation, variationProperty); if (variationProperty == "PIN") { return((string)property); } } return(string.Empty); }
/// <summary> /// Populates the variation properties in the entity view /// </summary> /// <param name="variationView">The variation view.</param> /// <param name="variation">The item variation component.</param> /// <param name="variationPropertyPolicy">The variation property policy.</param> protected virtual void PopulateVariationProperties(EntityView variationView, ItemVariationComponent variation, VariationPropertyPolicy variationPropertyPolicy) { foreach (var variationProperty in variationPropertyPolicy.PropertyNames) { var property = GetVariationProperty(variation, variationProperty); var insertIndex = variationView.Properties.Count > 0 ? variationView.Properties.Count - 1 : 0; variationView.Properties.Insert(insertIndex, new ViewProperty { Name = variationProperty, RawValue = property ?? string.Empty, IsReadOnly = true }); } }
public static ProductExtensionComponent GetProductExtensionComponent(SellableItem instance, string variationId) { ItemVariationComponent variation = instance.GetVariation(variationId); if (variation.HasComponent <ProductExtensionComponent>() == false) { var component = instance.GetComponent <ProductExtensionComponent>().Clone(); variation.SetComponent(component); return(component); } else { return(variation.GetComponent <ProductExtensionComponent>()); } }
private void AddTagsToVariant(Product sourceProduct, ItemVariationComponent destinationComponent) { if (sourceProduct.Tags == null) { return; } foreach (var sourceTag in sourceProduct.Tags?.Split('|')) { var destinationTag = destinationComponent.Tags.FirstOrDefault(x => x.Name == sourceTag); if (destinationTag == null) { destinationComponent.Tags.Add(new Tag(sourceTag)); } } Log($"{sourceProduct.Id} tag has been set"); }
private async Task UpsertSellableItem(SellableItem item, CommercePipelineExecutionContext context) { if (string.IsNullOrEmpty(item.ProductId)) { item.ProductId = item.Id.SimplifyEntityName().ProposeValidId(); } if (string.IsNullOrEmpty(item.FriendlyId)) { item.FriendlyId = item.Id.SimplifyEntityName(); } if (string.IsNullOrEmpty(item.SitecoreId)) { item.SitecoreId = GuidUtility.GetDeterministicGuidString(item.Id); } CommerceEntity entity = await _findEntityPipeline .Run(new FindEntityArgument(typeof(SellableItem), item.Id), context) .ConfigureAwait(false); if (entity == null) { await _persistEntityPipeline.Run(new PersistEntityArgument(item), context).ConfigureAwait(false); return; } if (!(entity is SellableItem)) { return; } var existingSellableItem = entity as SellableItem; // Try to merge the items. existingSellableItem.Name = item.Name; foreach (Policy policy in item.EntityPolicies) { if (existingSellableItem.HasPolicy(policy.GetType())) { existingSellableItem.RemovePolicy(policy.GetType()); } existingSellableItem.SetPolicy(policy); } if (item.HasComponent <ItemVariationsComponent>()) { var variations = existingSellableItem.GetComponent <ItemVariationsComponent>(); foreach (ItemVariationComponent variation in item .GetComponent <ItemVariationsComponent>().ChildComponents .OfType <ItemVariationComponent>()) { ItemVariationComponent existingVariation = existingSellableItem.GetVariation(variation.Id); if (existingVariation != null) { existingVariation.Name = variation.Name; foreach (Policy policy in variation.Policies) { if (existingVariation.Policies.Any(x => x.GetType() == policy.GetType())) { existingVariation.RemovePolicy(policy.GetType()); } existingVariation.SetPolicy(policy); } } else { variations.ChildComponents.Add(variation); } } } await _persistEntityPipeline.Run(new PersistEntityArgument(existingSellableItem), context).ConfigureAwait(false); }
protected override void MapLocalizeValues(ItemVariationComponent component) { component.DisplayName = SourceVariant.DisplayName; component.Description = SourceVariant.Description; }
protected override string GetLocalizableComponentPath(ItemVariationComponent component) { return(string.Format(CultureInfo.InvariantCulture, "{0}.{1}", typeof(ItemVariationsComponent).Name, typeof(ItemVariationComponent).Name)); }
public static void AddVariantListPrice(this ImportCatalogEntityResponse sellableItem, decimal sellableItemListPrice, ItemVariationComponent variant) { if (sellableItemListPrice == 0) { return; } var listPricingPolicy = variant.GetPolicy <ListPricingPolicy>(); listPricingPolicy.AddPrice(new Money("USD", sellableItemListPrice)); }
private void AddSellableItemPricing(EntityView entityView, SellableItem entity, ItemVariationComponent variation, CommercePipelineExecutionContext context) { var policy = context.GetPolicy <KnownCatalogViewsPolicy>(); var entityView1 = new EntityView { Name = policy.SellableItemPricing, EntityId = entityView.EntityId, EntityVersion = entityView.EntityVersion, ItemId = variation != null ? variation.Id : string.Empty, UiHint = "Flat" }; var entityView2 = entityView1; var entityView3 = new EntityView { Name = policy.SellableItemListPricing, EntityId = entityView.EntityId, EntityVersion = entityView.EntityVersion, ItemId = variation != null ? variation.Id : string.Empty, UiHint = "Table" }; var entityView4 = entityView3; if (entity != null) { var str = variation != null?variation.GetPolicy <PriceCardPolicy>().PriceCardName : entity.GetPolicy <PriceCardPolicy>().PriceCardName; var properties1 = entityView2.Properties; var viewProperty1 = new ViewProperty { Name = "PriceCardName", RawValue = str ?? string.Empty, IsReadOnly = true, IsRequired = false, IsHidden = false }; properties1.Add(viewProperty1); foreach (var price in (variation != null ? variation.GetPolicy <ListPricingPolicy>() : entity.GetPolicy <ListPricingPolicy>()).Prices) { var entityView5 = new EntityView { Name = context.GetPolicy <KnownCatalogViewsPolicy>().Summary, EntityId = entityView.EntityId, ItemId = (variation != null ? variation.Id : string.Empty) + "|" + price.CurrencyCode, UiHint = "Flat" }; var entityView6 = entityView5; var properties2 = entityView6.Properties; var viewProperty2 = new ViewProperty { Name = "Currency", RawValue = price.CurrencyCode }; properties2.Add(viewProperty2); var properties3 = entityView6.Properties; var viewProperty3 = new ViewProperty { Name = "ListPrice", RawValue = price.Amount }; properties3.Add(viewProperty3); entityView4.ChildViews.Add(entityView6); } } entityView2.ChildViews.Add(entityView4); entityView.ChildViews.Add(entityView2); }
public override async Task <bool> Run(SellableItemInventorySetsArgument argument, CommercePipelineExecutionContext context) { AssociateStoreInventoryToSellablteItemBlock associateStoreInventoryToSellablteItemBlock = this; Condition.Requires(argument).IsNotNull(string.Format("{0}: The argument can not be null", argument)); string sellableItemId = argument.SellableItemId; CommerceEntity entity = await associateStoreInventoryToSellablteItemBlock._findEntityPipeline.Run(new FindEntityArgument(typeof(SellableItem), sellableItemId, false), context).ConfigureAwait(false); CommercePipelineExecutionContext executionContext; if (!(entity is SellableItem)) { executionContext = context; CommerceContext commerceContext = context.CommerceContext; string validationError = context.GetPolicy <KnownResultCodes>().ValidationError; string commerceTermKey = "EntityNotFound"; object[] args = new object[1] { argument.SellableItemId }; string defaultMessage = string.Format("Entity {0} was not found.", argument.SellableItemId); executionContext.Abort(await commerceContext.AddMessage(validationError, commerceTermKey, args, defaultMessage).ConfigureAwait(false), context); executionContext = null; return(false); } SellableItem sellableItem = entity as SellableItem; if ((string.IsNullOrEmpty(argument.VariationId)) & sellableItem.HasComponent <ItemVariationsComponent>()) { executionContext = context; executionContext.Abort(await context.CommerceContext.AddMessage(context.GetPolicy <KnownResultCodes>().ValidationError, "AssociateInventoryWithVariant", new object[0], "Can not associate inventory to the base sellable item. Use one of the variants instead.").ConfigureAwait(false), context); executionContext = null; return(false); } ItemVariationComponent sellableItemVariation = null; if (argument.VariationId != null) { sellableItemVariation = sellableItem.GetVariation(argument.VariationId); if (!string.IsNullOrEmpty(argument.VariationId) && sellableItemVariation == null) { executionContext = context; CommerceContext commerceContext = context.CommerceContext; string validationError = context.GetPolicy <KnownResultCodes>().ValidationError; string commerceTermKey = "ItemNotFound"; object[] args = new object[1] { argument.VariationId }; string defaultMessage = string.Format("Item '{0}' was not found.", argument.VariationId); executionContext.Abort(await commerceContext.AddMessage(validationError, commerceTermKey, args, defaultMessage).ConfigureAwait(false), context); executionContext = null; return(false); } } List <InventoryAssociation> inventoryAssociations = new List <InventoryAssociation>(); foreach (var inventorySetId in argument.InventorySetIds) { bool isUpdate = false; Random rnd = new Random(); InventoryInformation inventoryInformation = await associateStoreInventoryToSellablteItemBlock._getInventoryInformationCommand .Process(context.CommerceContext, inventorySetId, argument.SellableItemId, argument.VariationId, false) .ConfigureAwait(false); IFindEntitiesInListPipeline entitiesInListPipeline = associateStoreInventoryToSellablteItemBlock._findEntitiesInListPipeline; FindEntitiesInListArgument entitiesInListArgument1 = new FindEntitiesInListArgument(typeof(SellableItem), string.Format("{0}-{1}", "InventorySetToInventoryInformation", inventorySetId.SimplifyEntityName()), 0, int.MaxValue); entitiesInListArgument1.LoadEntities = false; CommercePipelineExecutionContext context1 = context; FindEntitiesInListArgument entitiesInListArgument2 = await entitiesInListPipeline.Run(entitiesInListArgument1, context1).ConfigureAwait(false); if (inventoryInformation != null && entitiesInListArgument2 != null) { List <ListEntityReference> entityReferences = entitiesInListArgument2.EntityReferences.ToList(); string id = inventoryInformation.Id; if (entityReferences != null && entityReferences.Any(er => er.EntityId == id)) { inventoryInformation.Quantity = rnd.Next(50); isUpdate = true; } } if (!isUpdate) { string inventorySetName = string.Format("{0}-{1}", inventorySetId.SimplifyEntityName(), sellableItem.ProductId); if (!string.IsNullOrEmpty(argument.VariationId)) { inventorySetName += string.Format("-{0}", argument.VariationId); } InventoryInformation inventoryInformation1 = new InventoryInformation(); inventoryInformation1.Id = string.Format("{0}{1}", CommerceEntity.IdPrefix <InventoryInformation>(), inventorySetName); inventoryInformation1.FriendlyId = inventorySetName; EntityReference entityReference1 = new EntityReference(inventorySetId, ""); inventoryInformation1.InventorySet = entityReference1; EntityReference entityReference2 = new EntityReference(argument.SellableItemId, ""); inventoryInformation1.SellableItem = entityReference2; string variationId = argument.VariationId; inventoryInformation1.VariationId = variationId; inventoryInformation1.Quantity = rnd.Next(50); inventoryInformation = inventoryInformation1; } inventoryInformation.GetComponent <TransientListMembershipsComponent>().Memberships.Add(CommerceEntity.ListName <InventoryInformation>()); PersistEntityArgument persistEntityArgument1 = await associateStoreInventoryToSellablteItemBlock._persistEntityPipeline.Run(new PersistEntityArgument((CommerceEntity)inventoryInformation), context).ConfigureAwait(false); RelationshipArgument relationshipArgument = await associateStoreInventoryToSellablteItemBlock._createRelationshipPipeline.Run(new RelationshipArgument(inventorySetId, inventoryInformation.Id, "InventorySetToInventoryInformation"), context).ConfigureAwait(false); InventoryAssociation inventoryAssociation = new InventoryAssociation() { InventoryInformation = new EntityReference(inventoryInformation.Id, ""), InventorySet = new EntityReference(inventorySetId, "") }; inventoryAssociations.Add(inventoryAssociation); } (sellableItemVariation != null ? sellableItemVariation.GetComponent <InventoryComponent>() : sellableItem.GetComponent <InventoryComponent>()).InventoryAssociations.AddRange(inventoryAssociations); PersistEntityArgument persistEntityArgument2 = await associateStoreInventoryToSellablteItemBlock._persistEntityPipeline.Run(new PersistEntityArgument(sellableItem), context).ConfigureAwait(false); return(true); }
public abstract void SetVariantCustomFields(Variant sourceVariant, ItemVariationComponent destinationVariant);
private void ProcessVariants(ICollection <CommerceEntity> foundSellableItem, ICollection <CommerceEntity> updates, ICollection <CommerceEntity> adds) { foreach (var sourceVariant in _synchronizeCatalogArgument.Variants) { Log($"Starting processing of variant {sourceVariant.Id}"); if (string.IsNullOrEmpty(sourceVariant.Id) || string.IsNullOrEmpty(sourceVariant.ParentId)) { Log($"{sourceVariant.Id} {sourceVariant.ParentId} has no parentid or id and will be skipped"); continue; } var destinationProduct = updates.OfType <SellableItem>().FirstOrDefault(x => x.ProductId == sourceVariant.SplitParentId); if (destinationProduct == null) { destinationProduct = adds.OfType <SellableItem>().FirstOrDefault(x => x.ProductId == sourceVariant.SplitParentId); if (destinationProduct == null) { destinationProduct = foundSellableItem.OfType <SellableItem>().FirstOrDefault(x => x.ProductId == sourceVariant.SplitParentId); //Make sure we add it to the list of products that will get updated if (destinationProduct != null && updates.FirstOrDefault(x => x.Id == destinationProduct.Id) == null) { updates.Add(destinationProduct); } } } //The variant in the list must be attached to a product that exists, and or is being updated or created in the same run if (destinationProduct == null) { _synchronizeCatalogResult.LogMessages.Add( $"The variant {sourceVariant.Id} must be attached to a product that exists, and or is being updated or created in the same run"); continue; } var destinationVariation = destinationProduct.GetVariation(sourceVariant.Id); var newComponent = false; if (destinationVariation == null) { destinationVariation = new ItemVariationComponent(); newComponent = true; Log($"The variant {sourceVariant.Id} is new and will be added"); } if (sourceVariant.Operation.ToLower() == "delete") { var variations = destinationProduct.GetComponent <ItemVariationsComponent>(); variations.ChildComponents.Remove(destinationVariation); Log($"The variant {sourceVariant.Id} will be added to the list for deletion"); continue; } if (sourceVariant.Id != null) { Log($"{sourceVariant.Id} id has been set"); destinationVariation.Id = sourceVariant.Id; } if (sourceVariant.Name != null) { Log($"{sourceVariant.Id} Name has been set"); destinationVariation.Name = sourceVariant.Name; } if (sourceVariant.DisplayName != null) { Log($"{sourceVariant.Id} DisplayName has been set"); destinationVariation.DisplayName = sourceVariant.DisplayName; } if (sourceVariant.Description != null) { Log($"{sourceVariant.Id} Description has been set"); destinationVariation.Description = sourceVariant.Description; } AddTagsToVariant(sourceVariant, destinationVariation); AddListPriceToVariant(sourceVariant, destinationVariation); if (sourceVariant.Color != null) { Log($"{sourceVariant.Id} color has been set"); destinationVariation.GetComponent <Sitecore.Commerce.Plugin.Catalog.DisplayPropertiesComponent>() .Color = sourceVariant.Color; } if (sourceVariant.Size != null) { Log($"{sourceVariant.Id} size has been set"); destinationVariation.GetComponent <Sitecore.Commerce.Plugin.Catalog.DisplayPropertiesComponent>() .Size = sourceVariant.Size; } if (sourceVariant.Style != null) { Log($"{sourceVariant.Id} Style has been set"); destinationVariation.GetComponent <Sitecore.Commerce.Plugin.Catalog.DisplayPropertiesComponent>() .Style = sourceVariant.Style; } if (newComponent) { destinationProduct.GetComponent <ItemVariationsComponent>() .ChildComponents .Add(destinationVariation); } SetVariantCustomFields(sourceVariant, destinationVariation); Log($"Ending processing of variant {sourceVariant.Id}"); } }
public override void SetVariantCustomFields(Variant sourceVariant, ItemVariationComponent destinationVariant) { //Add your custom mappings here e.g. map to new components etc. }