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.

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

2 Answers 2

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();
1
  • 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, 2016 at 14:41
0

Just execute the SQL command in your database:

SELECT *  FROM `catalog_product_entity_varchar` WHERE `value` LIKE 'no_selection'

For more info check this support page out.

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.