0

I am using the code below to loop through all simple products of a configurable within Magento. The code shows specific colour data from each simple product.

However if there is two simple products in different sizes but both have the same colour it will echo the information about that colour twice I only need it to show it once.

<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>
4

2 回答 2

0

Change this:

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

to this:

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

and see if this works.

于 2013-07-25T14:04:26.913 回答
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
于 2013-07-25T14:08:34.267 回答