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?

Syspect
  • 921
  • 7
  • 22
  • 50
  • Check out this [answer](http://stackoverflow.com/questions/8456954/magento-programmatically-add-product-image). – Mihai Stancu Oct 30 '13 at 11:16
  • @MihaiStancu - Oh, I think I understand. So `$importDir = Mage::getBaseDir('media') . DS . 'import/';` is the directory where the images should be moved to (where Magento looks for images). And `addImageToGallery()` is the function that actually places the images there. Is that correct? Then arises the problem, that I cannot call `addImageToGallery()` on my `$product` variable, because it is of `SimpleXMLElement` type and I get the `call on non-object` error... :\ – Syspect Oct 30 '13 at 12:08
  • You need to create an new `$product` object. In Magento that should be easy `$new_product = Mage::getModel('catalog/product');` and then you need to load its data from the DB for example `$new_product->load($id); /* not sku */`. `addImageToGallery` should work fine on a `catalog/product` object. – Mihai Stancu Oct 30 '13 at 12:11
  • @MihaiStancu - but with `Mage::getBaseDir('media') . DS . 'import/';` I get the same directory as my `$importImagesDirectory`. Don't I need the directory where the images should be moved TO? Or the `addImageToMediaGallery()` finds the folder and does that by itself. Because I looked through its code and it doesn't seem to do that... – Syspect Oct 30 '13 at 12:47
  • Magento stores stuff 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` but afaik it is in the lower levels of the implementation. – Mihai Stancu Oct 30 '13 at 12:56

1 Answers1

1

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

Community
  • 1
  • 1
Mihai Stancu
  • 15,848
  • 2
  • 33
  • 51