1

In Sitecore Commerce 9.3, I'm attempting to import and sync categories, but I am not able to create or update categories programmatically while getting info for existing categories via _getCategoryCommand as shown on screenshot but _createCategoryCommand is neither throwing any exception nor creating categories.

No error logs were found. When _createCategoryCommand executes it never comes to the next line.

i.e. category.Name = parameter.Name.

I've read & followed this article - https://hachweb.wordpress.com/2018/09/09/sitecore-xc-9-0-2-walkthrough-of-creating-your-own-product-importer-with-various-pitfalls/

enter image description here

2
  • Is this only on creating category or do you face the same issue if you create a Catalog? Found a different code that casts it to a category after creating category = await _createCategoryCommand.Process(CurrentContext, catalog.Id, categoryModel.Name, categoryModel.DisplayName, categoryModel.Description) as Category; and last question is your catalogus you are refuring to existing?
    – Danny Paul
    Commented Mar 21 at 7:23
  • Please add code lines in place of Image for SEO purposes. Commented Mar 21 at 11:05

1 Answer 1

1

Try to use the below methods to create and update the category. You can modify the code accordingly to check the existing category.

  1. Find existing category -

    using Sitecore.Commerce.Plugin.Catalog;
    
    using System.Linq;
    
    using System.Threading.Tasks;
    
    public async Task<Category> FindCategoryByName(string categoryName)
    {
         // Get all categories
         var categories = await _commerceCommander.GetEntities<Category>(context.CommerceContext, CommerceEntity.ListName<Category>(), 0, int.MaxValue);
    
        // Find the category by name
        var category = categories.FirstOrDefault(c => c.DisplayName.Equals(categoryName, StringComparison.OrdinalIgnoreCase));
    
       return category;
      }
    
  2. Create a category -

    using Sitecore.Commerce.Core;
    using Sitecore.Commerce.Plugin.Catalog;
    using System;
    using System.Threading.Tasks;
    
    public async Task CreateCategory(string categoryName, string parentCategoryName = "")
    {
       // Assuming you have a Commerce Engine instance initialized
    
       // Create a new category
       var category = new Category
     {
         DisplayName = categoryName,
         ParentCategoryList = new ListEntityComponent
         {
             Items = new List<string> { parentCategoryName }
         }
     };
    
     // Add the category to the catalog
     await _commerceCommander.Pipeline<ICreateCategoryPipeline>().Run(new CreateCategoryArgument(category), context);
    }
    
  3. Update a Category

    using Sitecore.Commerce.Plugin.Catalog;
    using System.Threading.Tasks;
    
    public async Task UpdateCategory(string categoryId, string newCategoryName)
    {
       // Load the category
       var category = await _commerceCommander.GetEntity<Category>(context.CommerceContext, categoryId);
    
       if (category != null)
       {
          // Update the category name
          category.DisplayName = newCategoryName;
    
          // Save the changes
          await _commerceCommander.Pipeline<IUpdateCategoryPipeline>().Run(category, context.CommerceContext);
       }
    }
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.