Add image to variant from code


Is it possible to add a media file to a variant entirely from code? The imagefile i want to add already exists in the media library. Is it possible to update the Image field directly or would i need to connect the variant to the file with a pointer?

Litium version:

If you want to add a media file entirely from code you have to do do somthing like that:
You have to inject the variant service in the constructor of your class.

var variant = _variantService.Get("your variant id or system id");
variant = variant.MakeWritableClone()
// image ids is a list of Guid that contains all the image ids that will be connected to the variant
variant.Fields.AddOrUpdateValue(SystemFieldDefinitionConstants.Images, imageIds);
1 Like

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