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

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

    $productId = 1;
    //load the product
    $product = Mage::getModel('catalog/product')->load($productId);
    //get all images
    $mediaGallery = $product->getMediaGallery();
    //if there are images
    if (isset($mediaGallery['images'])){
        //loop through the images
        foreach ($mediaGallery['images'] as $image){
            //set the first image as the base image
            Mage::getSingleton('catalog/product_action')->updateAttributes(array($product->getId()), array('image'=>$image['file']), 0);
            //stop
            break;
        }
    }
    

提交回复
热议问题