1

It seems like I am the only one who cannot make use of addImageToGallery(), as I found many solutions of importing images to/with a product through this function.

First of all, I cannot use directly $product->addImageToGallery() as I found in all the examples, because I get error to call it on a non-object. Maybe because my $product is a SimpleXMLElement object. Therefore I went to this solution:

$model = Mage::getModel('catalog/product');
$product->$model->addImageToMediaGallery($filePath, array('image', 'small_image', 'thumbnail'), false, false);

I don't know if that is correct and if it is working, because my overall solution is not working. That was first.

Second, for a new product, the image gallery is null, so it has to be set. As I found in many examples, people use $product->setMediaGallery(). That's cool and easy. Too bad that there is NO such function in the API. What is going on around here?!

Third, I am lost and have no idea how to proceed. I also tried the old goodie way: here go down to the end of Page 3, where he talks about Product Image.

That is my overall code, that I have, and that doesn't work:

    $model = Mage::getModel('catalog/product');
    $product->$model->setMediaGallery(array('images'=>array (), 'values'=>array ()));
    $filePath = $importImagesDirectory.$this->_getPresentationAttributeValue($product, '@Art="Bild" and @Rang="1"'); //this gives me full path of the image. It's correct, no worries.
    $product->$model->addImageToMediaGallery($filePath, array('image', 'small_image', 'thumbnail'), false, false);
  • Why this: $product->$model, I can't figure out what is this? – mageUz Oct 29 '13 at 23:06
  • @mageUz - because I cannot make directly $product->Mage::getModel('catalog/product')->addImageToMediaGallery(). And also $product->addImageToMediaGallery() does not work. – Syspect Oct 30 '13 at 8:09
  • Can we see the rest of your code, so we know what $product is – Barry Carlyon Oct 30 '13 at 16:33
2

No offense, but do you understand what

$product->$model->setMediaGallery()

and

$product->Mage::getModel('catalog/product')->addImageToMediaGallery()

means?

I would say $product->Mage::getModel('catalog/product')->addImageToMediaGallery() should produce a parser error, I have no clue what PHP would do with this.

This $product->$model->setMediaGallery() means you are trying to access a public property of $product with the name $model (which should be a string!), so no luck here too.

So what you want is simply $model->addImageToMediaGallery()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.