cancel
Showing results for 
Search instead for 
Did you mean: 

Custom product attribute - display attribute value as image

Custom product attribute - display attribute value as image

Hi all

I have a custom product attribute that contain text names of badges.

So a particular value might be 'free delivery'.

In my custom template, I need to display the badge/badges associated with the badge name, using the name as part of the image name.

Current code so far is below, but I can't get the image to display.

Page code shows:

src="http://www.domain.co.uk/media/wysiwyg/badge/" so i'm missing the image file name.

<?php
/** @var Mage_Catalog_Model_Product $_product */
/** @var Mage_Eav_Model_Entity_Attribute_Set $attributeSet */
$_product = $this->getProduct();
/** @var array $badges_array */
$badges = $_product->getAttributeText('badges');

if (!empty($badges) && !is_array($badges)) {
    $badges = array($badges);
}
?>

<?php if (!empty($badges) && count($badges) > 0) : ?>
    <div class="product-attribute product-attribute--badges">
        <ul class="product-attribute__list">
            <?php foreach ($badges as $badge) : ?>
                <?php $img_src=strtolower(preg_replace('/[^A-Za-z0-9]/', '', $badge)); ?>
                <li class="product-attribute__item">
                    <img
                        class="product-attribute__image"
                        src="<?php echo $this->getUrl('media/wysiwyg/badge/' . $img_src . '.png')?>"                                            
                        alt="<?php echo $this->escapeHtml($badge); ?>"
                        title="<?php echo $this->escapeHtml($badge); ?>"
                    />
                </li>
            <?php endforeach; // $badges as $badge ?>
        </ul>
    </div>
<?php endif; // !empty($badges) && count($badges) > 0 ?>

Can anyone advise please?

Many Thanks

2 REPLIES 2

Re: Custom product attribute - display attribute value as image

Quick update:

Using the following:

 

src="<?php echo $this->getViewFileUrl('wysiwyg/badge/' . $img_src . '.png') ?>"

I get this on the product page:

 

http://www.domain.co.uk/static/version1619617765/frontend/MYVendor/argento-flat-custom/en_GB/wysiwyg...

 

So what is the correct method so that the correct img scr path is created:

http://domain.co.uk/media/wysiwyg/badge/freedelivery.png

 

I need to replace this

getViewFileUrl

Many Thanks

Andy

 

Re: Custom product attribute - display attribute value as image

So I've changed the

getViewFileUrl

to 

getUrl

 

and now I'm getting the correct path and image file but there's  / forwward slash at the end of the image file name!!

http://www.domain.co.uk/pub/media/wysiwyg/badge/freedelivery.png/

What's that about?

Any Ideas?

Andy