public override async Task <ImportEntityArgument> Run(ImportEntityArgument arg, CommercePipelineExecutionContext context)
        {
            var    commerceEntity = arg.ImportHandler.GetCommerceEntity();
            string entityId       = commerceEntity.Id;

            if (arg.ImportHandler.ParentEntityIds == null ||
                !arg.ImportHandler.ParentEntityIds.Any() ||
                !(commerceEntity is SellableItem))
            {
                return(await Task.FromResult(arg));
            }

            foreach (var catalog in arg.ImportHandler.ParentEntityIds)
            {
                if (catalog.Value == null || !catalog.Value.Any())
                {
                    await _associateSellableItemToParent
                    .Process(context.CommerceContext, catalog.Key, catalog.Key, entityId).ConfigureAwait(false);
                }
                else
                {
                    foreach (var parentId in catalog.Value)
                    {
                        await _associateSellableItemToParent
                        .Process(context.CommerceContext, catalog.Key, parentId, entityId).ConfigureAwait(false);
                    }
                }
            }

            return(await Task.FromResult(arg));
        }
        /// <summary>
        /// Asociate sellable items to parent category
        /// </summary>
        /// <param name="entityModel"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        private async Task AssociateSellableItemsToParent(EntityCollectionModel entityModel, CommercePipelineExecutionContext context)
        {
            CommerceList <Catalog>  catalogs   = _findEntitiesInListCommand.Process <Catalog>(context.CommerceContext, CommerceEntity.ListName <Catalog>(), 0, int.MaxValue).Result;
            CommerceList <Category> categories = _findEntitiesInListCommand.Process <Category>(context.CommerceContext, CommerceEntity.ListName <Category>(), 0, int.MaxValue).Result;

            foreach (var entity in entityModel.Entities)
            {
                var sellableItem = entity as SellableItem;
                if (!string.IsNullOrEmpty(sellableItem.ParentCatalogList) || !string.IsNullOrEmpty(sellableItem.ParentCategoryList))
                {
                    var parentCatalog = catalogs.Items.FirstOrDefault(i => i.SitecoreId.Equals(sellableItem.ParentCatalogList));
                    if (parentCatalog != null)
                    {
                        if (string.IsNullOrEmpty(sellableItem.ParentCategoryList))
                        {
                            await _associateSellableItemToParentCommand.Process(context.CommerceContext, parentCatalog.Id, parentCatalog.Id, sellableItem.Id);
                        }
                        else
                        {
                            foreach (string categorySitecoreId in sellableItem.ParentCategoryList.Split('|'))
                            {
                                var parentCategory = categories.Items.FirstOrDefault(i => i.SitecoreId.Equals(categorySitecoreId));
                                if (parentCategory != null)
                                {
                                    await _associateSellableItemToParentCommand.Process(context.CommerceContext, parentCatalog.Id, parentCategory.Id, sellableItem.Id);
                                }
                            }
                        }
                    }
                }
            }
        }
Exemple #3
0
        private async Task AssociateSellableItemToParent(SellableItem sitem, Catalog catalog, Category category, CommercePipelineExecutionContext context)
        {
            var    sItemId    = sitem.Id;
            string catalogId  = GenerateFullCatalogName(catalog.Name);
            string categoryId = GenerateFullCategoryId(catalog.Name, category.Name);
            var    catalogReferenceArgument = await _associateSellableItemToParentCommand.Process(context.CommerceContext, catalogId, categoryId, sItemId);

            //Add the new sellable item to the category
            var referenceArgument = await commander.Command <AssociateSellableItemToParentCommand>()
                                    .Process(context.CommerceContext, catalog.Id, category.Id, sItemId);
        }