Magento – Show Custom Multi-Select Attribute Values as Images

imagemultiselect-attribute

I am trying to display a custom multiselect attribute as images on the frontend if they have been selected by the store admin.

So far I have been able to get the values "as text" on the frontend but I am not sure how I can go about associating each value with an image…

Here is what I have so far

 <?php
   $multiSelectArray = $this->getProduct ()->getAttributeText('suitable_for');
   $lastItem = end ($multiSelectArray);
   foreach ($multiSelectArray as $multiSelectItem) {
   echo $multiSelectItem;
   if ($multiSelectItem != $lastItem) echo ", ";
     }
 ?>

The above code will show the values of the multiselect but I am stuck on how to associate the values with an image instead of text.

I anyone can assist I'd be very grateful!

Best Answer

Do this:

 <?php
   $multiSelectArray = $this->getProduct ()->getAttributeText('suitable_for');
   $lastItem = end ($multiSelectArray);
   foreach ($multiSelectArray as $multiSelectItem) :?>
   <img src="/media/img/<?php echo $multiSelectItem ?>"></img>
   <?php endforeach;
 ?>

and store your images in /media/img/[item].[ext], the same value as the text stored.