2

I use an import script to create products with associated images that are assigned to roles thumbnail and small image.

It works well, products and images are imported and they display as thumbnail and small image on the front-end website.

But when I try to edit one of those products via Magento admin, the roles thumbnail and small image are not selected in the UI and saving the product will make this image's roles to be lost.

Why may cause effective thumbnail to not be detected as one when loading the Edit product page? Did I do something the bad way in this import script?

Any clue of what the source of this problem can be?

Here is the import code:

    $product->setMediaGallery(array(
                                  'images' => array(),
                                  'values' => array()
                              ));

    foreach ($product['pictures'] as $key => $picture)
    {
        $image_url = $picture['picture'];
        $image_ext = pathinfo($image_url, PATHINFO_EXTENSION);
        $imageData = base64_encode(file_get_contents($image_url));
        $image = $this->_imageContentFactory->create()
            ->setType('image/jpeg')
            ->setName($sku . '-' . $key . '.' . $image_ext)
            ->setBase64EncodedData($imageData);

        $entry = $this->_productAttributeMediaGalleryEntryFactory->create();
        $entry->setContent($image);
        $entry->setMediaType('image');
        if (0 == $key)
            $entry->setTypes(['image', 'small_image', 'thumbnail']);
        else
            $entry->setTypes(['image']);

        $entry->setDisabled(false);
        $entry->setPosition($key);

        $entries[] = $entry;
    }

    $product->setMediaGalleryEntries($entries);

    $product->setImage($entries[0]->getFile());
    $product->setThumbnail($entries[0]->getFile());
    $product->setSmallImage($entries[0]->getFile());
    $this->_productRepository->save($product);

EDIT: after more investigation, it appears roles are only lost for 'all store views', in any other store views, the image rules are correctly detected.

  • I am facing this exact issue! Only difference is we are using default Magento import – Hashid Apr 7 '18 at 6:56
0

you need to used this to import product with images.

// Adding Image to product
$imagePath = $entries[0]->getFile(); // path of the image
$product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), false, false);
$product->save();
| improve this answer | |
  • Thanks for your help Dhiren, but I think this is not a good thing to use the almost deprecated $product->save() method, that's why i use repositories to save my product, and I never manage to got it work with addImageToMediaGallery(), i added the line where I save the product to the code snippet for info – Gael COAT Oct 28 '16 at 14:41
0

Just excute the sql command in your database

SELECT * FROM catalog_product_entity_varchar WHERE value LIKE 'no_selection'

For more info check this out

https://support.magento.com/hc/en-us/articles/115002446014-Product-images-do-not-display-despite-Product-Edit-image-roles-

| improve this answer | |

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.