以编程方式设置基础图像 [英] Set Base Image Programmatically

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

问题描述

我遇到了无法解决的问题.部分原因是我无法用正确的术语来解释它.我是新手,很抱歉这个笨拙的问题.

I've got this problem that I can't solve. Partly because I can't explain it with the right terms. I'm new to this so sorry for this clumsy question.

您可以在下面看到我的目标概览.

Below you can see an overview of my goal.

我正在尝试在我的 magento 中销售类似的产品,为此我编写了一些代码,一切都运行良好..

I am trying to sell Similar product in my magento for this i wrote some code that every thing is working fine..

但我对图像只有一个问题..

But i have only one problem with images..

即我正在获取当前产品的所有图像但未选择基本图像,我如何以编程方式将第一个图像设置为基本图像.

that is I am getting all images of current product but the base image is not selected, How can i set the very first image as base image Programmatically.

有什么想法吗?

推荐答案

您好,您可以这样做:

$image =$imagePath."image.png";
$product->setMediaGallery(array('images'=>array (), 'values'=>array ()));
if(is_file($image))
{
    $product->addImageToMediaGallery($image, array ('image', 'small_image', 'thumbnail'), false, false);
}

即你需要先设置媒体库,P.S这是一个必要的步骤.

i.e you need to first set the media gallery, P.S This is a necessary step.

然后使用 addImageToMediaGallery 将所有图片添加到图库,其中 'image' ref to 'base_image'

then add all images to gallery using addImageToMediaGallery where 'image' ref to 'base_image'

即在上面的示例中,我们在一次调用中将 image.png 设置为 base_image、small_image 和缩略图.

i.e in above example we are setting image.png to base_image, small_image and thumbnail image in a single call.

希望对你有所帮助.

这篇关于以编程方式设置基础图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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