Try our conversational search powered by Generative AI!

How to set primary category in Commerce 11?

Vote:
 

I have just migrated my project to the Commerce 11 and found out that after the product import (custom import) all the links to the products has changed. Now all products are directly referenced under the catalog root.

As I understand this is caused by the primary category change mentioned here:

https://world.episerver.com/documentation/upgrading/episerver-commerce/commerce-11/breaking-changes-commerce-11/

and here:

https://world.episerver.com/blogs/David-Bowen/Dates/2017/7/commerce-11-release/

But I cannot find IsPrimary property on the NodeRelation as it is stated in the first link. How can I set the primary category?

Now when importing, I am setting just parent reference like this for the new product:

var productContent = ContentRepository.Service.GetDefault(parentReference);

And I am moving existing products:

var productContent = ContentRepository.Service.Get(productReference);
if (productContent.ParentLink != parentReference)
{
    var newLink = ContentRepository.Service.Move(productContent.ContentLink, parentReference, AccessLevel.NoAccess, AccessLevel.NoAccess);
    productContent = ContentRepository.Service.Get(newLink);
}

parentRefrence here is the link to the category it should be in (primary category).

All other categories are set by relations:

var oldRelations = RelationRepository.Service.GetParents(productContent.ContentLink);
foreach (var nodeRelation in oldRelations)
{
    RelationRepository.Service.RemoveRelation(nodeRelation);
}

var relations = categoryLinks
    .Select(
        categoryLink => new NodeRelation
        {
            Child = productContent.ContentLink,
            Parent = categoryLink,
        });
RelationRepository.Service.UpdateRelations(relations);
#181705
Aug 28, 2017 16:34
Vote:
 

We are going to introduce NodeEntryRelation, and that's where you can set IsPrimary. It'll be included in Commerce 11.2, releasing this week :) 

#181706
Aug 28, 2017 17:07
Vote:
 

Thanks, Quan, but there should be some way to work with the current version too.

I am looking at the decompiled code for moving the content in the Commerce and I see that it adds default primary relation for the product. It takes the product's parent category link and the product's link when creating it.

I found in my code that I was creating another relation for the parent category and it might overwrite the primary one. But after the fix, it still doesn't work. :(

#181708
Aug 28, 2017 18:25
Vote:
 

I hope there was (When it comes to business, I don't mind workarounds as long as they work) - but 11.2 is what you need. It contains a very nice performance improvment in promotion engine - so I recommend to upgrade instead. 

I can send you the release packages for 11.2 if you want - they will take some times to get on the feed 

#181709
Aug 28, 2017 19:19
Vote:
 

I'm not quite following your description of the problem you are seing. If you use IContentRepository.Move on an entry, it will make sure there is a Relation with IsPrimary=true for the desired parent. That is, if there already is another primary relation it will be removed, and a relation to the desired primary relation is either added or updated (if it already exists but is not primary). So using the Move operation to set the primary category should work just fine.

The problem that I see with your code is if it runs in the order you have it in the post. Because after the move operation, you remove ALL the category relations including the primary one you just set by moving.

#181713
Aug 29, 2017 8:10
Vote:
 

Good point Magnus. I haven't noticed that I am removing also the primary relation.

But if I cannot add the primary relation manually to the relations, why it is listed there?

Next, while Move should work, I couldn't find any place where the new primary relation is created for a new product.

#181715
Aug 29, 2017 8:35
Vote:
 

It is listed because it is a relation like all others, just that it has the special primary flag the content provider uses to select the parent link. Not showing it seems to cause some confusion, but as Quan said, IsPrimary will be directly available on the relation object in 11.2.

For new entries, the ParentLink you use when creating it will be the primary relation.

#181717
Aug 29, 2017 8:50
Vote:
 

Thanks for the clarification.

One thing is still not clear to me. Yesterday I found another solution to update IsPrimary (through NodeEntryRelationModelCommitter) and products got fixed, but not variations. Variations still had no primary category. Only after re-saving of the variation, it was fixed.

How does Commerce set the primary category for variations? There is no relation for it (or is?).

#181720
Aug 29, 2017 8:55
Vote:
 

When it comes to node-entry relation, all entries are treated the same - so I'm not sure why you get the different behavior with variants 

#181724
Aug 29, 2017 9:20
Vote:
 

NodeEntryRelationModelCommitter is not something you should use directly. If you need/want to do direct manipulation of IsPrimary in your current version I suggest you resort to the ICatalogSystem / CatalogRelationDto instead.

A different behavior for variations makes no sense. Are you sure it isn't some other issue with your code, like removing and re-adding the categories after creating the variation?

#181728
Aug 29, 2017 9:25
Vote:
 

NodeEntryRelationModelCommitter was a temp fix while waiting for the new Commerce version. As it was used in Service API for the same purpose, I thought that it works as expected.

Regarding variations, I did not manipulate NodeRelation directly as it is Category -> Product relations as I understand. Do variations have similar relation - Category -> Variation?

#181729
Aug 29, 2017 9:29
Vote:
 

No, it's only NodeEntryRelation, for entry part it can be product, variant, package or bundle. 

#181730
Aug 29, 2017 9:31
Vote:
 

Ah, ok. That explains why it fixes variations only after re-saving. Then it creates the Category -> Variation relation automatically on save.

#181731
Aug 29, 2017 9:34
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.