Not sure if this is the problem, but I think if you call load
again after calling save
it might work.
So after $product->save();
do this:
$_product = Mage::getModel('catalog/product')->load($product->getId());
$items = $_product->getMediaGalleryImages();
//then your foreach loop here
Here is why I think this.
The getMediaGalleryImages
method looks like this:
public function getMediaGalleryImages()
{
if(!$this->hasData('media_gallery_images') && is_array($this->getMediaGallery('images'))) {
$images = new Varien_Data_Collection();
foreach ($this->getMediaGallery('images') as $image) {
if ($image['disabled']) {
continue;
}
$image['url'] = $this->getMediaConfig()->getMediaUrl($image['file']);
$image['id'] = isset($image['value_id']) ? $image['value_id'] : null;
$image['path'] = $this->getMediaConfig()->getMediaPath($image['file']);
$images->addItem(new Varien_Object($image));
}
$this->setData('media_gallery_images', $images);
}
return $this->getData('media_gallery_images');
}
Look a the first if statement. The images are retrieved again from the db only if inside the product instance _data['media_gallery_images']
is not set or $this->getMediaGallery('images')
is not an array.
Since you already called load
on the product before adding the image _data['media_gallery_images']
is always set, so the code is not executed. You only get the images you previously had and the image you added.
But the image you just added does not have an id because the method that is called to actually assign the image does not return the id: Mage_Catalog_Model_Product_Attribute_Backend_Media::addImage
.
Reloading the product instance again should get you the id.
Small note: I think save
is not really needed, but not sure.