Correct way to change Product-Category relation


Hi all,

[using commerce 9.24]

A while back, I related products (and variants) to Categories, by doing this:

// Create new Category relation:
                    var nodeRelation = relations.NewNodeEntryRelationRow();
                    nodeRelation.CatalogId = _catalogId;
                    nodeRelation.CatalogEntryId = catalogEntryId;
                    nodeRelation.CatalogNodeId = targetCategoryNode.CatalogNodeId;
                    nodeRelation.SortOrder = 0;
                    if (nodeRelation.RowState == DataRowState.Detached)

Today, I'd like to change those relations -- I'd like to relate those products to a different category (and thus remove the previous relation).

How can I accomplish this programmatically?


 - Ken

Edited, Feb 22, 2018 0:16

In Commerce we use the term "association" when it's entry-entry (think about related products), and "relation" when it's parent-child. Minor, but it would be easier to follow.

To move a product to a new category, you have to option:

  • Use IContentRepository.Move - CatalogContentProvider will handle things under the hood.
  • You can of course get the current NodeEntryRelationRow and change the CatalogNodeId to the new one, then save it.

If you upgrade to Commerce 11+ then IRelationRepository can be used to Remove/Update relations (yes, it's already in Commerce 9 but the APIs were significantly changed in Commerce 11 so I'd not really recommend to use it unless you upgrade)

Feb 22, 2018 1:43

(^ fixed the terminology in my first post...)

Feb 22, 2018 21:48

Thanks Quan,

I used the second option. It SEEMED to work - because now when I retrieve the (only) NodeEntryRelationRow for my product, it correctly shows the new Category. 

However - when I view the product in CMS admin screen (in the "Belongs To" tab) it shows BOTH categories.  Is there another piece of data I need to clean up somewhere?

Feb 22, 2018 23:59

No that should be it. It does sound like there was a cache some where. Can you turn on the browser console and disable cache to see if the problem is still there?

Feb 23, 2018 10:43

You're right Quan, there's some caching happening (Epi's cache?). 

The anomalies I'm seeing sometimes persist for up to 10 minutes - but eventually do resolve themselves to the correct data..

Is there a way i can clear the cached data programmatically?

Feb 23, 2018 21:40

Are you running the code to update the relation in the same site? Or on a different instance? The cache should be invalidated automatically if the remote events are configured correctly.

9.24 is quite old (2 years ago, more or less), so I'm not sure, there might be a bug which was fixed in later version. However that would be quite unlikely. 

Feb 23, 2018 23:25

Huh now that you mention it -- we are running the code in a different Epi instance (but sharing the same databases).

Do you think that is why the cache persists?

Would we have a better experience if we used IContentRepository.Move?

Feb 24, 2018 0:08

 IContentRepository.Move uses same cache mechanism, so if you don't fix the cache invalidation issue, it will be the same.

The cache uses the remote events to broadcast messages - so make sure you have the right configuration 

Feb 24, 2018 8:39
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.