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); }
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); }
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 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>()); } }
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); }