简体   繁体   English

以编程方式将多个图像添加到Magento产品

[英]Adding multiple images to Magento Products programmatically

I have a magento site, my products are uploaded through a csv file. 我有一个magento网站,我的产品通过csv文件上传。 Now I need to update images for that products. 现在我需要更新该产品的图像。 All of my images are downloaded an saved in my magento site media folder. 我的所有图像都下载并保存在我的magento站点媒体文件夹中。 I have created a csv file with 'SKU' and 'Absolute path' of image files. 我创建了一个带有'SKU'和'绝对路径'的图像文件的csv文件。

Then I am trying to load that images with following code 然后我尝试使用以下代码加载图像

<?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);
?>

Problem is that last image is set to all fields in product media attributes. 问题是最后一个图像设置为产品媒体属性中的所有字段。 How can I add images to different media attributes through above code. 如何通过上面的代码将图像添加到不同的媒体属性。


I have changed my code as following 我已将代码更改为以下内容

<?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);
?>

Now my front site has been crashed. 现在我的前台网站已经崩溃了。 Cant load any products in my site. 无法加载我网站上的任何产品。 Anyone have thoughts what I am missing ? 任何人都有想法我错过了什么?

This is a because you are looping. 这是因为你正在循环。

Every time you call $ourProduct->addImageToMediaGallery($filePath, array('image', 'small_image', 'thumbnail'), true, false); 每次调用$ourProduct->addImageToMediaGallery($filePath, array('image', 'small_image', 'thumbnail'), true, false); the image, small_image & thumbnail are set to the new image that is added. 图像,small_image和缩略图设置为添加的新图像。

If you look at the comment on the function you are calling you will see this written out. 如果你查看你正在调用的函数的注释,你会看到这个注释。

/**
 * 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)

So, if you want the first image to be the one used for the media attributes, you can add a check right before the call to addImageToMediaGallery . 因此,如果您希望第一个图像是用于媒体属性的图像,则可以在调用addImageToMediaGallery之前添加一个检查。 Then you either pass in null or the attribute(s) you wish to set. 然后传入null或要设置的属性。

The exact method you will need to use will depend on the setup of you csv file. 您需要使用的确切方法取决于您的csv文件的设置。 Are the products in order, and other similar questions. 产品是否有序,以及其他类似问题。 Then you can figure out the exact check to use. 然后你可以弄清楚要使用的确切检查。

Hi the best and the fastest way to import images for products is Magmi plugin. 您最好和最快的方式导入产品的图像是Magmi插件。 You have to place all your images under /media/import folder and simply in your csv create a columns named as sku small_image base_image and thumbnail 你必须将所有图像放在/ media / import文件夹下,只需在你的csv中创建一个名为sku small_image base_image和thumbnail的列

And for the images column just enter the image path as /imagename.jpg 对于images列,只需输入图像路径为/imagename.jpg即可

Make sure to enable the image attribute processor plugin enable in magmi. 确保在magmi中启用图像属性处理器插件启用。 It will do the import in seconds . 它将在几秒钟内完成导入。 You can also import product images from urls also through magmi http://wiki.magmi.org/index.php?title=Image_attributes_processor 您还可以通过magmi http://wiki.magmi.org/index.php?title=Image_attributes_processor从网址导入产品图片

Hope this will help Thanks 希望这会有所帮助谢谢

You add the additional images pro grammatically using the following script. 您可以使用以下脚本以编程方式添加其他图像。

http://www.pearlbells.co.uk/how-to-add-main-image-and-additional-image-to-the-products-pro-grammatically-magento/ 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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM