i have a problem by getting a custom image in the frontend.
- I have a custom media product attribute "image_productlist"
- In my product import i fill the mediaGallery like this:
$oTargetProduct->addImageToMediaGallery($aMediaData['overview_image'], ['image_productlist', 'swatch_image'], false, true);
$oTargetProduct->save();
Where $aMediaData['overview_image'] has the MEDIA IMPORT PATH AND THE FILENAME!
After the import i see in my Admin Backend this image in the gallery and the flags 'image_productlist', 'swatch_image' marked at this image.
Now i want to get this image with flag: 'image_productlist' in my frontend block.
For this i do something like this:
// get the variant images
$existingMediaGalleryEntries = $_simpleProduct->getMediaGalleryEntries();
foreach ($existingMediaGalleryEntries as $key => $entry) {
var_dump($entry->getData());
}
I get this list of the images in the gallery but there is no FLAG 'image_productlist'.
What is wrong??
Thanks for help.
-
I think that if you want to retrieve a custom image type, you should retrieve product custom attribute directly. I need to do the same stuff. I keep you updated.Franck Garnier– Franck Garnier2016年11月14日 09:42:01 +00:00Commented Nov 14, 2016 at 9:42
2 Answers 2
Make sure the attribute is setup correctly and that the attribute setting Used in product listing is set to Yes under Storefront Properties. Then you can get a custom product image attribute by:
$imageHelper = $this->helper('Magento\Catalog\Helper\Image');
$attributeImage = $_product->getCustomAttribute('image_productlist');
$attributeImageUrl = $imageHelper->init($_product, 'image_productlist')->setImageFile($attributeImage->getValue())->getUrl();
On way to achieve it :
Update the app/design/frontend/{VendorName}/{ThemeName}/etc/view.xml of your theme :
<image id="product_custom_image" type="image_productlist">
<width>265</width>
<height>265</height>
</image>
You need to update the default configuration file view.xsd as described in this post with a plugin: Extending the complexType named "imageType" with a custom image type
Then use the default image helper function in order to retrieve your image type such as :
/**
* @var \Magento\Catalog\Helper\ImageFactory
*/
protected $helperFactory;
$image = $this->helperFactory->create()->init($product, 'product_custom_image')
->constrainOnly(true)
->keepAspectRatio(true)
->keepTransparency(true)
->keepFrame(false)
->resize(200, 300);
$imageUrl = $image->getUrl();