1
votes

Problem

I'm working on an integration with Magento which first removes all images from a product and then add new ones. The problem is that when I add them the 'thumbnail', 'small_image' and 'image' only gets set on the default store and not the individual stores.

Code

public function setProductImages($entityId, $images, $websiteIds, $removeFirst = true){
$product = Mage::getModel('catalog/product')->load($entityId);
    if($removeFirst){
        //First I need to reset image selection before I can remove all images
        $values = array(
            'image' => 'no_selection',
            'small_image' => 'no_selection',
            'thumbnail' => 'no_selection'
        );

        //Go through all sites and stores and set the image selection values
        foreach($websiteIds as $websiteId) {
            $website = Mage::getModel('core/website')->load($websiteId);
            foreach ($website->getStoreIds() as $storeId) {
                Mage::getSingleton('catalog/product_action')->updateAttributes(array($product->getId()), $values, $storeId);
            }
        }

        //Set the selection values on admin store
        Mage::getSingleton('catalog/product_action')->updateAttributes(array($product->getId()), $values, 0);

        //Remove all images on product
        $mediaGalleryAttribute = Mage::getModel('catalog/resource_eav_attribute')->loadByCode($product->getEntityTypeId(), 'media_gallery');
        $gallery = $product->getMediaGalleryImages();
        foreach ($gallery as $galleryImage) {
            $imageFile = $galleryImage->getFile();
            $mediaGalleryAttribute->getBackend()->removeImage($product, $imageFile);
        }

        $product->save();
    }
    foreach($images as $image) {        
        file_put_contents($path . DS . $image->filename, $image->content);
        $type = in_array($image->name, array('image', 'small_image', 'thumbnail')) ? $image->name : null;
        $product->addImageToMediaGallery($path . DS . $image->filename, $type, true, false);
    }
    $product->save();
}

Is there some way to tell addImageToMediaGallery to set on an array of stores? Or am I missing something?

1

1 Answers

2
votes

Solved it by replacing

foreach($images as $image) {        
    file_put_contents($path . DS . $image->filename, $image->content);
    $type = in_array($image->name, array('image', 'small_image', 'thumbnail')) ? $image->name : null;
    $product->addImageToMediaGallery($path . DS . $image->filename, $type, true, false);
}
$product->save();

with this:

$imageTypes = array();

//Sets images on default store for product
foreach($images as $image){
    file_put_contents($path . DS . $image->filename, $image->content);
    $type = in_array($image->name, array('image', 'small_image', 'thumbnail')) ? $image->name : null;
    $product->addImageToMediaGallery($path . DS . $image->filename, $type, true, false);

    //Store image, small_image and thumbnail for later use
    $imageTypes['image'] = $product->getImage();
    $imageTypes['small_image'] = $product->getSmallImage();
    $imageTypes['thumbnail'] = $product->getThumbnail();
}
$product->save();

//Go through all stores and set image, small_image and thumbnail
foreach($websiteIds as $websiteId) {
    $website = Mage::getModel('core/website')->load($websiteId);
    foreach($website->getStoreIds() as $storeId) {
        Mage::app()->setCurrentStore($storeId);
        Mage::getSingleton('catalog/product_action')->updateAttributes(array($product->getId()), $imageTypes, $storeId);
    }
}
$product->save();