1
votes

Having an issue with displaying multi-select attribute options: the following code used in catalog/product/list.phtml works perfectly to display the selected attributes - but only if MORE THAN ONE option is selected - so if only one option from the multi select attribute is selected it doesn't display anything?

<?php
$targetValues = $_product->getAttributeText('ni_featured_logo_multi');
foreach($targetValues as $_target) :?>
<div class="featuredlogolist">
<span class="helper"></span>
<img src="<?php echo $this->getSkinUrl() ?>FEATURED_LOGOS/<?php echo $_target ?>.png" class="featuredlogo"></img>
</div>
<?php endforeach;
?>

This is true for for the product page as well ( code used in catalog/product/view.phtml )

 <?php
   $multiSelectArray = $this->getProduct ()->getAttributeText('ni_featured_logo_multi');
   $lastItem = end ($multiSelectArray);
   foreach ($multiSelectArray as $multiSelectItem) :?>
   <img src="<?php echo $this->getSkinUrl() ?>FEATURED_LOGOS/<?php echo $multiSelectItem ?>.png" class="featuredlogo"></img>
   <?php endforeach;
 ?>

any ideas on how to adjust call in order to have the multi select attribute display when only 1 option is selected ? thanks!

2

2 Answers

1
votes

The problem is getAttributeText() actually returns an array only if there is more than one option, otherwise it just returns the single option as a string literal. I think the method declaration is actually wrong here, but I can confirm that this is the behavior from experience.

You should add a simple check like this:

if ($targetValues = $_product->getAttributeText('ni_featured_logo_multi')) {
    if (is_string($targetValues)) {
        $targetValues = array($targetValues);
    }
    foreach ($targetValues as $_target) ...
}
1
votes

wanted to post the working code - with edit from fantasticrice: multi select in catalog/product/list.phtml: ( this is getting an image name from skin folder )

 <?php if ($targetValues = $_product->getAttributeText('your_attribute_code')) {
    if (is_string($targetValues)) {
        $targetValues = array($targetValues);
    }
        foreach($targetValues as $_target) :?>
         <div class="featuredlogo">
         <img src="<?php echo $this->getSkinUrl() ?>FEATURED_LOGOS/<?php echo $_target ?>.png" class="featuredlogo"></img>
         </div>
        <?php endforeach;
                 }
    ?>

and here in catalog/product/view.phtml:

  <?php
    if ($multiSelectArray = $this->getProduct ()->getAttributeText('your_attribute_code')) {
    if (is_string($multiSelectArray)) {
        $multiSelectArray = array($multiSelectArray);
    }
    foreach ($multiSelectArray as $multiSelectItem) :?>
   <img src="<?php echo $this->getSkinUrl() ?>FEATURED_LOGOS/<?php echo $multiSelectItem ?>.png" class="featuredlogo"></img>
   <?php endforeach;

                    }
    ?>

thanks fantasticrice!