magentophpmage

magento - Update product default image to first image in image gallery


I have an import script that imports well over 2000+ products including their images. I run this script via CLI because I feel that this is the best way to go speed-wise even though I have the same import script available and executable at the magento admin as an extension. The script runs pretty well. Almost perfect! However, sometimes the addToImageGallery somehow malfunctions and results into some images having No Image as the default product image and the only other image as not selected as defaults at all. How do I mass-update all products to set the first image in the media gallery for the product to the default 'base', 'image' and 'thumbnail' image(s)?


Solution

  • I found a couple of tricks on doing this (and more) on this link:

    http://www.magentocommerce.com/boards/viewthread/59440/ (Thanks transio!)

    Although, for Magento 1.6.2.0 (which I use), the first SQL trick there (Trick 1 - Auto-set default base, thumb, small image to first image.) needs a bit of modification.

    On the second-to-the last-line there is a AND ev.attribute_id IN (70, 71, 72) part. This should point to attribute ID's which will probably not be relevant in Magento 1.6.2.0 anymore. To fix this, using any MySQL query tool (PHPMyAdmin or MySQL Query Browser), I took a look at the catalog_product_entity_varchar table. There should be entries like:

    value_id, entity_type_id, attribute_id, store_id, entity_id, value
    ..
    146649, 4, 116, 0, 1, '2'
    146650, 4, 76, 0, 1, ''
    146651, 4, 78, 0, 1, ''
    146652, 4, 79, 0, 1, '/B/0/B05-01.jpg'
    146653, 4, 80, 0, 1, '/B/0/B05-01.jpg'
    146654, 4, 81, 0, 1, '/B/0/B05-01.jpg'
    146655, 4, 96, 0, 1, ''
    146656, 4, 100, 0, 1, ''
    146657, 4, 102, 0, 1, 'container2'
    ..
    

    My money was on the group of three image paths as possible replacements. So the resulting SQL now should be:

    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 (79, 80, 81) # <-- attribute IDs updated here
        AND mgv.position = 1;
    

    So I committed to it, ran it and.. presto! All fixed! You might also want to encapsulate this in a transaction if you want. But this is out of this question's scope.

    Well, this is the fix that worked for me so far! If there are any more out there, please share!