Get MainCategory from BaseProductToCategoryLink collection

[Question (in english) - remember to come back and check the answer that solves your question to mark it as resolved, formatting of messages with markdown]

Litium version: [6.3.7]

I’m working on an article import which needs to be modified, and in the part where variants are added to a MainCategory it is fetched like this:
var editableBaseProduct = baseProduct.MakeWritableClone();
mainCategory = editableBaseProduct.CategoryLinks.First(c => c.MainCategory);
Add variants to it:
mainCategory.ActiveVariantSystemIds.Add(variant.SystemId);

It is working fine but then I noticed that on a product with 2 categories, both of them had MainCategory as “true” when i was debugging, that shouldn’t be possible or should it?

If I use the GetMainCategory extension one category is returned. I went on to check in Litium/Plan section and saw that the category that was returned was not selected, the other category was, why is that?

Basically I need to know if I’m adding variants to the correct MainCategory.

One base product can only have one main category selected in each assortment.

There is only one assortment, the image shows 2 categories that belong to the same assortment as I see it.

If I do: baseProduct.GetMainCategory(CurrentState.Current.WebSiteID); I get the “Glasull” category which is not the selected one as in the image.
When I look in baseProduct.CategoryLinks both categories have MainCategory property as “true”.

I can use the extension for getting the MainCategory but I wanted to know if this is a bug or is this behaviour normal?

Please, report that as a bug on our docs-site, it sounds like a validation rule is missing.

Ok, thanks for your help!

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