1

What are the steps to import a product with images. I mean - the images, precisely. So I have this code now:

protected function _getImages($product)
{
            $importImagesDirectory = Mage::getBaseDir()."/".Mage::getStoreConfig('xmlimport/product/import_images_dir');
            if(file_exists($importImagesDirectory))
            {
                $addImage['sku'] = (string)$product['PK_Produkt'];
                $addImage['_media_image'] = $importImagesDirectory . $this->_getPresentationAttributeValue($product, '@Art="Bild" and @Rang="1"');
                $addImage['_media_attribute_id'] = Mage::getSingleton('catalog/product')->getResource()->getAttribute('media_gallery')->getAttributeId();
                $addImage['_media_is_disabled'] = 0;
                $addImage['_media_position'] = 1;
// the following 4 lines are just acquiring string values - the name of the picture
                $addImage['_media_lable'] = $this->_getPresentationAttributeValue($product, '@Art="Bild" and @Rang="1"');
                $addImage['image'] = $this->_getPresentationAttributeValue($product, '@Art="Bild" and @Rang="1"');;
                $addImage['small_image'] = $this->_getPresentationAttributeValue($product, '@Art="Bild" and @Rang="1"');;
                $addImage['thumbnail'] = $this->_getPresentationAttributeValue($product, '@Art="Bild" and @Rang="1"');;

            }
            else Mage::log('Error! Image with name: ' . $importImagesDirectory . ' does not exist! ', null, 'error_image.log', true);

            return $addImage;
}

So this array is being merged with the array containing the product information, that is being imported. Now, I was said that the function should also move the images from my media/import/ folder to somewhere catalog/product/ folder, because Magento looks up for images in that folder. Anf if I just import the information, but don't move the images - then no effect (which actually happens right now).

So I am asking, how does that work, is there a function from the API to make this "movement", is that the correct procedure?

4

1 回答 1

1

根据这个答案,您需要使用addImageToMediaGallery.

您需要有一个catalog/product对象的实例。这应该很容易$product = Mage::getModel('catalog/product');,然后您需要从数据库加载它的数据,例如$product->load($id);

$product->addImageToMediaGallery($filePath, $imageType, false);

Magento 将产品文件存储在文件名的第一个字母的media/catalog/product/X/Y/XYfilename.ext位置X和位置。Y因此 Magento 根据文件名中的前两个字母创建了两个级别的子文件夹。此逻辑不直接存在于 中addImageToMediaGallery,它存在于 中Mage_Catalog_Model_Product_Attribute_Backend_Media::addImage()

magento_root:app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php

于 2013-10-30T12:59:08.543 回答