2

I have this PHP code (found of Stackoverflow): This shows the available attributes on the catalog view page in Magento.

 <?php if($_product->isConfigurable()): ?>

      <?php //get attributes
      $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 //check stock, status, ...
             //do not show unsaleable options
           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 due to my theme i have to include this in a Helper file (data.php)

Could anyone help me convert this into a piece of code suitable for the data.php?

I tried to remove all <?php and ?>. But that doesn't work.

  • Maybe try Mage::app() at the top – Tim Hallman May 23 '15 at 23:46
2

It won't help you on a long term if we just rewrite the code for your helper somehow (which we probably can't due to some missing information).

It's not a good practise to generate a lot of output via Helpers. Better use blocks and corresponding .phtml files for this.

What you need to know and do:

The code you posted above is intended to run in a template file (.phtml) which is a mix of PHP and HTML. That's why you have a lot of PHP-Code (enclosed by PHP opening and closing tags) mixed with HTML (<ul>, <li>,...).

To do:

  • Remove the PHP-Tags (<?php ... ?>)
  • Generate the output HTML-code: Define a variable like $html that 'collects' all the output you want to generate in your helper method and that is returned. Then add all the output to it: $html .= '<ul>'; instead of <ul>. At the end of the function, return the output: return $html;
  • Make sure the $_product variable is available in your helper method (depending on your method(s) )

This is quite hackish though.

1

This may or may not help you out as you may need to return the output but its a start

    public function getAvailableAttributesHelper() {

    //Get Current Product
    $product_id = Mage::registry('current_product')->getId();
    $_product   = Mage::getModel('catalog/product')->load($product_id);

    if($_product->isConfigurable()):

    //Get Attributes
    $attributes = $_product->getTypeInstance(true)->getConfigurableAttributes($_product);
    if(count($attributes)): 
    //<ul>
    foreach($attributes as $att): 
    $pAtt=$att->getProductAttribute();
    //get the child products
    $allProducts = $_product->getTypeInstance(true)->getUsedProducts(null, $_product);
    $frontValues =array();
    //<li>
    $pAtt->getFrontendLabel();
    //<ul>
    foreach($allProducts as $p):
    //check stock, status, ...
    //do not show unsaleable options
    if(!$p->isSaleable()) continue;
    $out=$p->getAttributeText($pAtt->getName());
    $frontValues[$out]=$out;
    endforeach;
    //<li>
     implode('</li><li>', $frontValues);
    //</li>
    //</ul>
    //</li>
    endforeach;
    //</ul>
    endif;
    endif;

    //Call in template files(.phtml)
    //echo Mage::helper('mymodule')->getAvailableAttributesHelper()

    }

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.