Magento:如何以编程方式将基本图像设置为列表中的第一个图像 [英] Magento: How to programatically set the base image to the first image in the list

查看:98
本文介绍了Magento:如何以编程方式将基本图像设置为列表中的第一个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到第三方离子筛装载机问题原始作者不再支持编码的feedloader插件(因为他们进行了测试)而不是花两周时间从头开始编写它,我决定在导入运行后修复它所遇到的问题。

I'm having a problem with a third-party ioncube loader encoded feedloader plugin which is no longer supported by the original author (since they went tits-up) and instead of putting in two weeks to completely write it from scratch, I've decided to fix the problems it has after the import has run.

唯一的问题是:我想用Mage库中的函数来编写这个,而不是依赖一些自定义查询可能会在Magento的下一次更新中或每当我更改数据库中的内容时中断(我只想更好地了解Magento的核心功能)

The only problem is: I want to write this using functions in the Mage library, not rely on a few custom queries that might break in the next update of Magento or whenever I change something in the database (I just want to get to know Magento's core functions better I guess)

除了设置基本图像(小图像和缩略图图像设置正确)之外,导入几乎所有内容都正确,如屏幕截图所示下面:

The import does nearly everything correct, except from setting the base image (the small and thumbnail image are set correctly), as you can see in the screenshot below:

该图像缺少数据库中的实际记录..(因此很想修复它一个查询,但我不会..我会继续寻找一个优雅的解决方案)

The actual record in the database is missing for that image.. (so tempted to fix it with a query, but I won't.. I'm going to keep looking for an elegant solution)

此外,函数$ product-> getMediaGalleryImages()不会t返回任何图像,因此我无法按照@SKV建议的方式使用以编程方式设置基本图像 ..除非我做错了。

Also, the function $product->getMediaGalleryImages() doesn't return any images, so I can't use the solution as suggested by @SKV over at Set Base Image Programmatically .. unless I am doing something wrong.

推荐答案

$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;
    }
}

这篇关于Magento:如何以编程方式将基本图像设置为列表中的第一个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆