2

I have this code, witch retrieves the values I need:

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

This is the markup that places this to my page:

<li><a id="a-<?php echo $out ?>"><?php echo implode('</li><li>">', $frontValues); ?></a></li>

The problem is that the echo $out is retrieved only ones, but there are more items in the array and all of them get's the same value, witch is not good for me.

The problem is that I can't figure out, how to make an Asociated Array to the values of $frontValaues array and add accordingly assign the correct id to the retrieved item.

I tryed adding

<?php $colorValues[$colors]=$colors; ?>

in foreach, and then replicating the same implode method, like this:

<li><a id="a-<?php echo implode('', $colorValues); ?>"><?php echo implode('</li><li>">', $frontValues); ?></a></li>

but then this array is adding all the values to the ID.

How can I separate them? So the only correct retrevied value would be asociated to the <a>.

I'm quite a newby here, so any suggestion is welcome.

Thanks,

Full code and working for anyone who may be interested in it

                <div class="color">
                  <?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 
                            $output = array();
                            foreach($allProducts as $p){
                              if(!$p->isSaleable()) continue;
                              $output[$p->getAttributeText($pAtt->getName())] = array(
                                            'out' => $p->getAttributeText($pAtt->getName()),
                                            'color' => $p->getData(strtolower($pAtt->getName()))
                                          );
                            }
                         ?>
                          <?php foreach($output  as $key => $value) : ?>
                            <li><a id="a-<?php echo $value['color'] ?>"><?php echo $value['out'] ?></a></li> 
                            <?php endforeach; ?>
                         </ul>
                        </div>
                      <?php endforeach ?>
                      </div>
                    <?php endif ?>
                  <?php endif ?>
                </div>
3

Not sure if I understand your issue correctly but try

foreach($allProducts as $p){
  if(!$p->isSaleable()) continue; 
  $out = $p->getAttributeText($pAtt->getName());
  $frontValues[$out]=$out; 
  $colors=$p->getData(strtolower($pAtt->getName()));
  $colorValues[$out]=$colors; 
}


<?php foreach($frontValues  as $key => $value) : ?>
   <li><a id="a-<?php echo $colorValues[$key] ?>"><?php echo $value ?></li> 
<?php endforeach; ?>

or

$output = array();
foreach($allProducts as $p){
  if(!$p->isSaleable()) continue; 
  $output[$p->getAttributeText($pAtt->getName())] = array(
                'out' => $p->getAttributeText($pAtt->getName()),
                'color' => $p->getData(strtolower($pAtt->getName()))
              );
}

<?php foreach($output  as $key => $value) : ?>
   <li><a id="a-<?php echo $value['color'] ?>"><?php echo $value['out'] ?></li> 
<?php endforeach; ?>
  • Hey, I tryed to change to your code and I got nothing. But I can see the logic in your code and I think you understood correctly. I've added full code for the items, so maybe there is something what's killing your code. Could you please see it above. Thanks, – Artur Rain Oct 24 '14 at 15:40
  • Ok, this works, but not correctly. This array takes to the account the number of items are available with that color. For example: I have 5 pink shoes, then he show's 5 "Pink" colors. How to ignore it? So only 1 color of each would be added? – Artur Rain Oct 24 '14 at 15:47
  • Please add an example of what you want the output to be (html format) – Renon Stewart Oct 24 '14 at 15:49
  • <li><a id="a-<?php echo $value['color'] ?>"><?php echo $value['out'] ?></li> this output is correct when rendered. But the thing is, that for each, takes into account the number of Items (different sizes) and then for everysize it gets a color. Please find a screenshot here: imgur.com/m0AoYzh – Artur Rain Oct 24 '14 at 15:53
  • I think this should be modified to get not all configurable attributes, but only color: <?php $attributes = $_product->getTypeInstance(true)->getConfigurableAttributes($_product) ?> – Artur Rain Oct 24 '14 at 15:55

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.