2

I have made a multiselect attribute called "Special"
For every value selection I have to show an image on the product page
I wrote the code below but it works only if I choose only one selection. When I choose more selections I have no image on fronted.
Any suggestions?

<?php
$_product = $this->getProduct();
$specialName = $_product->getAttributeText('special');
$specialId = $_product->getSpecial();
?> 
<?php if ($specialId) : ?>

<img src="/media/catalog/special/<?php echo str_replace(' ', '_',$specialId); ?>.jpg" alt="<?php echo $specialName; ?>" title="<?php echo $specialName; ?>" />

<?php endif; ?>
  • What kind of loop are you using? – fja3omega Mar 5 '18 at 4:01
  • Maybe the problem is that I do not use a loop – Costas Saloubardos Mar 5 '18 at 4:07
  • Can you help me with the loop please? – Costas Saloubardos Mar 5 '18 at 13:57
  • have you tried what Pawan answered? – fja3omega Mar 6 '18 at 1:57
1

You need to change your code like:

<?php if($specialId) :?>
    <?php $Ids = explode(',', $specialId);//Multi select attribute will give you comma seprated values
        foreach ($Ids as $id) { ?>
         <img src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB); ?>media/catalog/special/<?php echo str_replace(' ','_', $id);?>.jpg"/>//image name should be 8.jpg/9.jpg (you can also choose label here)
        <?php } ?>
<?php endif ?> 

Explanation: AS your attribute is multi selects first you need to get all values and explode them.it will give you an array. You can iterate a loop and echo image for each attribute value.

If Have any issue feel free to ask!

Note: I have use is for image you can use label as per your original code.

0

I edited the code from above and it worked like a charm

  <?php
$_product = $this->getProduct();
$specialName = $_product->getAttributeText('special');
$specialId = $_product->getSpecial();
//print_r($specialId); 
//echo "Hi";
//print_r($specialName);
//exit;
  ?> 
  <?php if ($specialId) : 
  $specialId = explode(",",$specialId); ?>
<?php for($i=0;$i<count($specialId);$i++){ ?>
<img src="/media/catalog/special/<?php echo str_replace(' ', '_',$specialId[$i]); ?>.jpg" alt="<?php echo $specialName[$i]; ?>" title="<?php echo $specialName[$i]; ?>" />
<?php } ?>
 <?php endif; ?> 

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.