我有以下代码,应该使用csv文件中的产品sku更新Magento中的产品图像。
在我的csv文件中,有两列,Product SKU中的First列和Image Name中的第二列。
}
请告诉我我的代码有什么问题。
答案 0 :(得分:1)
foreach ($skuImageMap as $sku => $images) {
$ourProduct = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
$saveFlag = true;
if ($ourProduct) {
foreach ($images as $index => $imagePath) {
if (file_exists($imagePath)) {
try {
$ourProduct->addImageToMediaGallery($imagePath, null, false, false);
$imageCount++;
} catch (Exception $e) {
$saveFlag = false;
echo "There was an issue saving image $imagePath to $sku :" . $e->getMessage() . "\n";
}
} else {
$saveFlag = false;
echo "The image $imagePath does not exist.\n";
}
}
if($saveFlag) {
try {
$ourProduct->save();
echo "The image $imagePath was added to $sku successfully : $imageCount \n";
} catch (Exception $e) {
zend_debug::dump($e->getMessage());
}
}
} else {
echo "The product $sku does not exist \n";
}
}