Syspect
Syspect

Reputation: 921

steps to import images with a product

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?

Upvotes: 1

Views: 504

Answers (1)

Mihai Stancu
Mihai Stancu

Reputation: 16107

As per this answer you need to use addImageToMediaGallery.

You need to have an instance of a catalog/product object. That should be easy $product = Mage::getModel('catalog/product'); and then you need to load its data from the DB for example $product->load($id);.

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

Magento stores product files in media/catalog/product/X/Y/XYfilename.ext where X and Y are the first letters of the filename. So Magento creates two levels of subfolders based on the first two letters in the filename. This logic is not present directly in addImageToMediaGallery, it's in Mage_Catalog_Model_Product_Attribute_Backend_Media::addImage().

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

Upvotes: 1

Related Questions