Sjors
Sjors

Reputation: 377

Magento set first image as default

I hope you can help me with this issue. I am using an import script for Magento 1.9.2.2 to download and set multiple product images. Unfortunately the last image is set as default and not the first one. Please see following code, what should be changed to set the first product image as default?

public function insertProduct($sku,$productDataArray)
{
    if(isset($productDataArray[$sku]) && !empty($productDataArray[$sku]))
    {
        $_product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
        if(!($_product))
        {
            $filesArray = $this->getGallaryFiles($sku); //download images

            $newProduct = Mage::getModel('catalog/product');
            $newProduct->setData($productDataArray[$sku]);

            if(isset($filesArray) && !empty($filesArray))
            {
                $newProduct->setMediaGallery(array('images'=>array (), 'values'=>array ()));
                $imageDir = Mage::getBaseDir('media').DS.'gallary_import'.DS.$sku.DS;
                $firstImage = true;
                foreach ($filesArray as $key => $file) {
                    if($firstImage)
                        $newProduct->addImageToMediaGallery($imageDir.$file, array('image','small_image','thumbnail'), true, false);
                    else
                        $newProduct->addImageToMediaGallery($imageDir.$file, array('image'), true, false);
                }
                rmdir($imageDir);
            }
            $newProduct->save();

            unset($_product);
            unset($newProduct);
            $this->_counts++;
        }else{

            /*$newProduct = Mage::getModel('catalog/product')->load($_product->getId());
            $newProduct->setData($productDataArray[$sku]);
            $newProduct->save();

            unset($newProduct);
            $this->_counts++;*/
            unset($_product);
        }
    }
}

Upvotes: 0

Views: 1757

Answers (2)

Sjors
Sjors

Reputation: 377

Thanks Steve! With your help it's working now. I only had to put the lines between {} and deleted 'image' in the last line (otherwise the last image was still the base image).

if($firstImage) { $newProduct->addImageToMediaGallery($imageDir.$file, array('image','small_image','thumbnail'), true, false); $firstImage = false; // added this } else $newProduct->addImageToMediaGallery($imageDir.$file, array(''), true, false);

Upvotes: 1

Steve
Steve

Reputation: 818

The $firstImage = true; doesn't seem to get reset once it has been through once so it still thinks everything is the first image, right until the last one, which remains the "first" IF I have read that correctly...(added firstImage = false; in foreach(); loop).

  $firstImage = true;
  foreach ($filesArray as $key => $file) {  // etc...

Added $firstImage = false;

       $firstImage = true;
       foreach ($filesArray as $key => $file) {
               if($firstImage)
                   $newProduct->addImageToMediaGallery($imageDir.$file, array('image','small_image','thumbnail'), true, false);
                   $firstImage = false;  // added this
               else
                   $newProduct->addImageToMediaGallery($imageDir.$file, array('image'), true, false);
               }
       }

For the sake of tidiness I have added the other suggestions for reference and deleted the comments.

$filesArray = $this->getGallaryFiles($sku); //download images could this be a problem instead of getGalleryFiles with an e? Perhaps the order you get them back out in can be changed. ShaileshTapa's answer says he has "set the last image as the base..." so maybe something in here: Magento: How to programatically set the base image to the first image in the list

These articles might help you get a specific image: How to get a product's image in Magento? and Magento get product images in shoppping cart

http://virginiabeachwebdevelopment.com/web-development-blog/programmatically-importing-products-magento See: Assign the default product attribute set.

This resource seems to have a huge collection of tweak code which might help gist.github.com/arosenhagen/2397824

Upvotes: 0

Related Questions