2

我正在尝试以编程方式将基本图像、小图像和缩略图图像添加到我的所有产品中。图像可以正常导入/media/catalog/product并按预期放置,但是当我在 Magento 后端查看产品图像时,仅选择了“基本图像”。

我正在遍历目录中的每个产品并运行:

$mediaArray = array('thumbnail', 'small_image', 'image');   
$file = Mage::getBaseDir('media') . DS . 'import' .  trim($imageName);
try {
    $product->addImageToMediaGallery($file, $mediaArray, false, false);
    $product->save();
} catch (Exception $e) {
    echo $e->getMessage();
}

我所有的图像名称都以斜线开头,并且图像本身被上传到/media/import. 我试图模仿 Mage 产品进口商会做的事情。

任何关于为什么没有设置小和缩略图标志的见解将不胜感激!

4

3 回答 3

3

这是我自己使用的导入代码,它有效:

        // Add three image sizes to media gallery
        $mediaArray = array(
            'thumbnail'   => $oscProduct['products_image'],
            'small_image' => $oscProduct['products_mediumimage'],
            'image'       => $oscProduct['products_largeimage'],
        );

        // Remove unset images, add image to gallery if exists
        $importDir = Mage::getBaseDir('media') . DS . 'import/';

        foreach ( $mediaArray as $imageType => $fileName ) {
            $filePath = $importDir . $fileName;
            if ( file_exists($filePath) ) {
                try {
                    $product->addImageToMediaGallery($filePath, $imageType, false);
                } catch (Exception $e) {
                    echo $e->getMessage();
                }
            } else {
                echo "Product does not have an image or the path is incorrect. Path was: {$filePath}<br/>";
            }
        }

Note$imageType不是数组,因为您的对应对象是。

编辑:您只想要一个图像,并将其设置为每种类型。使用 保存产品$product->save()后,假设您已经设置了image类型,请尝试以下操作:

$product->setThumbnail($product->getImage());
$product->setSmallImage($product->getImage());
$product->save();
于 2012-05-10T18:30:22.583 回答
0

我在复制产品,也想复制产品图像。

我为此工作了几个小时,但无法让 array('thumbnail', 'small_image', 'image') 实际设置这些值。它将图像添加到库中,但不设置上述类型。我还尝试输入为字符串,例如只是“图像”而不是数组,这也不起作用。最后,这对我有用:

//grab the pics from original product
$mediaApi = Mage::getModel("catalog/product_attribute_media_api");
$items = $mediaApi->items($product->getId());

//duplicate the product
$product = $product->duplicate();
$product->setStatus(1);

//also move the pics over
$product->setMediaGallery (array('images'=>array (), 'values'=>array ()));
$product->setImage($items[0]['file']);
$product->setThumbnail($items[0]['file']);
$product->setSmallImage($items[0]['file']);
于 2013-03-04T03:42:50.047 回答
0

除非您设置了store id.

我这样做了:

Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);

我只有一家店。

于 2013-11-07T18:08:48.163 回答