2

i have a problem by getting a custom image in the frontend.

  1. I have a custom media product attribute "image_productlist"
  2. 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!

  1. 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.

  2. 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.

asked Nov 12, 2016 at 12:38
1
  • 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. Commented Nov 14, 2016 at 9:42

2 Answers 2

0

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();
answered Nov 12, 2016 at 14:43
0

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();
answered Nov 14, 2016 at 10:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.