0

I have imported a number of products at once, but the problem is: the Base Image, Small Image, Thumbnail are all set to the same image.

On the catalog page, I'm trying to change the image shown when you hover over a product. I believe, to do this, the small image and thumbnail image would need to be set to different images for the same product.

How can I achieve this?

I found below query, but I do not understand how to modify it to meet my requirement.

UPDATE
    catalog_product_entity_media_gallery AS mg,
    catalog_product_entity_media_gallery_value AS mgv,
    catalog_product_entity_varchar AS ev
SET
    ev.value = mg.value
WHERE
    mg.value_id = mgv.value_id
AND
    mg.entity_id = ev.entity_id
AND
    ev.attribute_id IN (70, 71, 72)
AND
    mgv.position = 1;

Please, can you help me find the right modifications.

  • anyone please..? – Joey River Nov 26 '13 at 9:30
  • 3
    Golden rule of Magento: Don't touch Magento DB directly. If you want I can give you solution how programmatically change media images – mageUz Nov 26 '13 at 11:37
  • @mageUz PLease provide me a solution for my above problem? I need to change the product image on hover on the catalog page – Joey River Nov 26 '13 at 12:39
  • For some reason this is not working for me even if I checked the attribute_id:s from database and made corrections. It only set the images for 3 products. All the rest of the hundreds of products remain unchanged. – Webninja Apr 12 '17 at 12:37
1

Here is given programmatically adding media images to product, you can adjust this code to your own importing script:

    $mediaGalleryAttribute = Mage::getModel('catalog/resource_eav_attribute')
        ->loadByCode(Mage_Catalog_Model_Product::ENTITY, 'media_gallery');
    $mediaGallery          = $mediaGalleryAttribute->getBackend();
    $attrCode              = $mediaGalleryAttribute->getAttributeCode();


    $product          = Mage::getModel('catalog/product')->load($productId);
    $mediaGalleryData = $product->getData($attrCode);
    /**
     * If you want to remove old images use this code
     */
    if (isset($mediaGalleryData['images'])) {
        foreach ($mediaGalleryData['images'] as &$image) {
            $image['removed'] = 1;
        }
        $product->setData($attrCode, $mediaGalleryData);
    }
    /**
     * Here is real image file path
     */
    $thumbnailFile = 'real/path/to/thumbnail/file.jpg';
    $imageFile     = 'real/path/to/image/file.jpg';
    try {
        //Set thumbnail
        $imageFileUri = $mediaGallery->addImage($product, $thumbnailFile, null, false, false);
        $mediaGallery->setMediaAttribute($product, 'thumbnail', $imageFileUri);

        //Set image (or base image, small image whatever you want
        $imageFileUri = $mediaGallery->addImage($product, $imageFile, null, false, false);
        $mediaGallery->setMediaAttribute($product, 'image', $imageFileUri);
        $product->save();
    } catch (Exception $e) {
        Mage::logException($e);
    }
  • Perfect, did exactly what I need to do. Thanks a lot:) – Chiragit007 Nov 19 '14 at 9:06
0

May my solution works for you:

  1. I would introduce an attribute (image) called for example "hover_image"

  2. Use this function from Marius to set for example every second media-image as your hover-image https://stackoverflow.com/questions/19153891/magento-how-to-programatically-set-the-base-image-to-the-first-image-in-the-lis Take care - you have to modify it to use the second image.

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.