0

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);
        }
    }
}
Sjors
  • 301
  • 4
  • 14
  • http://virginiabeachwebdevelopment.com/web-development-blog/programmatically-importing-products-magento Assign the default product attribute set. // get and set default attribute set $def_attribute_set = Mage::getModel('catalog/product')->getResource()->getEntityType()->getDefaultAttributeSetId(); $product->setAttributeSetId($def_attribute_set); // need to look this up - See more at: http://virginiabeachwebdevelopment.com/web-development-blog/programmatically-importing-products-magento#sthash.wrUs4aOu.dpuf – Steve Dec 27 '15 at 15:14
  • `$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 there. http://stackoverflow.com/questions/19153891/magento-how-to-programatically-set-the-base-image-to-the-first-image-in-the-lis – Steve Dec 27 '15 at 15:16

2 Answers2

1

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);

Sjors
  • 301
  • 4
  • 14
0

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

Community
  • 1
  • 1
Steve
  • 808
  • 1
  • 9
  • 14
  • Thank you, I have added the code like this, but unfortunately nothing has changed. The images are in the same order and the last image is still the default one. `code $newProduct->setMediaGallery(array('images'=>array (), 'values'=>array ())); $imageDir = Mage::getBaseDir('media').DS.'gallary_import'.DS.$sku.DS; array_reverse($filesArray); array_reverse($newProduct); $firstImage = true;` – Sjors Dec 27 '15 at 14:25
  • Sorry about that. It was a bit of a guess. Does `$firstImage = false;` do anything? – Steve Dec 27 '15 at 14:43