Magento multi select 属性 - 如果只有 1 个选项 select 则不会显示在前端

Magento multi select attribute - wont show on front-end if only 1 option selected for product

显示多 select 属性选项时出现问题: catalog/product/list.phtml 中使用的以下代码可以完美地显示 selected 属性 - 但前提是 selected 有多个选项 - 因此,如果 multi select 属性中只有一个选项被 selected,它不会显示任何内容吗?

<?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;
?>

产品页面也是如此(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;
 ?>

关于如何调整调用以便在只有 1 个选项 selected 时显示多个 select 属性有什么想法吗?谢谢!

问题是 getAttributeText() 实际上 returns 只有当有多个选项时才是一个数组,否则它只是 returns 作为字符串文字的单个选项。我认为这里的方法声明实际上是错误的,但我可以确认这是经验行为。

您应该像这样添加一个简单的检查:

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

想要 post 工作代码 - 来自 fantasticrice 的编辑: multi select in catalog/product/list.phtml:(这是从皮肤文件夹获取图像名称)

 <?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;
                 }
    ?>

在 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;

                    }
    ?>

感谢fantasticrice!