magento 2 如何将基本图像或缩略图图像设置为产品
magento 2 how to set base or thumbnail image to product
我的问题是在 yopu 将图像添加到 magento 2 中的产品后,如何将其设置为产品的默认图像(基本图像)。
我找到了如何向产品添加图像,但找到了将其设置为产品基础图像的方法。
加载产品后:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load(PRODUCT ID);
您可以通过以下代码获取商品的所有图片:
$images = $product->getMediaGalleryImages();
这将 return 一个包含添加到产品的所有图像的集合。
您可以选择任何图像或像这样拍摄第一张:
$image = $images->getFirstItem();
之后你用
获得图像 url
$imageUrl = $image->getFile();
设置为产品是这样的:
$product->setImage($image);
$product->setSmallImage($image);
$product->setThumbnail($image);
$product->setSwatchImage($image);
并像这样保存产品:
$product->save();
得到你想要的产品id后,为每个产品写下如下代码
$product = $this->productRepository->getById($data['product_id']);
$mediaGalleryEntriesTemp = [];
$i = 1;
foreach ($product->getMediaGalleryEntries() as $item) {
if ($i === 1) {
$item->setTypes(['image', 'small_image', 'thumbnail']);
}
$mediaGalleryEntriesTemp[] = $item;
$i++;
}
$product->setMediaGalleryEntries($mediaGalleryEntriesTemp);
$this->productRepository->save($product);
Notice: 'swatch_image' role only available in simple product or
configurable child product, not configurable parent product.
经过我的测试,解决方案在 Magento 2.2.3 中有效,其他版本也应该有效。
我的 Magento 2.3.3 解决方案运行良好
对我来说,我的情况有点不同,我需要现有产品的基本图像和小图像,并且我有图像名称:)
保存图像后,您可以使用此代码制作基本图像。
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($product->getId());
$images = $product->getMediaGalleryEntries();
$mediaGallery = $product->getMediaGallery();
//if there are images
if (isset($mediaGallery['images'])){
//loop through the images
foreach ($mediaGallery['images'] as $image){
// make the base image by name
if(strpos($image['file'], 'PRODUCT_NAME') !== false){
$objectManager->get('Magento\Catalog\Model\Product\Action')->updateAttributes(array($product->getId()), array('image'=>$image['file'], 'small_image'=>$image['file']), 0);
//stop
break;
} else{
echo "can't find";
}
}
}
更改 PRODUCT_NAME 或者如果不需要它删除 if-else 条件
希望对其他人有所帮助
我的问题是在 yopu 将图像添加到 magento 2 中的产品后,如何将其设置为产品的默认图像(基本图像)。 我找到了如何向产品添加图像,但找到了将其设置为产品基础图像的方法。
加载产品后:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load(PRODUCT ID);
您可以通过以下代码获取商品的所有图片:
$images = $product->getMediaGalleryImages();
这将 return 一个包含添加到产品的所有图像的集合。 您可以选择任何图像或像这样拍摄第一张:
$image = $images->getFirstItem();
之后你用
获得图像 url$imageUrl = $image->getFile();
设置为产品是这样的:
$product->setImage($image);
$product->setSmallImage($image);
$product->setThumbnail($image);
$product->setSwatchImage($image);
并像这样保存产品:
$product->save();
得到你想要的产品id后,为每个产品写下如下代码
$product = $this->productRepository->getById($data['product_id']);
$mediaGalleryEntriesTemp = [];
$i = 1;
foreach ($product->getMediaGalleryEntries() as $item) {
if ($i === 1) {
$item->setTypes(['image', 'small_image', 'thumbnail']);
}
$mediaGalleryEntriesTemp[] = $item;
$i++;
}
$product->setMediaGalleryEntries($mediaGalleryEntriesTemp);
$this->productRepository->save($product);
Notice: 'swatch_image' role only available in simple product or configurable child product, not configurable parent product.
经过我的测试,解决方案在 Magento 2.2.3 中有效,其他版本也应该有效。
我的 Magento 2.3.3 解决方案运行良好
对我来说,我的情况有点不同,我需要现有产品的基本图像和小图像,并且我有图像名称:)
保存图像后,您可以使用此代码制作基本图像。
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($product->getId());
$images = $product->getMediaGalleryEntries();
$mediaGallery = $product->getMediaGallery();
//if there are images
if (isset($mediaGallery['images'])){
//loop through the images
foreach ($mediaGallery['images'] as $image){
// make the base image by name
if(strpos($image['file'], 'PRODUCT_NAME') !== false){
$objectManager->get('Magento\Catalog\Model\Product\Action')->updateAttributes(array($product->getId()), array('image'=>$image['file'], 'small_image'=>$image['file']), 0);
//stop
break;
} else{
echo "can't find";
}
}
}
更改 PRODUCT_NAME 或者如果不需要它删除 if-else 条件
希望对其他人有所帮助