You can't move a variant to a product. If you want to assign a variant to a product, you would have to use IRelationRepository to save a new ProductVariation relation
var relation = new ProductVariation();
relation.Parent = Product.ContentLink;
relation.Child = Variant.ContentLink;
_relationRepository.UpdateRelation(relation);
Hi Quan
Hmm, it seems contrary to creating a new variation which you can have the product as its parent:
var newVariation = contentRepository.GetDefault<MyVariation>(productLink);
And also in the UI, it seems to support this also:
Oh and additionally the context menu to create a new variation under the product is offered too:
Are you sure it shouldn't be possible to move a variant to under a product?
Good question. But the UI does not have to reflect the API 100%. It is used to make sure the life for editors as easy and as productive as possible. As a developer you should understand the underlying implementation and go with it.
Hi Quan, sure but the creation content API does allow putting a variation under a product.. And from noseying around the catalog content provider's move logic, I think it might just be a simple bug - looks like they prepared it to handle move of entries versus nodes differently but then just applied the same check anyway.. Feels like a mistake to me.
The API example you showed just set the ParentLink to the product. It will not work if you call GetChildren on the product or GetAncestors on the variants. I would say that is the inconsistency but sometimes we don't bother to check those conditions
I'll take your wisdom over mine :) But definitely wasn't intuitive behavior for the move logic for me.
Hi
I've got some logic that sometimes has to move a variation to beneath a product, I have code that looks like this:
Unfortunately it throws this error whenever it does this:
From digging around, it seems there might be a bug in the CatalogContentMoveHandler.. It seems to have two separate methods for handling the move of a entry vs node, but then both of those methods use the same GetNewParent private method which has a switch statement for the destination content type to only be CatalogNode or Catalog, otherwise throws the above error..
Is there a bug, or am I using this wrong?
We're on Commerce v11.8.5 (latest v11.x version)