Skip to content

Instantly share code, notes, and snippets.

@uabassguy
Created September 27, 2013 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uabassguy/6724019 to your computer and use it in GitHub Desktop.
Save uabassguy/6724019 to your computer and use it in GitHub Desktop.
Magento image import script (doesn't work)
<?php
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 = $sku . ".jpg";
// Add three image sizes to media gallery
$mediaArray = array(
'thumbnail' => $imgFile,
'small_image' => $imgFile,
'image' => $imgFile,
);
foreach($mediaArray as $imageType => $fileName) {
$filePath = $importDir.$fileName;
if ( file_exists($filePath) ) {
try {
$product->addImageToMediaGallery($filePath, $imageType, false, false);
$product->save();
echo "Sku: $sku updated.\n";die();
} catch (Exception $e) {
echo $e->getMessage();
}
} else {
echo "Could not match image to $sku. Path was: {$filePath}\n";break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment