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

后端 未结 3 1792
余生分开走
余生分开走 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:06

    This is the solution I eventually used in 'shell/fix_images.php':

    addAttributeToFilter('is_imported', 1); // attribute added by importer
            $c=0;
            foreach($products as $p) {
                $pid = $p->getId();
                $product = Mage::getModel('catalog/product')->load($pid);
                $mediaGallery = $product->getMediaGallery();
                if (isset($mediaGallery['images'])){
                    foreach ($mediaGallery['images'] as $image){
                        Mage::getSingleton('catalog/product_action')
                        ->updateAttributes(array($pid), array('image'=>$image['file']), 0);
                        $c++;
                        break;
                    }
                }
            }
            echo($c . " product(s) updated.");
        }
    
    }
    
    $shell = new Mage_Shell_Updater();
    $shell->run();
    

    If anyone should use this, be sure to remove the 'addAttributeToFilter' from your own Mage method chain.. If you are going to run this as a standalone script (without disabling the realtime indexing first), add this code at the beginning of run():

        $pCollection = Mage::getSingleton('index/indexer')->getProcessesCollection(); 
        foreach ($pCollection as $process) {
            $process->setMode(Mage_Index_Model_Process::MODE_MANUAL)->save();
        }
    

    at the end of run():

        foreach ($pCollection as $process) {
            $process->setMode(Mage_Index_Model_Process::MODE_REAL_TIME)->save();
        }
    

    Also, the abstract.php is from Mage_Shell, usually located in the /shell/ directory in the root of your Magento installation.

提交回复
热议问题