Set Base Image Programmatically(以编程方式设置基础图像)
问题描述
我遇到了无法解决的问题.部分原因是我无法用正确的术语来解释它.我是新手,很抱歉这个笨拙的问题.
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.
希望对你有所帮助.
这篇关于以编程方式设置基础图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:以编程方式设置基础图像


基础教程推荐
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01