我有一个magento网站,我的产品通过csv文件上传。现在我需要更新该产品的图片。我所有的图片都被下载并保存在我的magento网站媒体文件夹中。我创建了一个csv文件,其中包含图像文件的"SKU"answers"绝对路径"。
然后我试图用以下代码加载图像
<?php
require_once 'app/Mage.php';
Mage::app();
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
$importDir = Mage::getBaseDir('media') . DS . 'Products/Vases/';
$file_handle = fopen("sku_image.csv", "r");
while (($data = fgetcsv($file_handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
$productSKU = $data[0];
$ourProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);
for ($c=1; $c < $num; $c++) {
$fileName = $data[$c];
$filePath = $importDir.$fileName;
if (file_exists($filePath)) {
if ($ourProduct) {
$ourProduct->addImageToMediaGallery($filePath, array('image', 'small_image', 'thumbnail'), true, false);
$ourProduct->save();
}
} else {
echo $productSKU . " not done";
echo "<br>";
}
}
}
fclose($file_handle);
?>
问题是最后一个图像被设置为产品媒体属性中的所有字段。如何通过上面的代码将图像添加到不同的媒体属性中。
我已将我的代码更改为以下
<?php
require_once 'app/Mage.php';
Mage::app();
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
$importDir = Mage::getBaseDir('media') . DS . 'Products/Vases/';
$file_handle = fopen("sku_image.csv", "r");
while (($data = fgetcsv($file_handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
$productSKU = $data[0];
$ourProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);
for ($c=1; $c<$num; $c++) {
$fileName = $data[$c];
$filePath = $importDir.$fileName;
if (file_exists($filePath)) {
if ($ourProduct) {
if ($c==1) {
$ourProduct->addImageToMediaGallery($filePath, 'rotator_image', false, false);
} else {
$ourProduct->addImageToMediaGallery($filePath, null, false, false);
}
$ourProduct->save();
}
} else {
echo $productSKU . " not done";
echo "<br>";
}
}
echo "<br>";
}
fclose($file_handle);
?>
现在我的前台网站已经崩溃了。无法在我的网站中加载任何产品。有人想我错过了什么吗?
这是因为您正在循环。
每次调用$ourProduct->addImageToMediaGallery($filePath, array('image', 'small_image', 'thumbnail'), true, false);
图像时,small_image&缩略图被设置为所添加的新图像。
如果你查看对你正在调用的函数的评论,你会看到这是写出来的。
/**
* Add image to media gallery
*
* @param string $file file path of image in file system
* @param string|array $mediaAttribute code of attribute with type 'media_image',
* leave blank if image should be only in gallery
* @param boolean $move if true, it will move source file
* @param boolean $exclude mark image as disabled in product page view
* @return Mage_Catalog_Model_Product
*/
public function addImageToMediaGallery($file, $mediaAttribute=null, $move=false, $exclude=true)
因此,如果您希望第一个图像是用于媒体属性的图像,可以在调用addImageToMediaGallery之前添加一个复选框。然后传入null或要设置的属性。
您需要使用的确切方法将取决于csv文件的设置。产品是否有序,以及其他类似问题。然后你就可以算出要使用的确切支票了。
使用以下脚本按语法添加额外的图像。
http://www.pearlbells.co.uk/how-to-add-main-image-and-additional-image-to-the-products-pro-grammatically-magento/
$importDir = Mage::getBaseDir('media') . DS;
// additional images
if ($import_product[29] != '') {
$addImages = explode(",", trim($import_product[29]));
foreach ($addImages as $additional_image) {
$image_directory = $dir .DS.'data'.DS. trim($additional_image);
if (file_exists($image_directory)) {
$product->addImageToMediaGallery($image_directory, null, false, false);
} else {
$image_directory = $dir . 'data' . DS . 'comingsoon.jpg';
$product->addImageToMediaGallery($image_directory, null, false, false);
}
}
echo 'Additional images for product ' . $product->getName() . ' ' . $product->getId() . ' imported successfully' . PHP_EOL;
}
为产品导入图像的最佳和最快方法是Magmi插件。您必须将所有图像放在/media/import文件夹下,只需在csv中创建一个名为sku small_image base_image和thumbia的列。
对于图像列,只需输入图像路径为/imagename.jpg
确保在magmi中启用图像属性处理器插件enable。它将在几秒钟内完成导入。您也可以通过magmi从url导入产品图像http://wiki.magmi.org/index.php?title=Image_attributes_processor