Magento – Setting image, small_image, and thumbnail for media gallery imports

frontendgalleryimageimportmagmi

I have been faced with this dreaded issue that many have reported and failed to get working.

My goal is to add images to products by matching SKU with the file, which is basically $sku . ".jpg"

What I have done was imported a CSV using Magmi, all products were imported and I added images to each media gallery. The images, however, are all set to No Image in the admin panel. see screenshot http://screencloud.net/img/screenshots/b2b8cd4d67e82ff25c5e0ca8d34aef57.png

I created several scripts over the last few days in attempts to remedy the issue, however none of them have been successful. Here is my most recent script: script on gist

The images import fine, they are just not being set to thumbnail or small_image accordingly. I have even truncated the images in the database and physically removed the images to "start fresh" if you will.

I'm already 3 days into this issue and any help would be greatly appreciated, as none of the products show thumbnail or small_image on the frontend, nor the image base at this point.

I could set each image manually on each product, but on a database with 7000 products, it would take an insane amount of time to do each individually. I will also have to repeat this process when we do our next import of 200,000+ skus, so I need to get this working.

Best Answer

Try passing an array as the media type. Theer is no need to iterate over each mediaType and setting them individually.

your code would thus be, which will be faster as well.

ini_set('memory_limit', '-1');

require 'app/Mage.php';
Mage::app();

echo "Loading catalog...\n";
$products = Mage::getModel('catalog/product')->setStoreId(13)->getCollection()->addAttributeToSelect('*');
echo count($products) . " products selected for image import.\n";
// Remove unset images, add image to gallery if exists
$importDir = Mage::getBaseDir('media') . DS . 'import/';  

foreach ($products as $product) {
        $sku = Mage::getModel('catalog/product')->load($product->getId())->getSku();
        if (strstr($sku, " ")) {
            echo "Sku $sku contains whitespace, skipping.\n";
            continue;
        }
        $imgFile = $importDir . $sku . ".jpg";
        // Add three image sizes to media gallery
        $mediaArray = array(
            'thumbnail',
            'small_image',
            'image'
        );
        if (file_exists($imgFile)) {
            try {
                $product->addImageToMediaGallery($imgFile, $mediaArray, false, false);
                $product->save();
                echo "Sku: $sku updated.\n";
            } catch (Exception $e) {
                echo $e->getMessage();
            }
        } else {
            echo "Could not match image to $sku. Path was: {$filePath}\n";
            break;
        }
    }

Anyways, I suspect your issue is related to the fact that your product->save was within the media loop of your original code.

For reference, the end-point of the mediaAttribute is this function, located in class Mage_Catalog_Model_Product_Attribute_Backend_Media

/**
     * Set media attribute value
     *
     * @param Mage_Catalog_Model_Product $product
     * @param string|array $mediaAttribute
     * @param string $value
     * @return Mage_Catalog_Model_Product_Attribute_Backend_Media
     */
    public function setMediaAttribute(Mage_Catalog_Model_Product $product, $mediaAttribute, $value)
    {
        $mediaAttributeCodes = array_keys($product->getMediaAttributes());

        if (is_array($mediaAttribute)) {
            foreach ($mediaAttribute as $atttribute) {
                if (in_array($atttribute, $mediaAttributeCodes)) {
                    $product->setData($atttribute, $value);
                }
            }
        } elseif (in_array($mediaAttribute, $mediaAttributeCodes)) {
            $product->setData($mediaAttribute, $value);
        }

        return $this;
    }
Related Topic