2

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.

3

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;
    }
  • One of my previous scripts used the array for the attributes in addImageToMediaGallery, however that didn't seem to do the trick either. You're probably right about the product save though. I'll have to move those back out of the loop. Would file permissions be a factor? I wasn't sure if I should set a umask for this script or not. Thanks for your response on setMediaAttribute, will definitely be digging into that. As a side note in my original code I set it to die on the first success, so it may not be completing the loop of all 3, in any event I had passed an array before to no avail. – DWils Sep 27 '13 at 17:07
  • This still is not setting the image thumbnail, it just adds the image to the gallery as my previous script did. Any suggestions? – DWils Sep 27 '13 at 21:40
  • 1
    Odd, should have worked, as I use that same code. on a site. All I can suggest is to try your code in a vanilla install, as I suspect you may have a module interrupting the process. You can also do a bit of a debug in the routine i mention, see what is actually happening when you import. If you are not using xdebug, with debug breakpoints, I also suggest you get that going, as it will make your debugging live soooooo much easier. good luck. Here to help more if you got some more info. – ProxiBlue Sep 28 '13 at 10:41
  • Hmm, well I usually run this in a shell on the server, so I opened two terminal windows to see what pops up in var/log using multitail during runtime, all I got was repeats of this: system.log – DWils Sep 28 '13 at 21:02

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.