1

Reposting my question from another forum: I want to add a picture as attribute on product and category page but encountered a problem.

Using Media Image custom attribute type I could not display it on frontend and backend.

After that I use Google and found that the storefront properties in Media Image attribute is bugged and hidden.

Made them visisble, set the image for attribute using aaaand i got the end of image link as text in attribute field in frontend (Image 1)

also i use my code

$attributeName = $_product->getResource()->getAttribute('color_unc');
 if ($attributeName) {
 $labelValue = $attributeName->getFrontend()->getValue($_product);
 $labelName = $attributeName->getFrontend()->getLabel($_product);
 }

echo $labelName; echo ' ';echo $labelValue;

in category phtml file and expectedly received the same text (Image 2)

Maybe anyone know how to fix this and get the image instead of text? I will say right away: i want use this for configurable product which have only one color (i can't use swathes for obvious reasons)

Teja Bhagavan Kollepara
3,8275 gold badges33 silver badges69 bronze badges
asked Jan 12, 2018 at 14:56

1 Answer 1

0

Try this:

$productImageAttr = $product->getCustomAttribute( 'color_unc' );
$productImage = $this->helper('Magento\Catalog\Helper\Image')
->init($product, 'color_unc')
->setImageFile($productImageAttr->getValue());

Then include the image wherever you like

<img src="<?php echo $productImage->getUrl() ?>" alt="<?php echo $block->escapeHtml($product->getTitle()) ?>" />
answered Jan 25, 2018 at 11:34

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.