Category references on base product created by integration is removed when changing main category

I’m currently working on an product integration and I’m having trouble with the category links on the base product. When integration is run everything seemes to be working good, I can see the references to the categorys under “plan” on the baseproducts. However, if i choose to add another category through back office or if I change the main category, all categorys imported by the integration is removed (The actual categorys do still exist though but without referencing the baseproduct as an reference). There arent any errors in the Litium log or the event log.

Im adding the category with productlinks like this :

                 var newCategory = new Category(productFieldTemplate.SystemId (Guid)market.AssortmentSystemId)
                {
                    Id = mainCategory,
                };
                foreach (var language in _languageService.GetAll())
                {
                    newCategory.Localizations[language.CultureInfo].Name = mainCategory;
                    newCategory.Fields.AddOrUpdateValue("_url", language.CultureInfo, _slugifyService.Slugify(CultureInfo.GetCultureInfo(language.Id), mainCategory));
                }
                newCategory.ProductLinks.Add(new CategoryToProductLink(baseProduct.SystemId)
                {
                    MainCategory = true,
                    BaseProductSystemId = baseProduct.SystemId
                });
                _categoryService.Create(newCategory);

Has anyone experienced this before or have any idea what could go wrong?

Litium version: 7.4.0

Is the URL on the base product?
Does the base product have any variants?

The URL is on the base product.
The base product has variants, yes.

If you set the ActiveVariantSystemIds on the CategoryToProductLink that you are creating to the variants that you want to be visible on the category.

If you only adding the base product as you are doing in the example no variants are assigned to the category and the link will automatic be removed during cleanup.

Example from https://github.com/martenw/litium-demo-helper/blob/master/Services/ProductDemoService.cs

public void AddToCategory(BaseProduct baseProduct, Category category)
{
    var linkAlreadyExist = baseProduct.CategoryLinks.Any(l => l.CategorySystemId.Equals(category.SystemId));
    if (linkAlreadyExist)
        return;

    var tmpBaseProduct = baseProduct.MakeWritableClone();
    var variants = _variantService.GetByBaseProduct(baseProduct.SystemId).ToList();
    var variantSystemIds = new HashSet<Guid>(variants.Select(v => v.SystemId));
    var link = new BaseProductToCategoryLink(category.SystemId)
    {
        ActiveVariantSystemIds = variantSystemIds
    };
    tmpBaseProduct.CategoryLinks.Add(link);
    _baseProductService.Update(tmpBaseProduct);
}
1 Like

Great, thank you. Worked perfectly!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.