title: Magento set first image as default
body: <p>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?</p>
  
  <pre><code>public function insertProduct($sku,$productDataArray)
  {
      if(isset($productDataArray[$sku]) &amp;&amp; !empty($productDataArray[$sku]))
      {
          $_product = Mage::getModel('catalog/product')-&gt;loadByAttribute('sku',$sku);
          if(!($_product))
          {
              $filesArray = $this-&gt;getGallaryFiles($sku); //download images
  
              $newProduct = Mage::getModel('catalog/product');
              $newProduct-&gt;setData($productDataArray[$sku]);
  
              if(isset($filesArray) &amp;&amp; !empty($filesArray))
              {
                  $newProduct-&gt;setMediaGallery(array('images'=&gt;array (), 'values'=&gt;array ()));
                  $imageDir = Mage::getBaseDir('media').DS.'gallary_import'.DS.$sku.DS;
                  $firstImage = true;
                  foreach ($filesArray as $key =&gt; $file) {
                      if($firstImage)
                          $newProduct-&gt;addImageToMediaGallery($imageDir.$file, array('image','small_image','thumbnail'), true, false);
                      else
                          $newProduct-&gt;addImageToMediaGallery($imageDir.$file, array('image'), true, false);
                  }
                  rmdir($imageDir);
              }
              $newProduct-&gt;save();
  
              unset($_product);
              unset($newProduct);
              $this-&gt;_counts++;
          }else{
  
              /*$newProduct = Mage::getModel('catalog/product')-&gt;load($_product-&gt;getId());
              $newProduct-&gt;setData($productDataArray[$sku]);
              $newProduct-&gt;save();
  
              unset($newProduct);
              $this-&gt;_counts++;*/
              unset($_product);
          }
      }
  }
  </code></pre>