如果值是重复的,请不要回显

时间:2013-07-25 13:56:43

标签: php magento

我使用下面的代码遍历Magento中可配置的所有简单产品。该代码显示了每个简单产品的特定颜色数据。

然而,如果有两种不同尺寸的简单产品,但它们都具有相同的颜色,它会将有关该颜色的信息回显两次,我只需要它显示一次。

<div class="colour-swatch">
    <h1>Other Colours Available</h1>
    <?php
     $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
    $col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions(); ?>
    <div class="relative">
      <?php
        foreach($col as $simple_product){ ?>
            <div class="container-swatch">
              <img width="35" height="35" src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'catalog/product' . $simple_product->getSwatch() ?>">
                  <div class="content">
                    <div class="inside-swatch-name"><?php echo $simple_product->getAttributeText('real_colour'); ?></div>
                    <img src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'catalog/product' . $simple_product->getLargeSwatch() ?>">
                  </div>
            </div>
        <?php }  ?>
        <?php if ($synb == 'Yes') { ?>
        <div class="swatch-order">
          <a href="#" class="topopup2 order-samples-button">ORDER SAMPLES</a>
        </div>
        <?php } else {
          //do nothing 
        } ?>
    </div>

</div>

2 个答案:

答案 0 :(得分:0)

改变这个:

$col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions(); ?>

到此:

$col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions()->addGroupByAttribute('real_colour'); ?>

看看是否有效。

答案 1 :(得分:0)

$colors = array();
foreach($col as $simple_product){ 
$color = $simple_product->getAttributeText('real_colour');
if(!in_array($color, $colors)){
$colors[] = $color; ?>
//do the rest from your foreach