1

I am creating products with code and I am also adding images. This is the code:

$mediaArray = array(
    'thumbnail'   => $mainImage,
    'small_image' => $mainImage,
    'image'       => $mainImage,
);

foreach($mediaArray as $imageType => $fileName) {
     $filePath = $fileName;
    if (file_exists($filePath)) {
        try {
            $product->addImageToMediaGallery($filePath, $imageType, false, true);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }
}

Now this works, but they are not excluded from the gallery images. How would I do that?

Thanks!

1

Try this:

<?php
  $mediaArray = array(
    'thumbnail'   => $mainImage,
    'small_image' => $mainImage,
    'image'       => $mainImage
    );

foreach($mediaArray as $imageType => $fileName) {
     $filePath = $fileName;
    if (file_exists($filePath)) {
        try {
            $product->setMediaGallery (array('images'=>array (), 'values'=>array ()));
            $product->addImageToMediaGallery($filePath, array($imageType), false, true);
            //OR
            // $product->addImageToMediaGallery($filePath, array('gallery'), false, true);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }
}
?>
1
  • Thanks a lot, but it is not working ... any other ideas?
    – Arnie
    Jan 28 '16 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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