0

I have this code. It works great, get's the values.

<?php if($_product->isConfigurable()): ?>
  <?php $attributes = $_product->getTypeInstance(true)->getConfigurableAttributes($_product) ?>
  <?php if(count($attributes)): ?>
    <ul>
    <?php foreach($attributes as $att): ?>
      <?php $pAtt=$att->getProductAttribute();
        //get the child products
        $allProducts = $_product->getTypeInstance(true)->getUsedProducts(null, $_product);
        $frontValues =array() ?>
      <li><?php echo $pAtt->getFrontendLabel() ?>
       <ul>
       <?php foreach($allProducts as $p): ?>
         <?php if(!$p->isSaleable()) continue; ?>
         <?php $out=$p->getAttributeText($pAtt->getName()); ?>
         <?php $frontValues[$out]=$out; ?>
       <?php endforeach ?>
        <li><?php echo implode('</li><li>', $frontValues) ?></li>
       </ul>
      </li>
    <?php endforeach ?>
    </ul>
  <?php endif ?>
<?php endif ?>

But I don't want to see the Size option, just available colors and add ID to every <li> according to the color. For example:

The color is 251 in magento, so I want to add ID with a-Code (a-251) so I can target it with css easily.

Can you help me to modify a bit this code? Because I'm new, trying to modify it and getting errors.

Thanks,

UPDATE:

The code I have now:

                  <?php if($_product->isConfigurable()): ?>
                    <?php $attributes = $_product->getTypeInstance(true)->getConfigurableAttributes($_product) ?>
                    <?php if(count($attributes)): ?>
                      <div>
                      <?php foreach($attributes as $att): ?>
                        <?php $pAtt=$att->getProductAttribute();
                          if ($pAtt->getFrontendLabel() != 'Color') continue;
                          //get the child products
                          $allProducts = $_product->getTypeInstance(true)->getUsedProducts(null, $_product);
                          $frontValues =array() ?>
                        <div style="text-align:center;"><!--<?php echo $pAtt->getFrontendLabel() ?>-->
                         <ul>
                         <?php foreach($allProducts as $p): ?>
                           <?php if(!$p->isSaleable()) continue; ?>
                           <?php $out=$p->getAttributeText($pAtt->getName()); ?>
                           <?php $frontValues[$out]=$out; ?>
                           <?php $colors=$p->getData(strtolower($pAtt->getName()))?>
                         <?php endforeach ?>
                          <li><a id="a-<?php echo $colors ?>"><?php echo implode('</li><li>', $frontValues) ?></a></li>
                         </ul>
                        </div>
                      <?php endforeach ?>
                      </div>
                    <?php endif ?>
                  <?php endif ?>

I need to add the id in to the array, so it would add correct values (id's) to each color, can you help?

1

Add after $pAtt=$att->getProductAttribute(); line if ($pAtt->getFrontendLabel() != 'Color') continue;

Actually it's not the best solution, but you can start with it and then optimize the code.

  • One more thing, how do I add id to every item? I have this in Layered, but this doesn't work here: `id="a-<?php echo $_item->getValueString() ?>' – Artur Rain Oct 20 '14 at 11:00
  • Pls try $pAtt->getId() – Amasty Oct 20 '14 at 12:21
  • This added a numeric vallue, but it doesn't match with the one I have in backend. For example: value for Black is 160, an getId, gets 92, any ideas? This is what i Mean: <option value="160" selected="selected">Negro</option> I want the final render to be <a id="a-160">Negro</a> – Artur Rain Oct 20 '14 at 12:24
  • may be $p->getData(strtolower($pAtt->getName())) helps? – Amasty Oct 20 '14 at 12:35
  • One small issue, how do I add it to array? Because now every color get's only the first value. Tryed to add: <?php $colors=$p->getData(strtolower($pAtt->getName()))?> to foreach, and thant echo colors, but with no luck, ideas? – Artur Rain Oct 20 '14 at 13:21

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.