private async Task <Category> CreateOrGetCategory(CommercePipelineExecutionContext context, Catalog catalog, string categoryName, string parentCategoryId = null) { try { string categoryId = GenerateFullCategoryId(catalog.Name, categoryName); //Check if catalog with given name already exists before trying to create a new one Category category = await _findEntityCommand.Process(context.CommerceContext, typeof(Category), categoryId, false) as Category; if (category == null) { context.Logger.LogInformation($"ProductImport:Category Not Found, result is nill _getCategoryCommand:{categoryId}"); context.Logger.LogInformation($"ProductImport:Creating Category: {categoryId}"); category = await _createCategoryCommand.Process(context.CommerceContext, catalog.Id, categoryName, categoryName, categoryName) as Category; category.ParentCatalogList = catalog.SitecoreId; } else { context.Logger.LogInformation($"ProductImport: Category found _findEntityPipeline, catalog: {categoryName}"); } return(category); } catch (Exception ex) { context.Logger.LogInformation($"ProductImport: Error during catelog process" + ex.Message); return(null); } }
private async Task <Category> CreateOrGetCategory(CommercePipelineExecutionContext context, Catalog catalog, string categoryName, string parentCategoryId = null) { try { string categoryId = GenerateFullCategoryId(catalog.Name, categoryName); //Check if catalog with given name already exists before trying to create a new one Category category = await _findEntityCommand.Process(context.CommerceContext, typeof(Category), categoryId, false) as Category; if (category == null) { context.Logger.LogInformation($"ProductImport:Category Not Found, result is nill _getCategoryCommand:{categoryId}"); context.Logger.LogInformation($"ProductImport:Creating Category: {categoryId}"); category = await _createCategoryCommand.Process(context.CommerceContext, catalog.Id, categoryName, categoryName, categoryName) as Category; //category.ParentCatalogList = catalog.SitecoreId; //int version = category.Version; //if(category.Version != version + 1) //var newItemVersion = category.EntityVersion + 1; //await _addEntityVersionCommand.Process(context.CommerceContext, category, newItemVersion); //Save Category Result //var result = await _commander.Pipeline<IPersistEntityPipeline>() // .Run(new PersistEntityArgument(category), context.CommerceContext.GetPipelineContextOptions()); //if (string.IsNullOrWhiteSpace(parentCategoryId)) // await _associateCategoryToParentCommand.Process(context.CommerceContext, catalog.Id, catalog.Id, category.Id); //else // await _associateCategoryToParentCommand.Process(context.CommerceContext, catalog.Id, parentCategoryId, category.Id); return(category); } else { context.Logger.LogInformation($"ProductImport: Category found _findEntityPipeline, catalog: {categoryName}"); } return(category); } catch (Exception ex) { context.Logger.LogInformation($"ProductImport: Error during catelog process" + ex.Message); return(null); } }