0

I add an image to product's image gallery with this code:

// $filePath: image full path
$mediaArray = array(
    "thumbnail"   => $filePath,
    "small_image" => $filePath,
    "image"       => $filePath,
);

$product->addImageToMediaGallery($filePath, $mediaArray, true, false);

$product->save();

After this code, the image will be added to gallery but it is unselected. How can I select this image as "base image" (and/or Small Image, Thumbnail)?

1 Answer 1

2

The $mediaArray parameter must be an array with values any of the image types you want to set. So instead of :

$mediaArray = array(
    "thumbnail"   => $filePath,
    "small_image" => $filePath,
    "image"       => $filePath,
);

Use this:

$mediaArray = array(
    "thumbnail",
    "small_image",
    "image",
);

Or any other combination of image attributes you have even an empty array if you don't want to set the image as main image for an attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.