Add image to variant from code

Hi,

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: 7.2.3.0

Hi,
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);
_variantService.Update(variant);
1 Like

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