Created
September 27, 2013 04:12
-
-
Save uabassguy/6724019 to your computer and use it in GitHub Desktop.
Magento image import script (doesn't work)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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