Magento: How to programatically set the base image to the first image in the list

后端 未结 3 1746
余生分开走
余生分开走 2021-02-06 09:34

The problem

Current Situation

I\'m having a problem with a third-party ioncube loader encoded feedloader plugin which is no longer supported by the original

3条回答
  •  礼貌的吻别
    2021-02-06 10:11

    In my problem i have set the images for the enabled grouped product. I have set the last image as the base,small and thumbnail image.

    $collection = Mage::getModel('catalog/product')
                      ->getCollection()
                      ->addAttributeToFilter("type_id",array("eq","grouped"))
                      ->addAttributeToFilter("status",array("eq","1"));
    foreach($collection as $tmpProduct) {
      $sku = $tmpProduct->getSku();
      $product = Mage::getModel('catalog/product')
                     ->loadByAttribute("sku",$sku);
      $_images = Mage::getModel('catalog/product')
                     ->load($product->getId())
                     ->getMediaGalleryImages();
      $checkImage =  Mage::getBaseDir('media').DS.'catalog'.DS.'product'.DS.$product->getImage();
    
      if( $product->getImage()!=''  && is_file($checkImage)) {
        continue; // do nothing
      } else {
        if($_images) {
          $baseImage = '';
          foreach($_images as $_image) {
            $baseImage = $_image->getFile();
          }
          $groupedImagePath = Mage::getBaseDir('media').DS.'catalog'.DS.'product'.DS.$baseImage;
          $product->setMediaGallery(
                      array('images'=>array (),
                            'values'=>array ()));
          $product->addImageToMediaGallery(
                      $groupedImagePath,
                      array('image', 'thumbnail', 'small_image'),
                      false,
                      false);   
          Mage::getModel('catalog/product_attribute_media_api')
              ->remove($product->getId(),$baseImage);
          $product->save();  
          echo "
    $sku has selected image
    "; } } }

提交回复
热议问题