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.

  • 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 Nov 14 '16 at 9:42
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();
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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.